link a button to an object?

Monkey Forums/Monkey Programming/link a button to an object?

slenkar(Posted 2011) [#1]
If I had 3 different classes,
dog,horse,cat

and then I had 3 buttons on screen with the same name as the classes,

Could I get monkey to create a new dog when I press the 'dog' button without explicitly programming it?

i.e.
without having to type
if button_pushed.name='dog'
local d:dog=new dog()
endif

could this be done with generics or interfaces?


Gerry Quinn(Posted 2011) [#2]
No, but there's a guy selling a reflection solution.

Oh wait...


Samah(Posted 2011) [#3]
You could do some kind of mapped templating...



slenkar(Posted 2011) [#4]
thanks samah,nice little example

where does template:dog come from?


Samah(Posted 2011) [#5]
Since there's no way of getting the "class" from a string (at runtime), you'll need to make an instance of any type of class that you think might be needed and put it in a map. As long as that class has an instance method to create a new object, you can do it that way.

Simpler example: