Save and Load .bat game

17,772

Solution 1

Try something like this:

@echo @ECHO OFF           > savegame.cmd
@echo SET ITEMS=%ITEMS%   >> savegame.cmd
@echo SET HEALTH=%HEALTH% >> savegame.cmd
@echo SET MONEY=%MONEY%   >> savegame.cmd

will "save" those three variables in savegame.cmd. Then you can call that file to reload the variables.

(Doing it with a for /f is quite a bit trickier.)

Solution 2

You could also save/load with only values, like

(
  echo %highscore%
  echo %playername%
  echo %points%
) > savegame.sav

and load them with

< savegame.sav (
  set /p highscore=
  set /p playername=
  set /p points=
)

The first part simply redirects the echo outputs to a file.
The loading part using also file redirection, but in this case as input source.
set /p commands in a block can read consecutively lines from the file.

Solution 3

Or


@echo off
> savegame.bat echo.SET ITEMS=%ITEMS%
>> savegame.bat echo.SET HEALTH=%HEALTH%
>> savegame.bat echo.SET MONEY=%MONEY%
>> savegame.bat echo.SET LEVEL=%LEVEL%


The Words in Bold Are The Variables to be Written on the bat file.

The "savegame.bat" is where you will put the file's name that the Variables will be saved to.

And lastly the > Arrow mean that it will delete everything and start from the beginning of the file as of the >> arrow is telling it to add that line to the very end of all the writing at the very bottom of the file.

I Hope This Helps, as-well as Help anyone else who has the same question :D

Solution 4

Here's one that I am actually working on currently.

You use INI files for the savegames, and load them with a dir command.

This is how I went about loading a list of savegames in. The "." delimiter is so that it will not show the .ini extension, as not to confuse users. This however does not allow users to put periods in the savegame names.

set randomDirIndex=%random%dirindex
dir /b savegames\>%temp%\%randomDirIndex%
for /f "delims=." %%a in (%temp%\%randomDirIndex%) do (
    echo %%a
)

Here's an example of the savegame INI file:

[PlayerStats]
health=100
energy=75
mana=50
[Inventory]
sword=1
key=0
[Info]
name=John Doe

I also used this question (check the first answer) to get the load INI script.

Now to load it you would use a series of for /f commands:

for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats health') do (
    set health=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats energy') do (
    set energy=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini PlayerStats mana') do (
    set mana=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory sword') do (
    set hasSword=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Inventory key') do (
    set hasKey=%%a
)
for /f "delims=" %%a in ('call ini.bat savegames\%~1.ini Info name') do (
    set playerName=%%a
)

And finally to save the game you just have to echo all of the things into a file, essentially rewriting the INI file. (refer to other people's answers.)

Share:
17,772

Related videos on Youtube

Gareth Jones
Author by

Gareth Jones

Updated on June 24, 2022

Comments

  • Gareth Jones
    Gareth Jones almost 2 years

    I'm making a text game written in bat, and the game has been done, (or more, a good part of it, such as the commands, and at the stage where you can play it); however, I want to add the power to save your game, and load it again.

    I think one could do this by having the .bat file write the variables which need to be saved (such as the item variables); however, I don't know how to do this. Any help would be appreciated, thanks.

    I should have said, I can make it load, by use of:

     for /f "delims=" %%x in (config.txt) do (set "%%x")
    

    However, I don't know how to make the .bat write to the file and so "save".

    • Ell
      Ell over 12 years
      I haven't used Windows In a very long time, but cant you pipe? e.g. echo "mysavedata" >> save.dat