Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to make a modular ship system, where parts can be added or removed to the ship entity.

I am using a class I made called ModuleManager and it has a list of ShipModules. this works fine for very basic block building ships, but I want to add a ThrusterModule that has thrust capabilities, and possible a GunMountModule for obvious reasons.

the problem is that I cannot do this.

List<ShipModules> modules = new List<ShipModules>();
ThrusterModule engine = new ThrusterModule();
modules.Add(engine);

I know that I cannot do this because they are different types.

Question: How can I add all these modules to the module manager? because I don't want to have to deal with having several different type modules that need several different variables to hold them

I have an idea of what to do, but I want to know if it would be the best route, I have heard of Class Composition. would that be beneficial for what I have explained? thanks.

share|improve this question
1  
Your questions are pretty borderline between game development and just general programming. You may want to search for answers to these types of questions on stackoverflow.com before asking here. Try to think how you'd generalize the question to more general programming. For example, this question would just be asking how to store multiple different types in the same list. –  Byte56 Sep 24 '13 at 2:01

3 Answers 3

This is essentially the same problem you had before. If all the modules inherit from the same class you can add all of them to the same list.

You could store a type ID in the ShipModules class to identify what type of module the module is for the purpose of casting it. However, I'd suggest that if you want to have specific module functionality, you keep all those modules of the same type in a separate list.

So, if you want to have a list of ThrusterModule's only, you can create a list specifically for them.

share|improve this answer
    
but then how do I access the child information? if I have a ThrusterModule(that is a ShipModule). but I only have the ShipModule portion in a list, how do I access the data held only in the ThrusterModule? –  Joseph Dour Sep 24 '13 at 2:13
1  
As I said, you cast it. That's done with (ThrusterModule)module. You have to ensure the object you're casting is actually the type you're casting to or you'll have exceptions. Typically you want to avoid this, which is why it's preferred to not do that and keep type specific lists when you need to access the "child" information. –  Byte56 Sep 24 '13 at 2:21
    
Interfaces are a good choice too –  Blau Sep 24 '13 at 7:56

your ship must have a components map, then it can have one IEngine, by example, but you can create a lot of inherited Engine and switch as you want.

A good book about game architecture : http://www.amazon.fr/Game-Coding-Complete-Mike-McShaffry/dp/1133776574

share|improve this answer
1  
All you need is Inheritance and polymorphism –  mcamier Sep 24 '13 at 11:55

as Blau said, try with interface instead. Create a basic IShipModule interface, and you'll extend it to all the classes that will act as module for your ship.

 class GunMount : IShipModule

 class Thruster : IShipModule

and your list will holds all the classes that extends the interface IShipModule .

Look here for explanation, its in java but you should understand anyway

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.