Nested Types?

BlitzMax Forums/BlitzMax Beginners Area/Nested Types?

BeardKingX(Posted 2013) [#1]
I am trying to figure out how to do something along the line of the following:

A player type with an inventory filled with item types (so each player would have there own collection of item types separate from other players).

I attempted to do this in blitzplus as follows:

Type player
	Field name$
	Field inventory.item
End Type

Type item
	Field name$
End Type
p.player = new player
	p\name$ = “steve”
	p\inventory.item = new item
		p\inventory\name$ = “sword”


…but quickly discovered this does not actually nest the item within the specific player type. Is this possible in blitzmax? If so could someone show me a markup of the appropriate syntax?


Zeke(Posted 2013) [#2]
SuperStrict

Type Player
	Field name$
	Field inventory:TList=New TList
End Type

Type item
	Field name$
End Type

Local p:player=New player
p.name="steve"
Local i:item=New item
i.name="sword"
p.inventory.addlast(i)

For Local i:item=EachIn p.inventory
	Print "item - "+i.name
Next

or
SuperStrict

Type Player
	Field name$
	Field inventory:item[10] '10 slots
End Type

Type item
	Field name$
End Type

Local p:player=New player
p.name="steve"
p.inventory[3]=New item
p.inventory[3].name="sword"

For Local i:Int=0 Until p.inventory.length
	If p.inventory[i] Then
		Print "item #"+i+" - "+p.inventory[i].name
	Else
		Print "item #"+i+" - <empty>"
	EndIf
Next



BeardKingX(Posted 2013) [#3]
thanks works perfectly!