开发者

Save and Load .bat game

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 i开发者_运维百科tem 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".


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.


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


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


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


I've once written a batch game (text-adventure like) back in DOS 5 era. I've then chosen to make each location in the game a separate folder on disk. Each location had its own bat file to initialize the location, like showing its description and to initialize which locations could be moved to from there.

Objects and persons had their own bat file. So when talking to a person, the bat file of that person needed to handle that, and save conversation info. Picking up an object was as simple as moving object.bat from the location directory to the inventory directory.

That way, not only could I easily add locations, objects and persons to the game, but also most of the information was saved automatically, because the status was kept inside files, or in the location of files. The rest was saved by saving relevant values to a batch file that was run on load, in a similar way as Mat described in his answer.

I must say, I didn't know then of all the for loop trickery and string manipulation possibilities, if they even were possible MSDOS 5, otherwise it could have been a little easier, not to mention faster. ;)


Try this:

 (echo items=%items%) >> gamesave.txt
 (echo money=%money%) >> gamesave.txt

etc.


you can try this :

make a variable

set /p savegame=

then make this code

echo %savegame% > save.txt

then you can open the file,type anything and it will save as save.txt ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜