Skip a specific iteration in a FOR loop
Skip a specific iteration in a FOR loop
In a game I am programming with Batch, a specific function is used to draw the map with a preset tileset. It's fairly complicated and takes more than a few seconds to draw the entire full color map.
I am trying to put an icon in for the player. Rather than changing the map data, I would like to implement it entirely within the graphics subroutine.
Right now, the icon displays, but it pushes the entire line of graphics back one space. I would like to make it skip the tile that the player is on top of, but the only way to do this seems to be to do something similar to ye olde NEXT command in BASIC. I am not aware of a Batch equivalent to this, so I have come here for help. I need to skip a specific iteration of the FOR loop after the player tile is drawn.
This won't make much sense without a considerable amount of context, but here's the subroutine as it stands:
:D
REM Draw tiles
IF /I %OSV% LSS 3 (
FOR /L %%G IN (0,1,31) DO (
REM Old tileset
IF "%2" == "1" IF "%%G" == "%X%" CALL :C 0C "i" & REM skip the rest
IF "!M%MAP%%1:~%%G,1!" == "0" CALL :C 00 " "
IF "!M%MAP%%1:~%%G,1!" == "1" CALL :C 2A "w"
IF "!M%MAP%%1:~%%G,1!" == "2" CALL :C 87 "S"
IF "!M%MAP%%1:~%%G,1!" == "3" CALL :C 6E "W"
IF "!M%MAP%%1:~%%G,1!" == "4" CALL :C 0A "T"
IF "!M%MAP%%1:~%%G,1!" == "5" CALL :C 0A "n"
IF "!M%MAP%%1:~%%G,1!" == "6" CALL :C 6E "H"
IF "!M%MAP%%1:~%%G,1!" == "7" CALL :C 91 "m"
IF "!M%MAP%%1:~%%G,1!" == "8" CALL :C 19 "m"
IF "!M%MAP%%1:~%%G,1!" == "9" CALL :C B3 "M"
IF "!M%MAP%%1:~%%G,1!" == "A" CALL :C 4C "m"
IF "!M%MAP%%1:~%%G,1!" == "B" CALL :C C4 "M"
IF "!M%MAP%%1:~%%G,1!" == "C" CALL :C 6E "O"
IF "!M%MAP%%1:~%%G,1!" == "D" CALL :C 6E "W"
IF "!M%MAP%%1:~%%G,1!" == "E" CALL :C 78 "W"
IF "!M%MAP%%1:~%%G,1!" == "F" CALL :C 0D "X"
IF "!M%MAP%%1:~%%G,1!" == "G" CALL :C 6E "E"
IF "!M%MAP%%1:~%%G,1!" == "H" CALL :C 48 "E"
IF "!M%MAP%%1:~%%G,1!" == "I" CALL :C 68 "n"
IF "!M%MAP%%1:~%%G,1!" == "J" CALL :C 6E "m"
IF "!M%MAP%%1:~%%G,1!" == "K" CALL :C E6 "W"
IF "!M%MAP%%1:~%%G,1!" == "L" CALL :C 6A "T"
IF "!M%MAP%%1:~%%G,1!" == "M" CALL :C 2A "w"
IF "!M%MAP%%1:~%%G,1!" == "N" CALL :C 87 "S"
IF "!M%MAP%%1:~%%G,1!" == "O" CALL :C 0D "i"
IF "!M%MAP%%1:~%%G,1!" == "P" CALL :C 6A "Y"
IF "!M%MAP%%1:~%%G,1!" == "Q" CALL :C BF "C"
)
)
GOTO :EOF
Please note I removed the advanced ("Fancy") tileset because it added more complexity and didn't show up correctly here.
After the character is drawn (the sixth line), I need to skip the rest of the sequence to prevent it from drawing the tile that the player is standing on top of.
The variables passed to the function are:
There are no outputs except to the terminal via the C function, which simply draws a colored character.
Any ideas would be great.
EDIT: Here is some example outputs without the player icon. The red nembers indicate the player's position. I would like to replace the tile at this position with the player's icon.
Overworld screenshot

Desert screenshot

The loop makes no sense without probably hundreds of lines of context to back it up, but okay.
– FAMICOMASTER
Sep 16 '18 at 8:08
You shouldn't post the whole code. Just a Minimal, Complete, and Verifiable example is needed. Otherwise who knows what kind of loop are you using? Looping through strings, lines or numbers?
– phuclv
Sep 16 '18 at 8:11
I posted the subroutine I am using, it is mostly composed of one big loop that uses many external variables and is generally unreadable without taking a good long look at it. Enjoy.
– FAMICOMASTER
Sep 16 '18 at 8:16
you should put the comment on the line you mention. "The sixth line" is not helpful. And give examples on how the output looks like
– phuclv
Sep 16 '18 at 15:10
1 Answer
1
You didn't create an MCVE and your code doesn't make sense since if it stops after the 6th line then the rest is just useless and there's no need to loop 32 times. Thus this is just an example
To prevent the block from running when a condition is true, just invert the condition
FOR /L %%G IN (0,1,31) DO (
rem Things that will done every time
IF NOT "%%G" == "6" (do something) else (
rem Things that will be done when %%G != 6
rem i.e. if %%G != 6 next
)
)
In this case if you want to skip the remaining commands when the conditions on the first line is true then
FOR /L %%G IN (0,1,31) DO (
IF "%2" == "1" IF "%%G" == "%X%" (CALL :C 0C "i") ELSE (
IF "!M%MAP%%1:~%%G,1!" == "0" CALL :C 00 " "
IF "!M%MAP%%1:~%%G,1!" == "1" CALL :C 2A "w"
IF "!M%MAP%%1:~%%G,1!" == "2" CALL :C 87 "S"
IF ...
)
)
Again the for loop is not necessary, since every loop you only jump into one case. The first line looks like it's checking if that's the player, so just split it into a separate function
IF /I %OSV% LSS 3 (
IF "!M%MAP%%1:~0,1!" == "0" (call :checkplayer %2 %%G %X% || CALL :C 00 " ")
IF "!M%MAP%%1:~1,1!" == "1" (call :checkplayer %2 %%G %X% || CALL :C 2A "w")
IF "!M%MAP%%1:~2,1!" == "2" (call :checkplayer %2 %%G %X% || CALL :C 87 "S")
...
)
:checkplayer
IF "%1" == "1" IF "%2" == "%3" (
CALL :C 0C "i"
exit /b 0
) ELSE (
exit /b 1
)
This is not useful, it only displays the player character once, the rest of the program loops 32 times to draw the 32 horizontal tiles in each line of the map. The player can only be in one place at any given time, and the script as is draws the player in the correct position but draws the tile underneath him next as well. This misaligns the whole rest of the map and is very annoying, I'd like it to skip the rest of the loop after it draws the player.
– FAMICOMASTER
Sep 16 '18 at 9:10
It is not checking for the player, it is checking for the player before drawing the icon and then continuing to draw the entire rest of the map. I'm not going to call out the exact point where the player is, I would have to draw every single tile individually!
– FAMICOMASTER
Sep 16 '18 at 21:50
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
what's your for loop? show your code first
– phuclv
Sep 16 '18 at 7:45