Question about Lists (AGAIN)

BlitzMax Forums/BlitzMax Beginners Area/Question about Lists (AGAIN)

Takis76(Posted 2014) [#1]
Hello,

I have read some books about lists for blitzmax and I am trying to implement them in my game.
I have done some work but I have a problem.

We assume I have one type which holds the dropped items on the level.

Type My_dropped_items

	Field level:Byte 'The level the item is on
	Field x:Byte 'The position x on the map
	Field y:Byte 'The position y on the map
	Field quadrant:Byte 'The quadrant of the square of the map
	Field item_number:Int 'The item number 1=Sword 2=Potion etc...
	
EndType
Global dropped_items:My_dropped_items = New My_dropped_items


The type above holds my dropped items on some level , on some position in the map , the item number and the quadrant of each square. Each square holds 4 quadrants of items.

I am creating a list of dropped items which its variable is global

Global Floor_Items_List:TList = CreateList()


And then I add 3 items on this list manually to test the code. Later this could be populated loading the level from file , for now manually.

	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 6
	dropped_items.item_number = 1
	dropped_items.quadrant = 1
	ListAddLast(Floor_Items_List, dropped_items)

	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 6
	dropped_items.item_number = 1
	dropped_items.quadrant = 2
	ListAddLast(Floor_Items_List, dropped_items)
		
	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 7
	dropped_items.item_number = 1
	dropped_items.quadrant = 4
	ListAddLast(Floor_Items_List, dropped_items)


The code above add 3 items
1: For Level 1 at position 5,6 and item number 1 in quadrant 1
2: For Level 1 at position 5,6 and item number 1 in quadrant 2
3: For Level 1 at position 5,7 and item number 1 in quadrant 4

Then if I want to see all of those items I use this code which I supposedly learnt from the books:

For dropped_items = EachIn Floor_Items_List
     If dropped_items.quadrant = 1
     (show the sprite of the item on first quadrant of the floor square) <-This is a pseudo code
     Endif
     If dropped_items.quadrant = 2
     (show the sprite of the item on second quadrant of the floor square) <-This is a pseudo code
     Endif
     If dropped_items.quadrant = 3
     (show the sprite of the item on third quadrant of the floor square) <-This is a pseudo code
     Endif
     If dropped_items.quadrant = 4
     (show the sprite of the item on fourth quadrant of the floor square) <-This is a pseudo code
     Endif

next


According to the code above I must see all the items for each squares in each of its quadrants. But one strange thing is happening. I see only the last one.

The Count() command of the list returns 3 items

DrawText(Floor_Items_List.Count() , 200, 100)


Why do I see only the last item of the list and I should seen all items, because I have a for eachin loop. (This is in the main game loop)

where is my error?


Thank you :)


Jesse(Posted 2014) [#2]
When you use 'New' on a Type in essense you are creating an instance of the type or what is know as an object. The instance is stored in memory and the address location is stored in the variable used to

assign the new instance. In your case, you created the instance, stored the address in the list, changed the variable values of the instance, decided you were going to put it again in the list and finally

changed the value of the variables and put the same object again on the list. you were actually modifying the same object multiple times. and trying to put the address of the same object on the list multiple times.

If you want to store multiple objects in the list you need to create multiple instances and store each instance in the list each time

this is what you are missing:
	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 6
	dropped_items.item_number = 1
	dropped_items.quadrant = 1
	ListAddLast(Floor_Items_List, dropped_items)

	dropped_items = New My_dropped_items '*********************************************
	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 6
	dropped_items.item_number = 1
	dropped_items.quadrant = 2
	ListAddLast(Floor_Items_List, dropped_items)
		
	dropped_items = New My_dropped_items '***********************************************
	dropped_items.level = 1
	dropped_items.x = 5
	dropped_items.y = 7
	dropped_items.item_number = 1
	dropped_items.quadrant = 4
	ListAddLast(Floor_Items_List, dropped_items)



Jesse(Posted 2014) [#3]
.


Takis76(Posted 2014) [#4]
Perfect , it worked. Thank you very very much... :D