开发者

Save and Load .bat game -- Error with single numbers

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 digits (for example 1 or 0) it will "save" the variable as " " and thus will crash the game whenever you do anything that needs that variable. The game sets the variable fine before that.

For example if I pick up the rag, then type Inv, it will say I'm holding the rag. If I then save and load again, then type Inv, it wont say I'm holding anything!

It also won't echo "Nothing" which it should do if %raghave% = 00

(I also have the save file open in Notepad++ and so can see that set RagHave=)

(Also if I use Mat's code with the spaces, then the variable is set as "set RagHave=1" an开发者_StackOverflow中文版d so adds a space at the end)


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜