将变量设置为批处理中的文件名

时间:2014-07-02 19:16:04

标签: variables batch-file detection

所以我正在做一个小模拟器游戏,我需要系统识别以前保存到该帐户的健康和耐力。

例如,如果一个玩家以四个健康状态退出游戏,当他们重新加载他们的保存时,他们的健康状况仍然是4.我已经玩弄了CMD提示和我的代码一点点,我只是似乎无法找到办法。有什么帮助吗?

感谢。

2 个答案:

答案 0 :(得分:0)

大声笑尝试使用>>和<的东西。它们生成.txt文件,然后可以通过批处理文件Watch:

读取
@echo off
if %health% EQU 10 (
echo 10 >> health.txt
)
if %health% EQU 9 (
echo 9 >> health.txt
)
if %health% EQU 8 (
echo 8 >> health.txt
)
if %health% EQU 7 (
echo 7 >> health.txt
)
if %health% EQU 6 (
echo 6 >> health.txt
)
if %health% EQU 5 (
echo 5 >> health.txt
)
if %health% EQU 4 (
echo 4 >> health.txt
)
if %health% EQU 3 (
echo 3 >> health.txt
)
if %health% EQU 2 (
echo 2 >> health.txt
)
if %health% EQU 1 (
echo 1 >> health.txt
)

这将使批处理文件将健康状况存储在名为health.txt的文本文件中。现在它需要阅读它。我们将用set来做到这一点。

@echo off
if %health% EQU 10 (
echo 10 >> health.txt
)
if %health% EQU 9 (
echo 9 >> health.txt
)
if %health% EQU 8 (
echo 8 >> health.txt
)
if %health% EQU 7 (
echo 7 >> health.txt
)
if %health% EQU 6 (
echo 6 >> health.txt
)
if %health% EQU 5 (
echo 5 >> health.txt
)
if %health% EQU 4 (
echo 4 >> health.txt
)
if %health% EQU 3 (
echo 3 >> health.txt
)
if %health% EQU 2 (
echo 2 >> health.txt
)
if %health% EQU 1 (
echo 1 >> health.txt
)
set health=>health.txt

基本上它的作用是创建一个文件health.txt,然后读取并将其命名为变量%health%。 (假设%health%是你用来存储这个人健康的东西。 如果您提供更多代码,我可以帮助您更多。我会更好理解。 简而言之,echo>> health.txt生成health.txt,并设置health =

答案 1 :(得分:0)

简单的方法是为要保存的所有变量保留前缀。例如,使用$作为所有变量保存的前缀。

现在:假设您在某个模糊的变量中有玩家的名字,例如playername

为示例设置一些随机值:

set /a $health=7
set /a $stamina=5
set "$somethingelseofinterest=+1 Greatsword of dragon-slaying"
set "playername=Fred"

然后保存所需的设置,您只需要

set $ >"%playername%.game"

将所有$变量保存到名为“Fred.game”

的文件中

要重新加载感兴趣的变量,

if exist "%playername%.game" (
 for /f "delims=" %%a in ('type "%playername%.game" ') do set %%a
) else (
 rem in here you could set defaults for a new player
)
相关问题