Map Container used for multiple Instances

Monkey Forums/Monkey Programming/Map Container used for multiple Instances

Gamer(Posted 2012) [#1]
I have two questions.

I am wondering if i can use an IntMap, or another Container to hold multiple Instances of a variety of Classes. Right now i have something like this.



Maybe i am going about this the wrong way. But what i would like to do is have different classes that inherit UnitBase and store them in my IntMap. Say a Class for a Zombie, and then a class for a Orc, ect. That way all of my instance for the monsters in the game are stored in a single container.

Also when i create and Instance i notice that OnCreate() is not run.

Thanks.


Gamer(Posted 2012) [#2]
Wait. I can use a variety of IntMaps, one for each Class i would like to store, and contain the IntMaps inside a StringMap. I think... I would still like some feed back in case i am overthinking or underthinking what i am trying to do.

Thanks.


marksibly(Posted 2012) [#3]
Hi,

Use inheritance to store multiple 'subclasses' of a class in the same container, eg:

Class Unit
   Method Update()...blah
End

Class Zombie Extends Unit
   Method Update()...blah
End

Class Pixie Extends Unit
   Method Update()...blah
End

Global Units:=New IntMap<Unit>

Function Etc()
   Units.Set 0,New Unit
   Units.Set 1,New Zombie
   Units.Set 2,New Pixie
End


'Extends' means that a class 'is a kind of' some other class, so you can use it whether the other class is expected.

Also, use Method New() to construct objects - OnCreate() is just for the Mojo App.


Gamer(Posted 2012) [#4]
Got it, thanks allot!