I'm currently working on a casual game in batch, and I'm running into a slight issue when recovering save data.
All the save data is stored in custom INI files (idea from fourth answer here, other code from first answer here). The INI file is first created when the user inputs the name for the save, and said name is set as a variable called savename. The INI file is made when a template, sdata.text, is copied over through type sdata.txt > saves\%savename%.ini
.
The variable loadsave, of which is used when a user is loading a previous save file, is then set equal to savename (set loadsave=%savename%
). The file then calls a separate file called lsdata.bat, and lsdata.bat takes values from the INI file and assigns them as variables for use in the game.
An example of the INI file would be:
[GameData]
lastfile=expo
lastsection=INTROGAME
nextsection=SECTWO
[PlayerData]
held=none
name=none
lname=none`
and lsdata.bat looks like
for /f "delims=" %%a in ('call get.bat saves\%loadsave%.ini GameData lastfile') do (
set lastfile=%%a
echo setting var lastfile...
)
for /f "delims=" %%a in ('call get.bat saves\%loadsave%.ini GameData lastsection') do (
set lastsection=%%a
echo setting var lastsection...
)
for /f "delims=" %%a in ('call get.bat saves\%loadsave%.ini GameData nextsection') do (
set nextsection=%%a
echo setting var nextsection...
)
Et cetera. The file get.bat is the exact same as ini.bat from the second link I mentioned up there.
The problem arises when recalling a modified save file. When creating a new save, lsdata.bat is called and everything runs smoothly (I can tell from the echoed lines of text that help me confirm the FOR loop was completed). However, when using a modified save file where all the values are not the defaulted ones, all of the FOR loops are skipped and the file skips to an ECHO and a CECHO I have at the bottom.
If the save file is created and exited before the game has a chance to overwrite some values, everything is recovered as it should. If even one value has been changed, though, it skips all the FOR loops and the variables are all equal to NUL. When the game first loads, all variables are set to NUL for testing purposes, and then can be reassigned values when a new save is created or an old one is loaded.
Delayed expansion is on, and if you try to call variables (with ! or %), it still shows them set equal to NUL.
How can I prevent the modified files from causing this weird bug?