create ship parts

BlitzMax Forums/BlitzMax Programming/create ship parts

slenkar(Posted 2008) [#1]
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)


Mahan(Posted 2008) [#2]
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.


Brucey(Posted 2008) [#3]
Note also that Reflection is much much slower than equivalent if/select code.


slenkar(Posted 2008) [#4]
ok thanks , ill have to stick with the original way of doing it,(i need the create function)


Czar Flavius(Posted 2008) [#5]
A shot in the dark but maybe each button could contain a function pointer to a particular type's create function.