0

I'm developing a little text-based RPG for the windows CMD. I'm trying to make a more friendly game-saving system (the actual one is based on level codes, and isn't so user friendly). How i can take a code from a .txt or another archive and use it as variable inside my batch code? here's the game-saving method i'm using: for saving:

:save1:
echo ultima partida guardada: P1cWyj (copiar el codigo en el campo de abajo tal como se ve)>C:\Users\%username%\Documents\ProjectText_Data\partida.txt
goto save load
:save2:
echo ultima partida guardada: RxcPy7 (copiar el codigo en el campo de abajo tal como se ve)>C:\Users\%username%\Documents\ProjectText_Data\partida.txt
goto save load
:save3:
echo ultima partida guardada: OeycP5 (copiar el codigo en el campo de abajo tal como se ve)>C:\Users\%username%\Documents\ProjectText_Data\partida.txt
goto save load
:save4:
echo ultima partida guardada: P4gcyK (copiar el codigo en el campo de abajo tal como se ve)>C:\Users\%username%\Documents\ProjectText_Data\partida.txt
goto save load

For loading:

:5:
cls
echo --------------------------------------------------------------------------------                              
echo                          #### ####  ####  #### ####  #### 
echo                         #   #    #   # # #  #     #   # # 
echo                         #     ####   #   #  #  ####   #   
echo                         ##    #  #   #    ##   #  #   #   
echo                          #### ##### ###  ##### ##### ###  
echo                                          #   #            
echo                                          ####           
echo --------------------------------------------------------------------------------
type C:\Users\%username%\Documents\PrjctTxt_Data\partida.txt
set /p answer=Escriba la clave de 6 digitos de arriba (respetando mayusculas y minusculas):
if %answer%==P1cWyj goto inicio_1
if not %answer%==a goto 5
  • ignore the ascii art, is the old version of the "menu". the new one is more "clean" – Matytoonist Sep 25 '20 at 17:06
  • 1
    rather than reinventing the wheel, [take a look here](https://stackoverflow.com/a/63619756/12343998) for a simple script that demonstrates a method of saving / loading user specific values by prefixing them with the $ characters and iterates over those prefixed variables with a `for /F` loop over a `Set $` command. – T3RR0R Sep 25 '20 at 18:17
  • @T3RR0R looks good, but how i can implement it? Every screen on the "gme" has his own tag (:1: for example) – Matytoonist Sep 25 '20 at 22:57
  • `for /f "usebackq tokens=4" %%a in ("C:\Users\%username%\Documents\ProjectText_Data\partida.txt") do set "correct=%%a"` See `for/?` for more information. – Stephan Sep 26 '20 at 09:13

1 Answers1

0

the example script has a macro for saving location (label) changes - Loc, as well as a Menu macro that facilitates navigation to indexed labels. An alternative Menu macro for non indexed labels can be found here. The Save macro is built into the Loc macro, and is executed whenever the Location macro is expanded. When expanding the Location macro, substring modification is used to replace @ with the Label for the current script location.

The best way for you to learn how to use the macro's would be to build a small script with them, defining any initial variables required where indicated prior to commencing the script body.

Additional remarks have been added regarding the usage of macro's to aid in their use.

@Echo off
@Echo off
:new
rem  *********************  Display any existing character names for continuation or deletion of characters
 If Exist "%TEMP%\%~n0_*_save.bat" (Echo/Your Characters:&Echo/&(For /F "Delims=" %%G in ('Dir "%TEMP%\%~n0_*_save.bat" /B')Do For /F "Tokens=2 Delims=_" %%o in ("%%~nG") Do  < Nul Set /P "=[%%o] ")&Echo/)
:character
 Set /P "Name=Name: "
 If Exist "%TEMP%\%~n0_%Name%_save.bat" (Echo/[C]ontinue / [D]elete?&For /F "Delims=" %%O in ('Choice /N /C:cd')Do If /I "%%O"=="C" (Goto :playon)Else (Del /P "%TEMP%\%~n0_%Name%_save.bat" & Goto :character))
 If "%Name%"=="" Goto :character
:playon
::: -------------------------------------------------------------------||    MACRO DEFINITIONS
Setlocal DisableDelayedExpansion
(Set \n=^^^
%= Do Not Modify =%
)
Set Menu=CLS^&Set "Copt="^&For %%n in (1 2) Do if %%n==2 (%\n%
 For %%G in (!OPTS!)Do (%\n%
  Set "opt=@%%~G"%\n%
  Set "opt=!opt:_= !"^&Set "Opt=!Opt:~,-1!"%\n%
  Set "Copt=!Copt!%%~G"%\n%
  Echo/!Opt! [%%~G]%\n%
 )%\n%
 Echo/[E]xit%\n%
 (For /F "Delims=" %%O in ('Choice /N /C !Copt!E')Do If "%%O"=="E" (Endlocal^&Endlocal^&Set "Name="^&Goto :New) Else (CLS^&Goto :@%%O))%\n%
) Else Set OPTS=
rem ***  Inventory Macro. Displays all elements for the given group and their current values.
rem :::  Usage: %INV:@=$varname[%
 Set "INV=Echo/&(For /F "Tokens=2 Delims==" %%i in ('Set @') Do (Set "VN=%%i"&^< Nul Set /P"=[!VN:$=!:!%%i!] "))&Echo/"
rem ***  Autosave macro. Can be incorperated into other macro's
rem :::  Usage: %Save%
 Set SAVE=(For /F "Tokens=1 Delims==" %%i in ('Set $') Do (If not "!%%i!"=="" Echo/Set "%%i=!%%i!"))^>"%TEMP%\%~n0_!name!_save.bat"
rem ***  Location Display Macro with autosave macro included
rem :::  Usage: %Loc:@=LocationLABEL%
 Set "Loc=(Set "$Loc=@"&Title !$Loc:_= !)&%Save%"
rem ***  Loot box Macro to generate random loot from specified range of an indexed array
rem ***  !random! %%4 + Index# will access an index range between the index # and 4 above the index number.
rem :::  Usage: %Loot:@=index#%
 Set "LOOT=(For /F "UsebackQ Delims=" %%i in (`"Set /A i#=!Random! %%4 + @"`) Do For /F "UsebackQ Delims=" %%v in (`"Set /A v#=!Random! %%3 + 1"`) Do (Set "VN=!$Loot[%%i]:$=!"&Echo/You got %%v !VN!&Set /A "!$Loot[%%i]!+=%%v")) 2> Nul & %SAVE%"
rem ***  the below  macros /I /V and /P are not used in this example. - They are an optional method for defining
rem ***  variables prefixed with $ that automatically saves them for reloading
rem :::  usage: %/I:V=Varname%Input Prompt String:
 Set "/I=For %%n in (1 2)Do If %%n==2 (Set /P "$V=!$PromptStr:$=!: "&%Save%)Else Set $PromptStr="
rem :::  usage: %/P:V=Varname%VariableValue
 Set "/V=For %%n in (1 2)Do If %%n==2 (Set "$V=!str!"&%Save%)Else Set str="
rem :::  usage: %/A:V=Varname%=Equation
 Set "/A=For %%n in (1 2)Do If %%n==2 (Set /A "$V!sum!"&%Save%)Else Set sum="
rem *** Wait prompt Macro
rem :::  usage: %Wait:#=Integer value for time in seconds%Wait Prompt String
 Set "Wait=For %%n in (1 2)Do If %%n==2 (Timeout # /Nobreak > Nul & (Pause | Echo/!Output!) 2> Nul )Else Set Output="
rem ***  Array definition macro. Asigns the element names to an indexed Groupname (Array), With each element being assigned an initial 0 value
rem :::  Usage: %DefArray%{ArrayName}{Element names as list}
 Set "DefArray=For %%n in (1 2) Do if %%n==2 ((If "!i#!"=="" (Set "i#=0"))&For /F "Tokens=1,2 Delims={}" %%G in ("!List!") Do (For %%i in (%%~H) Do (Set "$%%~G[!i#!]=$%%i"&Set "$%%i=0"&Set /A "i#+=1"))) Else Set List="
rem ***  Usage: %List/D/V:$V=ListName%"VarA=Value" "VarB=Value" "VarC=Value"
 Set "List/D/V=Set "#$V=0"&Set "$$V="&For %%n in (1 2)Do if %%n==2 (For %%G in (!$V!)Do (Set "%%~G" > Nul &Set /A "#$V+=1"))Else Set $V="
::: -------------------------------------------------------------------||    END MACRO DEFINITIONS

::: -------------------------------------------------------------------||    Script specific varaiable Definition
REM *** required to be enabled PRIOR to macro Use, AFTER definition.
 Setlocal EnableDelayedExpansion
rem *** Define Initial script variables here.
rem ::: %DefArray% can be used to intialise array elements with a zero value: %DefArray%{ArrayName}{Element names as list}
rem ::: %List/D/V% can be used to define multiple variables as a list %List/D/V:$V=ListName%"VarA=Value" "$VarB=Value" "VarC=Value"
rem ::: %List/D/V% does not prefix variables with $V by default. Values you wish to save, prefix with $
::: -------------------------------------------------------------------||    Script Load / Return to last Location saved with %Loc:@=Label%
rem *** Loads Bat script containing Set "$Var=Value" output of %Save% and %Loc:@=Label% macros.
 IF Exist "%TEMP%\%~n0_!name!_save.bat" (
  Call "%TEMP%\%~n0_!name!_save.bat"
  Goto :!$Loc!
 )
:Start
 %Loc:@=Start%
::: -------------------------------------------------------------------||    Script Body
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • can you explain how it works? (without terminology, i'm new at this) – Matytoonist Sep 26 '20 at 20:40
  • The save macro uses a for /f loop to iterate over variables prefixed with the `$` character via the command `Set $`, splitting the output into variable name and value using the for loop options `tokens=1,2 Delims==`. The output of the For /F loop is redirected to a batch save file that can be called to load saved values. Positioning of the load test - `If exist ... Call ...` after Name selection and base variable assignment ensures script flow functions as intended. – T3RR0R Sep 26 '20 at 22:42
  • Labels are tracked and saved by expanding the Loc macro with the current labelname for labels you wish to use as 'Savepoints' – T3RR0R Sep 26 '20 at 23:19