create ship parts
BlitzMax Forums/BlitzMax Programming/create ship parts
| ||
How would I do this with reflection? I have a some ship parts e.g.(reactor, shield, hull, laser) They are all types derived from Type Ship_part endtype I have to press a button in the game to create a new ship part I have the buttons name that was pressed e.g. reactor At the moment Im doing a select case test to see what part was selected and then creating it manually in code. How would I create a certain ship part using reflection? (calling the create function that every ship part has) the create function is like this: reactor.create(s:ship,x,y) |
| ||
When you say "create a ship part" do you simply mean that you want to instanciate an object by it's class name? If thats what you mean you can do it this way with reflection: Local newTypeId:TTypeId = TTypeId.ForName(<class name string>) Local t:Ship_part = Ship_part(newTypeId.NewObject()) Note however that if the ship parts (inherited classes) have different properties you'll still have to cast to the proper Ship_part to use those properties, again using reflection or a select..case EDIT oh, and I forgot to mention: This will only instanciate the object but any factories like your create would eighter have to be rewritten as methods, or you'd have to setup things in your object from the outside after instanciating it this way. |
| ||
Note also that Reflection is much much slower than equivalent if/select code. |
| ||
ok thanks , ill have to stick with the original way of doing it,(i need the create function) |
| ||
A shot in the dark but maybe each button could contain a function pointer to a particular type's create function. |