Whatīs wrong with this crappy loop

BlitzMax Forums/BlitzMax Beginners Area/Whatīs wrong with this crappy loop

Lillpeter(Posted 2005) [#1]
Canīt seem solve this, iīm just trying to iterate through my enemy list with:

For enemy:enemy_type = EachIn enemy_list
enemy.move()
Next

I recently upgraded my Bmax and i saw there was some changes for the lists so i guess it has something to do with that, but i canīt see what iīm doing wrong even with the documentation at hand. I get the following error:

Compile error
ForEach must be used with a string, array or appropriate object

As far as i know my objects are very appropriate, could it be that the list is empty until i add an object?


Perturbatio(Posted 2005) [#2]
Type TEnemy
	Global List:TList
	
	Method Move()
		Print "moving"
	End Method
	
	Function Create:TEnemy()
		Local tempEnemy:TEnemy = New TEnemy
			If Not List Then List:TList = New TList
			List.AddLast(tempEnemy)
		Return tempEnemy
	End Function
End Type

Tenemy.Create() '1
Tenemy.Create() '2
Tenemy.Create() '3
Tenemy.Create() '4
Tenemy.Create() '5

For Local enemy:Tenemy = EachIn TEnemy.List
	enemy.Move()
Next



Rimmsy(Posted 2005) [#3]
I don't think bmx has made any notable changes to Tlist. It looks like you're most likely simply not defining the enemy_list somewhere. Are you sure it's in scope?

Have you got Strict at the top of your code? It's a must. Are you defining enemy as a local to loop? Like for local enemy:enemy_type = eachin enemy_list

global itemList:Tlist=createlist()

type item
  field x
 method update() end method
end type

function updateAll()
 for local i:item = eachin itemList
 i.update()
next
end function



Rimmsy(Posted 2005) [#4]
Damn you petry!


Perturbatio(Posted 2005) [#5]
Damn you petry!


Damn me too :)


Lillpeter(Posted 2005) [#6]
Ah crap! I forgot to make the list global.
Well, problem solved, thanks for your help.

Rimmsy, what do you mean with strict beeing a must? i know what it means but i havnīt used it, perhaps i should?


deps(Posted 2005) [#7]
just put strict at the top of the file. The compiler will yell at you every time you are using a variable that isn't in scope. This also means that you from now on must always put local or global in front of new variables. (unless when they are fields in a type)

It's a must because it help you solve all those bugs that pops up when you misspell a variable or, as in this case, trying to use a variable that isn't in the current scope or global.

Lycka till med spelet.


Rimmsy(Posted 2005) [#8]
I'd never go back. The amount of times I've saved myself from going loopy because of a misspelt local variable.

There's not real reason not to have it on. After a while you just get into the habit of declaring everything before hand it and it feels a lot nicer to program with. I think goto doesn't work in strict mode but who the hell uses that these days?

Give it a go, you'll like it.


Perturbatio(Posted 2005) [#9]
not only that, I believe strict mode gives a (slight) performance boost.