LOOP trouble!

BlitzMax Forums/BlitzMax Programming/LOOP trouble!

Robby(Posted 2012) [#1]
Hey guys! Got a loop problem! In simplified form. its like this:
Assume the B array to be B[10] - like ten possible spaceships maybe, and I currently have 5 - NpcMany = 5


For npc = 1 To NpcMany

If B(npc) = 1 Then
B(npc) = 2
Return
End If

If B(npc) = 2 Then
B(npc) = 1
Return
End If
' and several other IF blocks

Next

The real code is much more complex - but the idea is it must RETURN when it does anything, because the next IF will change it right back.
(Or in the actual code, royally muff up a lot of stuff!)

The trouble is it never completes the loop, 1 - 5 !!! When something happens to 1, it must exit. Never reaches 2,3,4 and 5.

How do I make it go through the WHOLE loop, and when he's done something to 1, go to the NEXT one, instead of falling into the other if blocks and re-affecting the thing it just modified?

Can I just replace the RETURNS with something like
Goto nextone and label at the end:

#nextone
Next

Or is that bad programming? Really perplexed here! -Rob


Robby(Posted 2012) [#2]
When you have a programming problem, just take your dog for a walk! I did that an had an idea. All I really need to do is make the function do one character at a time, then call it from another loop. Like:

Function WalkNPCS()
Local jj
For jj = 1 To NpcMany
WalkCalc(jj)
Next jj
End Function

Function WalkCalc(npc) ' I know - need Int, etc. - just typing fast
'do all the stuff above
End Function

Then maybe a little flag at top to exit if loop not done, avoid recurrsion, etc.

This will work, but I'm still a little curious if there's a way to do it in one function, in the loop? Thanks again, -Rob


xcessive(Posted 2012) [#3]
Array indexing starts at 0, so your for loops should also start at 0, or you are always missing the first item.

Regardless, it sounds like you should be using a list, rather than an array to store your game entities.


ima747(Posted 2012) [#4]
Check the documentation section under language -> Program flow for some more detail on loop handling

1) arrays/lists/etc. are almost always zero based, meaning they start at 0 rather than 1. So if you have array[10] you have
array[0]
array[1]
...
array[9]
NO array[10], 0 - 9 = 10 objects, starting at 0

there are 2 kinds of for loops. for/to/next (which exits when the value exceeds the to value) and for/until/next (which exits when the value equals the to value). Untils are generally easier as you can just go
for i = 0 until myCollectionOffStuff.count
without having to remember that you need to subtract one for the count since arrays and lists are both zero based

Lastly to try to answer you actual question, how you can prematurely exit a single iteration of a loop without exiting the whole loop, what you want is "continue".
e.g.
for local i:int = 0 until 100
if (i  = 10)
continue
endif
print i
next

That will take i from 0 to 99 (once it hits 100 it ends the loop because it's an until, we start with 0 because collections do generally), printing each value. It will however not print 10 because it will continue the loop before reaching "next" if i is equal to 10

Hope that helps!


Zeke(Posted 2012) [#5]
If B[npc]=1 Then
	'code
ElseIf B[npc]=2 Then
	'code
ElseIf B[npc]=3 Then
	'code
EndIf

or
Select B[npc]
	Case 1
		'code
	Case 2
		'code
	Case 3
		'code
End Select