lists of items in TYPES

BlitzMax Forums/BlitzMax Beginners Area/lists of items in TYPES

bradford6(Posted 2004) [#1]
I am trying to create a TYPE that has the ability to hold a list of types in it's namespace. the example i have is series of rooms and a bunch of stuff. since stuff can only be in a single room at a time, how would I associate the particualr stuff with the room. keep in mind that 'stuff' is an object of type TYPE.

stuff = a laptop and a stapler

rooms = kitchen and bedroom


this is what I have so far. Is there a better way?

Global all_things:Tlist = CreateList()

For i$ = EachIn ["spoon","car","TV","telephone","Laptop"] 
	my:stuff = New stuff
	my.name = i$
	ListAddLast all_things,my
Next

Type stuff 
	Field x,y
	Field name$ ' name of this thing
	Field room$ ' room that this thing is currently in
End Type 

Type room
	Field name$
	
	Method add_a_thing(xp,yp,name_of_thing:String)
			For b:stuff = EachIn all_things
				If b.name = name_of_thing
					b.room$ = name$
				EndIf
			next
	End Method
	
	Method list_things()
		Print "The "+name+" has:"
		For b:stuff = EachIn all_things
			If b.room$ = name$
				Print b.name$
			endif	
			
		next 
	End Method
	
End Type


' -----------------------------------------------------------------------

kitchen:room = New room
	kitchen.name = "Kitchen"
		kitchen.add_a_thing(100,100,"car")
		kitchen.add_a_thing(100,100,"spoon")
		kitchen.add_a_thing(100,100,"Laptop")

bedroom:room = New room
	bedroom.name = "Bedroom"
		Bedroom.add_a_thing(100,100,"TV")
		Bedroom.add_a_thing(100,100,"telephone")
		Bedroom.add_a_thing(100,100,"spoon") 		' spoon is same object as spoon in kitchen

kitchen.list_things()		
Bedroom.list_things()
Print " =================== "
		Bedroom.add_a_thing(100,100,"Laptop") 			' taken from the kitchen
		Bedroom.add_a_thing(100,100,"foosball table")  ' does not exist in list of all_things
	
kitchen.list_things()		
Bedroom.list_things()



skn3(Posted 2004) [#2]
Just give each room a TLIST field. Then you can just add/edit/release from any rooms list of items. If you want each item to only be allowed in 1 list, just make sure when you change from one room TLIST to enother room TLIST, that it removes and adds correctly from each of the rooms TLISTs


FlameDuck(Posted 2004) [#3]
Yeah, the most practical way to do this is to have a list of rooms, and have each room have a list of items.

The problem here is that you might have to do sanity checks to ensure that the item is actually removed from it's orioginal list, which depending on design might have to recurse (or itterate) through all room lists.

An alternative was to have a long list of all items, and let each have a room field. The disadvantage here is that you have to itterate though the entire (or more accurately on average half the list) every time you list the contents of a room.

So it depends on what you'll be doing most often. Listing room inventory, or moving objects around.


skn3(Posted 2004) [#4]
well you dont have to do a full scan, you could just store a link in each item to which room it is currently in. Obviously you would update this when you make any changed to an item.


FlameDuck(Posted 2004) [#5]
well you dont have to do a full scan, you could just store a link in each item to which room it is currently in.
You could, but you probably shouldn't (as you'd then have a cyclic reference and have to take care in which order you de-reference objects to avoid memory leaks).