Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and Load .bat game

Tags:

batch-file

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".

like image 599
Gareth Jones Avatar asked Oct 06 '11 22:10

Gareth Jones


People also ask

How do I play a .BAT file?

Using a BAT file in Windows is as simple as double-clicking or double-tapping it. You don't need to download any special program or tool.

Can you make games with batch files?

Before deciding to make a shoot-em-up game or point-and-click game, know that even with MS DOS 8, batch script has serious limitations to its capabilities. You will not be able to create more of a game than a text-based game. It can be a quiz or a scenario game, but you will have only text.

Where do I save .bat files?

bat file from the command prompt regardless of the command prompt's current path, then you need to save the . bat file in a directory that is included in the PATH system variable (assuming you are using windows). 'a. bat' is not recognized as an internal or external command, operable program or batch file.


4 Answers

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.

like image 105
jeb Avatar answered Oct 09 '22 00:10

jeb


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.)

like image 6
Mat Avatar answered Oct 09 '22 01:10

Mat


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

like image 1
user1931470 Avatar answered Oct 09 '22 00:10

user1931470


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.)

like image 1
Technoguyfication Avatar answered Oct 09 '22 00:10

Technoguyfication