@ECHO OFF
SETLOCAL
@ECHO OFF
SET map=CGWGWBBBTB
SET playerposition=1
CALL SET playereposition=%%map:~%playerposition%,1%%
SET play
SET playerposition=2
CALL SET playereposition=%%map:~%playerposition%,1%%
SET play
SET playerposition=3
CALL SET playereposition=%%map:~%playerposition%,1%%
SET play
You could also use a subroutine:
SET playerposition=4
CALL :setsubstr playereposition map %playerposition% 1
SET play
GOTO :EOF
:setsubstr
SETLOCAL ENABLEDELAYEDEXPANSION
SET "return=!%2:~%3,%4!"
endlocal&SET "%1=%return%"
GOTO :EOF
Here, the temporary variable return
is used to contain the substring-from-3rd-parameter-length-4th-parameter-of the string second-parameter into variable-name first-parameter.
The endlocal&...
disposes of the temporary variable and uses a parsing trick to assign the value to %1
.
Note that using this approach also allows the routine to be made "smart" by allowing (with changes) an omitted 4th parameter to default to 1, for instance.
Note also in all of this that position-counting in a string starts from 0
, not 1
.