如何从批处理文件中保存变量,然后加载它们以使用它们? [英] How do I save variables from a batch file then load them to use them?

查看:90
本文介绍了如何从批处理文件中保存变量,然后加载它们以使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个经验不足的编码员,我喜欢批处理,因为它是一种更直接的编码语言,但是我想将多个变量保存到txt文件或其他格式,然后将那些相同的变量加载"到批处理文件中,键入,以便玩家可以看到,但我可以使用它

I'm a pretty inexperienced coder and I love batch because its one of the more straight forward coding languages but I want to save multiple variables to a txt file or other format then "load" those same variables to the batch file not to type so the player can see but so that I can use it

这是我到目前为止要加载的内容

This is what I have so far for loading:

:nick
cls
color 02
set /p nick= Enter your nickname: 
if exist ((savegame%nick%.txt) goto load)
goto instructions

:load
for /f "blevel=" %%a in (savegame%nick%.txt) do set blevel=%%a&goto loadexist
for /f "mlevel=" %%a in (savegame%nick%.txt) do set mlevel=%%a&goto loadexist
for /f "alevel=" %%a in (savegame%nick%.txt) do set alevel=%%a&goto loadexist

这就是我要保存的内容:

this is what I have for saving:

:save
cls
echo Saving please wait...
(echo blevel=%blevel%)> savegame%nick%.txt
(echo mlevel=%mlevel%)>> savegame%nick%.txt
(echo alevel=%alevel%)>> savegame%nick%.txt
timeout /t 5 /nobreak >nul
exit

就像我说的那样,我非常缺乏经验,我的目标是创建一款游戏,请随时指出任何缺陷.在我的游戏中,我有3个角色,布鲁图斯,梅迪纳和阿喀琉斯. blevelmlevelalevel指的是您在游戏中所处的位置以及您选择扮演的角色,因此您不必为了整个游戏而完全玩.

Like I said I'm very inexperienced and my goal is to create a game please feel free to point out any flaws. In my game I have 3 characters Brutus, Mediana, and Achilles. blevel, mlevel, and alevel refers to the place in the game where you are and the character you chose to play as so you don't have to play the whole game to get to where you were.

因为这显然仍在进行中,所以我只有一个可以保存的实例,并且它在第一个Brutus promt中保存:

As this is obviously still a work in progress I only have one instance where you can save and its in the first Brutus promt:

:brutus 1
set blevel=1
cls
echo.
echo.
echo.
echo You chose brutus good choice but are you sure this is who you want?
echo 1) go back and choose 
echo 2) continue
echo 0) save
set /p c=C:\

if "%c%" == 1 goto choose character
if "%c%" == 2 goto brutus 2
if "%c%" == 0 goto save
goto brutus 1

因此,当您选择这样的选项时,各自的级别会上升.每次我重新打开游戏时,键入我用来保存文件的昵称时,cmd都将关闭,或者它不起作用,并像文件或其中的变量不存在那样跳过它.我的意图是在每个关键的选择上都使用这三个字符,所以最后我可能每个字符至少要有100个.

So when you get to a choice like this your respective level goes up. Every time I reopen the game it either closes cmd when I type the nickname I used to save the file or it doesn't work and skips over it like the file or the variables in it don't exist. My intention is to do this with all three characters at every crucial choice so in the end I will probably have at least 100 per character.

推荐答案

您的保存"逻辑是可以的.

your "save" logic is ok.

要加载,您只需要一行:

For loading, you need just a single line:

for /f "delims=" %%i in (savegame%nick%.txt) do set "%%i"

使用echo on运行它时,您会看到它处理每一行并设置变量.

When you run it with echo on, you see that it processes every line and sets your variables.

这篇关于如何从批处理文件中保存变量,然后加载它们以使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆