What�s wrong with this crappy loop
BlitzMax Forums/BlitzMax Beginners Area/What�s wrong with this crappy loop
| ||
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? |
| ||
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 |
| ||
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 |
| ||
Damn you petry! |
| ||
Damn you petry! Damn me too :) |
| ||
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? |
| ||
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. |
| ||
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. |
| ||
not only that, I believe strict mode gives a (slight) performance boost. |