Effective communication in a component system

Yes, this is another question about my game engine that is going really well thanks to you guys.

So, if you've watched the video (or not), the objects in the game are made up of various components for things like position, sprites, movement, collision, sounds, health, etc. I have several message types defined for the "tell" type of communication between objects and components, but this is only getting through so far. There are many times when I just need to ask for something, like the position of an entity.

There are dozens of lines in my code that looks like this:

SomeComponent comp = (SomeComponent)entity.GetComponent(typeof(SomeComponent));
if (comp != null) comp.GetSomething();

      

I know this is very ugly, and I know the casting of odors is the wrong OO design. But whatever one may say, it seems that there is no better way. I could of course "hardcode" my component types and just have

SomeComponent comp = entity.GetSomeComponent();

      

but it looks like a failure, and a bad one.

I literally JUST REALIZED by writing this, getting my code this way for months without a solution, a generic movie will help me.

SomeComponent comp = entity.GetComponent<SomeComponent>();

      

It's amazing how it works. If anything, this is still just a semantic improvement. My questions remain.

  • Is it really that bad?
  • What's the best alternative?
+2


a source to share


3 answers


Without knowing the overall design of your game, it's difficult to know if this template is good or bad in isolation. If it works and works well, the only potential argument against it is architectural - will it scale?



0


a source


I am working on a game and I am using type parameters to search for other components in the same way. I got the idea from http://t-machine.org/index.php/2010/05/09/entity-system-1-javaandroid/ . I'm not sure if this is the best way, it seems like a misuse of the generics system. It works though.



0


a source


Why do you think this is bad?

This is what generics were created for.

0


a source







All Articles