Jump to content
  • Advertisement
Sign in to follow this  
Akitsune

C# object design

This topic is 3043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a C# project for a while now and have been running into some problems with my object model. My world has online persistence and uses a serialization library, which just adds more complication to the whole situation.

My current model is that all objects fall into some very brief categories, the most troublesome being my node category. Nodes are grid-aligned, collision affecting entities with a non-static string property representing 3d model path. This means that every single Node can have a different 3d model. Nodes are intended to be used in a similar fashion to structures, doodads, and destructibles in the WC3 editor.

My nodes feel VERY weakly typed (I know this isn't the proper term). The alternative (and arguably proper way) is to make a class derived from Node, but this also can be a pain to do. I would presumably need a List generic for each new class, right? Considering how many new classes (50+) I would be implementing, this seems counter-intuitive.

Is there a better way to organize my objects? An ID-based system, perhaps?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Valkoran
Why do you think that you would need to create a list for each new class derived from Node?


Maybe I don't properly understand the List generic. I was under the impression that by making a Wall : Node class and a Door : Node class I wouldn't be able to store both within a List<Node>. I assume I am incorrect?

What if Door had an extra property, bool Open?

Share this post


Link to post
Share on other sites
all types in c# derive from the object class so as a last resort you can group them as a list of Object type and cast into whatever classes you need

but as has been said list in c# are generic so you can store any type you want

all you need to do is a foreach loop looking for the type youre currently working with


hope I made any sense lol im on my 6th monster now

Share this post


Link to post
Share on other sites

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
// one liners for demonstration
class Enemy { public void Shoot() { } }
class Ship : Enemy { public void Fly() { } }
class Turret : Enemy { public void Target() { } }

class Program
{
static List<Enemy> CreateEnemies()
{
var Random = new Random(DateTime.Now.Millisecond);
var Enemies = from i in Enumerable.Range(0, 10)
select (Random.Next() % 2 == 1) ? (Enemy)new Ship() : (Enemy)new Turret();
return Enemies.ToList();
}

static void Main(string[] args)
{
var Enemies = CreateEnemies();

// Type by default: Enemy
foreach (var e in Enemies)
{
e.Shoot();
}

// only the ships
foreach (var s in Enemies.OfType<Ship>())
{
s.Fly();
s.Shoot();
}

// only the turrets
foreach (var t in Enemies.OfType<Turret>())
{
t.Target();
t.Shoot();
}
}
}
}

Share this post


Link to post
Share on other sites
You guys all understood the problem perfectly, thanks! Rather than using your solution, davepermen, I'm using virtual and override (but your example proved that my question about bool Open is possible).

Share this post


Link to post
Share on other sites
Quote:
Original post by Akitsune
Quote:
Original post by Valkoran
Why do you think that you would need to create a list for each new class derived from Node?


Maybe I don't properly understand the List generic. I was under the impression that by making a Wall : Node class and a Door : Node class I wouldn't be able to store both within a List<Node>. I assume I am incorrect?

What if Door had an extra property, bool Open?


This works fine in C# - assuming Node is a class (not a struct). The <Node> qualifier on the List makes sure, at compile-time, that everything that goes into the List is some kind of Node (or else the compiler will complain that it can't guarantee this). Walls and Doors are kinds of Nodes, so this is not a problem. At run-time, when you pull things back out of the list, you don't know what kind of Node they are, but you do know that they're Nodes. The compiler will complain about code that assumes you have a Door, but you can work around this with polymorphism or (please don't do this unless you really have to) explicitly type-checking and casting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Akitsune
You guys all understood the problem perfectly, thanks! Rather than using your solution, davepermen, I'm using virtual and override (but your example proved that my question about bool Open is possible).


sure that's the way to go. just wanted to show that you can use baseclasslists, call the baseclass methods (which might be overridden of course), and can even access the items of certain types in that list easily, if the need comes one day. in a perfect encapsulated oo world, that should never happen. then again, i don't care. if it works good for the situation, i use it (i use that OfType style interfacing often in winforms, where i get access to all controls of a window, and maybe want to alter all buttons.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!