1.

Solve : Save/load help?

Answer»

Ok I need some help with a test file to load data from a save state after checking if it even exist.
Here's the code:
Code: [Select]@echo off
title test save game batch
color 0f

set /a gold=0
set /a hp=100
set /a level=1
set /a potions=1
set /a food=1

:name
echo what is your first name?
set /p name=
goto StartScreen

:StartScreen
cls
echo Please chose an option:
echo.
echo 1. Load
echo 2. Change Varibles
echo 3. Check Varibles
echo 4. Save
echo 5. Exit
echo.
set /p pass=
if %pass%==1 goto load
if %pass%==2 goto ChangeVaribles
if %pass%==3 goto CheckVaribles
if %pass%==4 goto Save
if %pass%==5 exit
goto StartScreen

:load
cls
if exist ("tsgb%name%.txt") goto loader
echo No file exist for you.
pause >nul
goto StartScreen

:loader
for /f %%a in ("tsgb%name%.txt") do set %%a
echo Done. Please check stats to verify.
pause >nul
goto StartScreen

:ChangeVaribles
cls
echo what would you LIKE to change?
echo.
echo 1. Gold
echo 2. HP
echo 3. Level
echo 4. Potions
echo 5. Food
echo.
set /p change1=
echo what would you like to change it to?
set /p change2=
if %change1%==1 set gold=%change2%
if %change1%==1 set change1=gold
if %change1%==2 set hp=%change2%
if %change1%==2 set change1=HP
if %change1%==3 set level=%change2%
if %change1%==3 set change1=Level
if %change1%==4 set potions=%change2%
if %change1%==4 set change1=Potions
if %change1%==5 set food=%change2%
if %change1%==5 set change1=Food

echo.
echo.
echo Done. Changed %change1% to %change2%
goto StartScreen

:CheckVaribles
cls
echo Gold %gold%
echo HP %hp%
echo Level %level%
echo Potions %potions%
echo Food %food%
pause >nul
goto StartScreen

:save
cls
(echo gold=%gold%)> tsgb%name%.txt
(echo hp=%hp%)>> tsgb%name%.txt
(echo Level=%level%)>> tsgb%name%.txt
(echo Potions=%potions%)>> tsgb%name%.txt
(echo food=%food%)>> tsgb%name%.txt
echo Done. Saved to tsgb%name%.txt
pause >nul
goto StartScreen


Each time it changes everything as it should and SAVES to the correct file, but when I select load even after just SAVING it, i get the message: No file exist for you.

Any suggestions?
if exist "tsgb%name%.txt" goto loader instead of if exist ("tsgb%name%.txt") goto loader

If that doesn't help then check that the file actually exists in the current folder.QUOTE from: foxidrive on July 12, 2013, 06:03:13 PM

if exist "tsgb%name%.txt" goto loader instead of if exist ("tsgb%name%.txt") goto loader

If that doesn't help then check that the file actually exists in the current folder.

Ok that sort of worked. Now I get a whole new issue.
The output on the :loader screen now reads


Environment variable tsgbjasperjester.txt not defined.
Done. Please check stats to verify.


just as a note i tried removing the () around the

Code: [Select]for /f %%a in ("tsgb%name%.txt") do set %%a
but it just closed immediately.

thank you again for the helpCode: [Select]for /f "usebackq delims=" %%a in ("tsgb%name%.txt") do set myvar=%%a
If that text file just has one line of text in it and you are trying to assign it to a variable then just do this.
Code: [Select]set /p myvar=<tsgb%name%.txtQuote from: Squashman on July 12, 2013, 08:15:29 PM
Code: [Select]for /f "usebackq delims=" %%a in ("tsgb%name%.txt") do set myvar=%%a
If that text file just has one line of text in it and you are trying to assign it to a variable then just do this.
Code: [Select]set /p myvar=<tsgb%name%.txt

Uh, I'm not sure what that was supposed to do but it did no load the changed variables at all. I am actually trying to change 5 in this test game, but there 12 variables in the full game.Quote from: Jasperjester on July 12, 2013, 08:10:43 PM
The output on the :loader screen now reads


Environment variable tsgbjasperjester.txt not defined.
Done. Please check stats to verify.


just as a note i tried removing the () around the

Code: [Select]for /f %%a in ("tsgb%name%.txt") do set %%a
but it just closed immediately.

Yes, that is a different command and has different syntax and it should be:


Code: [Select]for /f "delims=" %%a in ('type "tsgb%name%.txt" ') do set variable=%%a
but this will only populate the variable to the last line in the file

Quote from: foxidrive on July 13, 2013, 04:07:03 AM
but this will only populate the variable to the last line in the file

ok but if one player has 7 stats to track and each player gets up to 3 companions each will ever changing stats that have to be saved and loaded, this will require me to enter the same line at least 8 times for each player totaling 36 times and creating 36 small files for each player stats alone not including the money and inventory systems I have in place. I need all this in a maximum of 6 pages. There are already 4 people here who are testers and react differently and that would seriously bloat the folder in which the files are saved.1. Writevals.bat

@echo off
REM save values
set name=Joe
set money=1000
set energy=50
set friend1=Mary
set friend2=Jim
set friend3=Peter
if exist "%name%.values" del "%name%.values"
echo money=%money% >> "%name%.values"
echo energy=%energy% >> "%name%.values"
echo friend1=%friend1% >> "%name%.values"
echo friend2=%friend2% >> "%name%.values"
echo friend3=%friend3% >> "%name%.values"

2. Joe.values

money=1000
energy=50
friend1=Mary
friend2=Jim
friend3=Peter

3. Readvals.bat

@echo off
REM read values
set name=Joe
set money=
set energy=
set friend1=
set friend2=
set friend3=
for /f "delims=" %%A in ('type "%name%.values"') do (
set %%A
)
echo name=%name%
echo energy=%energy%
echo friend1=%friend1%
echo friend2=%friend2%
echo friend3=%friend3%

4. Output of Readvals.bat

name=Joe
money=1000
energy=50
friend1=Mary
friend2=Jim
friend3=PeterQuote from: Jasperjester on July 14, 2013, 01:18:43 AM
ok but if one player has 7 stats to track and each player gets up to 3 companions each will ever changing stats that have to be saved and loaded

I was pointing out that your syntax was wrong and it would not read a file at all.Quote from: foxidrive on July 14, 2013, 05:05:37 AM
I was pointing out that your syntax was wrong and it would not read a file at all.

Oh, I apologize for the misunderstanding. I hope you may forgive my ignorance on the matter. I am still learning

Quote from: Salmon Trout on July 14, 2013, 02:49:42 AM
for /f "delims=" %%A in ('type "%name%.values"') do (
set %%A
)

Had an issue where it wouldn't read the files at first, but it turned out, I forgot the tsbg before the %name%.values

Kudos for all the help!!!!!!!!!!!!!What is a 'tsbg'?
Quote from: Salmon Trout on July 14, 2013, 07:05:51 AM
What is a 'tsbg'?

tsbg was a prefix I added to make sure i never mixed up the test save batch game with the real game save files.
example:
Code: [Select]tsbg%name%.values

vs

Code: [Select]realgame%name%.values


Discussion

No Comment Found