Page 1 of 1

Problem loading variable from file

Posted: 18 Feb 2021 07:37
by RobinTendo
So I'm making a batch game, and wanted to be able to save progress. After some searching this is what I came up with, although it might be completely wrong.

Save script:

Code: Select all

:save
cls
echo %money%>money.sgf
echo %story%>story.sgf
echo %name%>name.sgf
goto gamemenu
Load script:

Code: Select all

:loadgame
cls
set /p money=<money.sgf
set /p name=<name.sgf
set /p story=<story.sgf
goto gamemenu
When I go to :loadgame, the file just closes. Does anyone know what I did wrong?
(probably everything tbh)

Re: Problem loading variable from file

Posted: 18 Feb 2021 10:30
by T3RRY
The Set command can be used to output all variables with a given prefix, allowing easy redirection of the values to a file, and by using a for /f loop to split the variables from the values they can be output to a cmd or bat file prefixed with the set command to all said file to be called when you wish to load the values.

An example of a simple login / character account script using such a method:

Code: Select all

@Echo off
:login
rem /* clear any preexisting definitions */
 For /F "tokens=1,2* Delims==" %%G in ('Set "#"')Do Set "%%G=" 2> nul
rem /* example definitions */
 Set /P "#name=Enter Name: "
 Set /P "#Pass=Enter Password: "
rem /* transfer entered value to non prefixed value to preserve for comparison */
 Set "Verify=%#pass%"
 If Not Exist "%TEMP%\%~n0_%#Name%_Save.cmd" (
  Set /P "#location=Enter location: "
  Set /P "#destination=Enter destination: "
 )Else (
rem /* load saved values */
  Call "%TEMP%\%~n0_%#Name%_Save.cmd"
 )
 If /I not "%Verify%" == "%#Pass%" (
  Echo(Invalid Password
  Goto :Login
 )
 (For /F "tokens=1,2* Delims==" %%G in ('Set "#"')Do Echo(Set "%%G=%%H") >"%TEMP%\%~n0_%#Name%_Save.cmd"
 Set #

Re: Problem loading variable from file

Posted: 19 Feb 2021 01:44
by RobinTendo
Hi, thanks for the post. But I'm kind of new to batch programming, and I dont really know how this works or how to implement it in my own script. Could you explain that a little further please? Thanks :D

Re: Problem loading variable from file

Posted: 19 Feb 2021 05:52
by Eureka!

Code: Select all

@echo off
setlocal
pushd "%~dp0"


set money=12
set story=interesting
set name=John Doe


:save
(
  set money
  set story
  set name
) > settings.ini


:load
for /f "usebackq delims=" %%x in (.\settings.ini) do set "%%x"

This has a disadvantage, however:
set name will show you all variables that start with name (try it with set PATH; it will show you PATH and PATHEXT).
So choose your variable names wisely :) On the other hand: it might load extra variables, but you can just ignore them.

It also has an advantage:
If you start all the variables that you want to save with a unique prefix, let's say $_money, etc, you can save them all at once with:

Code: Select all

:save
set $_ > settings.ini

Re: Problem loading variable from file

Posted: 19 Feb 2021 07:31
by penpen
RobinTendo wrote:
18 Feb 2021 07:37
When I go to :loadgame, the file just closes. Does anyone know what I did wrong?
(probably everything tbh)
At least in the code you posted, there is nothing wrong.
But you might improve your code in various ways:
- Start a command with a redirection to protect your code from adding spaces at the end of redirected text.
- Use doublequotes around filenames to avoid errors with filenames that contain spaces.
- Use doublequotes around set parameters ('set "a=1"' instead ' set a=1').
- Encapsulate functionality into functions and call them (instead of using a single giant function), which improves readability of your code (which helps you to keep track of your program flow)
- Avoid using unneccessary files, so you should use only one savegame file (for each single savegame).

Eureka! wrote:
19 Feb 2021 05:52
So choose your variable names wisely :)
Better use safe code and avoid using code that can go wrong.


The easiest way should be to use batch files for savegames; sth like the following (untested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "money=100"
set "story=interesting"
set "name=default"
for %%a in ("money" "story" "name") do echo("%%~a=!%%~a!"
echo(===
set "savegame=savegame1.bat"

call :save savegame
for %%a in ("money" "story" "name") do set "%%~a=sth different"
for %%a in ("money" "story" "name") do echo("%%~a=!%%~a!"
echo(===
call :loadgame savegame
for %%a in ("money" "story" "name") do echo("%%~a=!%%~a!"
goto :eof

:save
>"!%~1!" (
	for %%a in ("money" "story" "name") do echo(set "%%~a=!%%~a!"
)
goto :eof

:loadgame
call "!%~1!"
goto :eof

penpen

Re: Problem loading variable from file

Posted: 19 Feb 2021 11:43
by RobinTendo
Eureka! wrote:
19 Feb 2021 05:52
:save
(
set money
set story
set name
) > settings.ini

:load
for /f "usebackq delims=" %%x in (.\settings.ini) do set "%%x"
I tried this method, and the saving part works, but when loading my file crashes. This is my code:

Code: Select all

:load
for /f "usebackq delims=" %%x in (savegame.sav) do set "%%x"
cls
echo Game Loaded!
pause>nul
cls
goto startmenu
Does anyone know what I'm doing wrong?

Oh also, just saw the how to get help quick post, so here's my info.bat output:

Code: Select all

 INFO.BAT version 1.5
--------------------------------------------------------------------------------
Windows version        :  Microsoft Windows [Version 10.0.17763.737]
Product name           :  Windows 10 Pro Education, 64 bit
Performance indicators :  Processor Cores: 4      Visible RAM: 4062992 kilobytes

Date/Time format       :  (dd/mm/yy)  vr 19-02-2021  19:29:14,99
__APPDIR__             :  C:\windows\system32\
ComSpec                :  C:\windows\system32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Disabled
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  nl-NL       Code Pages: OEM  850    ANSI 1252
DIR  format            :  18-02-2021  13:48     8.053.063.680 pagefile.sys
Permissions            :  Elevated Admin=No, Admin group=Yes

                          Missing from the tool collection:  debug

Re: Problem loading variable from file

Posted: 19 Feb 2021 14:07
by ShadowThief
Run the script from the command prompt instead of double-clicking it so that the window stays open so that you can see the error that you get.