Save and Load .bat game — Error with single numbers

后端 未结 1 1826
走了就别回头了
走了就别回头了 2021-01-27 02:06

In this qustion, Save and Load .bat game I used Mat\'s answer.

But now I have a problem with \"saving\" numbers using said answer.

If the variable is not double

相关标签:
1条回答
  • 2021-01-27 02:29

    The problem is Mat's solution! For better understanding I repeat his solution

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

    In my opinion, it has multiple disadvantages.
    The @ prefix isn't necessaray.
    The redirection is repeated for each line (I don't like redundancy).
    It needs the spaces, as without spaces you got problems with numbers.

    Sample with items=1

    @echo set ITEMS=1>>savegame.cmd
    

    This results not in writeing set items=1 it writes set items= to 1>>savegame.cmd 1>> is the standard stream.

    You can solve all problems with

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

    The quotes are used to ensure that "hidden" spaces after the set are ignored.

    Btw. It's a bad idea to use a construct like if %raghave% = 00, (you need two equal signs), as 00 isn't a normal number you can't count or calculate with it, it's better to use 0 instead.

    Then also this should work

    set /a items=0
    set /a items=items+1
    set /a items=items-1
    if %items%==0 echo There are no items
    
    0 讨论(0)
提交回复
热议问题