2D Array with different object types

Monkey Forums/Monkey Programming/2D Array with different object types

Raz(Posted 2012) [#1]
For ease of reference and manipulation (specifically for iterating through them and checking items against other items), I'd like to store different object types in a 2d array. as an example...

Items:GameItem[][] ' ASSUME I'VE FULLY INITIALISED THE ARRAY

Items[ItemType.COIN][0] = New Coin()
Items[ItemType.GEM][1] = New Gem()


Coins and Gems extend GameItem, but also have their own specific variables...

Class Coin Extends GameItem
    Field CoinVariable:Int
End

Class Gem Extends GameItem
    Field GemVariable:Int
End


I want to be able to do this..

Items[ItemType.COIN][0].CoinVariable = 1234


But understandably it doesn't work, because Items[][] is cast as GameItem (which isn't aware of either CoinVariable or GemVariable).

Is there a way of doing this?

Ta :)

Edit: Currently I think the easiest way to do it is to store all items in the Items[][] array as well as in item specific arrays, so Coins[] and Gems[]. This will allow me to access the specific variables as well.


Jesse(Posted 2012) [#2]
you can do it this way but it's not ideal:
local gem:Gem = Gem(itemType[item[COIN][0])
if gem   gem.CoinVariable = 1234            


the best the correct way to do it is to encapsulate the object and pass the value through a method or function.
first something to note: for example if gems, coins etc... have variable, you don't have call the gem gemVariable or the coin coinVariable, you can just call it Variable its obvious it's a gem variable or a coin variable etc if it's in its corresponding class.

To assign the value you can just call the method or the function:

Items[ItemType.COIN][0].SetVariable(1234)


Have an abstract of the SetVariable in the base class.
   method SetVariable:Void(v:int) Abstract


and in each corresponding class just have the method:
Method SetVariable:Void(v:int)
    variable = v
End method

if it's a variable all of the extended classes have, then move the variable and the method to the base class and you only have to declare the method and the "variable" only once and you won't need to declare the abstract.


Raz(Posted 2012) [#3]
Hi Jesse, thanks for responding :)

it almost sounds like I should have some nondescript variables and each sub items uses them as required. (Similar to how Klik products work with "Alterable Variable A,B and C")

I thought about this and wasn't able to come up with a way that makes referencing item specific variables easy from other items without encapsulation (when targeting Xbox, this is something I've been told to avoid).

Having a global 2D array of all items and 1D arrays of each sub item type appears to work quite well and creates basically no garbage. I was concerned about too many instances of the same object, but they're just nodes, so it really isn't going to be a problem as I doubt I will ever be dealing with thousands of objects.

All I need is a ItemType identifier and an ItemID identifier which are both ints.

Global Items:GameItem[][]
Global Coins:Coin[]
Global Players:Player[]


So Items[ItemType.COIN][ID] references the same Coin as Coins[ID].