Save and Load .bat game

后端 未结 7 1597
予麋鹿
予麋鹿 2020-12-05 21:13

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 wa

相关标签:
7条回答
  • 2020-12-05 21:19

    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

    0 讨论(0)
  • 2020-12-05 21:20

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

    0 讨论(0)
  • 2020-12-05 21:20

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

    0 讨论(0)
  • 2020-12-05 21:23

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

    0 讨论(0)
  • 2020-12-05 21:24

    Create the file path for the save:

     `If not exist "%userprofile%\Saves" md "%userprofile%\saves"`
    

    Assign Variable to Folder file path:

    `set saveLoc="%userprofile%\saves"`
    

    Create blank File at target Location:

    `If not exist "%saveLoc%\data.sav" break>%saveLoc%\data.sav"`
    

    Assign a variable to your file path

    `set Gamesave="%saveLoc%\data.sav`
    

    For efficient coding... Block all your data into a single variable, prefixing with a character for delims.

    `set data=+%var1%+%var2%+%var3%+%var4%`
    

    THEN TO SAVE:

    `ECHO %data% >%Gamesave%`
    

    For efficient coding... Assign a variable to For /F loop components to bookend each token you wish to retrieve...

     `Set lo=For /f "usebackq tokens=`
        `Set ad= delims=+" %%a IN (%Gamesave%) DO SET`
    

    THEN TO LOAD:

    `%lo%1%ad% var1=%%a`
    `%lo%2%ad% var2=%%a`
    `%lo%3%ad% var3=%%a`
    `%lo%4%ad% var4=%%a`
    

    This approach saves an awful lot of typing.

    0 讨论(0)
  • 2020-12-05 21:25

    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.

    0 讨论(0)
提交回复
热议问题