[英]How do I save progress in batch
I am making an interactive fiction game using windows batch script. 我正在使用Windows批处理脚本制作交互式小说游戏。 I sorta figured out how to do it, but I can't for the life of me figure out why it wont work.
我有点想出了怎么做,但我一生都无法弄清楚为什么它行不通。
This is the loader: 这是加载程序:
@echo off
cls
:load
set /p s_name=Name:
if not exist "savedata/%s_name%/" goto user_error
set /p %name%= <savedata/%s_name%/name.pok
set /p %money%= < savedata/%s_name%/money.pok
set /p %area%= < savedata/%s_name%/location.pok
echo Name: %name%
echo Money: %money%
echo Location: Area %area%
echo.
pause
exit
:user_error
cls
echo.
echo File Not Found.
echo.
echo.
pause
exit
and this is the save file creator: 这是保存文件的创建者:
@echo off
cls
set /p lnm=Your Name:
set m=700
set l=27
md "savedata/%lnm%"
echo %lnm% > savedata/%lnm%/name.pok
echo %m% > savedata/%lnm%/money.pok
echo %l% > savedata/%lnm%/location.pok
echo save complete.
pause
Mind you, these are just tests, but you get the point. 提醒您,这些只是测试,但您明白了。
Necessary changes: 必要的更改:
set /p %name%=
(and so on) command; set /p %name%=
(依此类推)命令; >
and <
redirectors (as well as with =
assignments in set
commands). >
和<
重定向程序(以及set
命令中的=
赋值)时,不得包含周围,前导或尾随空格。 The loader: 装载机:
set /p name=<savedata/%s_name%/name.pok
set /p money=<savedata/%s_name%/money.pok
set /p area=<savedata/%s_name%/location.pok
echo Name: %name%, echo Money: %money%, echo Location: Area %area%
The save file creator: 保存文件创建者:
md "savedata/%lnm%" 2>nul
echo %lnm%>savedata/%lnm%/name.pok
echo %m%>savedata/%lnm%/money.pok
echo %l%>savedata/%lnm%/location.pok
echo save complete.
echo Name: %lnm%, echo Money: %m%, echo Location: Area %l%
Output (without some echo
s and cls
s): 输出(没有一些
echo
S和cls
S):
==>29036918load
Name:nn
File Not Found.
Press any key to continue . . .
==>29036918save
Your Name:nn
save complete.
Name: nn, echo Money: 700, echo Location: Area 27
Press any key to continue . . .
==>29036918load
Name:nn
Name: nn, echo Money: 700, echo Location: Area 27
Press any key to continue . . .
==>
What I would recommend would be to reserve one character or string as a prefix for the variables you want to save/reload. 我建议将保留一个字符或字符串作为要保存/重新加载的变量的前缀。 This way, your save routine and reload routine are vastly simplified.
这样,您的保存例程和重新加载例程就大大简化了。
For instance, choose $
as a prefix reserved for save/reload variables, then 例如,选择
$
作为保留用于保存/重新加载变量的前缀,然后
set $>filename
is your save routine (every variable starting $
will be saved in filename
as individual lines in varname=value
format) for instance 是您的保存例程(例如,每个以
$
开头的变量将以varname=value
格式存储为filename
作为单独的行)
$money=700
And your reload routine is simply 而且您的重新加载例程非常简单
for /f "delims=" %%a in (filename) do set "%%a"
which then reloads the variables from the file. 然后从文件中重新加载变量。
This way, you don't need to change anything when you want to save another variable - so long as the variablename starts $
, it will be saved when the save line is executed and reloaded by the reload line described. 这样,当您要保存另一个变量时,您无需更改任何内容-只要变量名以
$
开头,它将在执行保存行并由所述的重新加载行重新加载时保存。
Meanwhile, please note that /
introduces switches in cmd
and \\
is the directory-separator. 同时,请注意
/
在cmd
引入了开关, \\
是目录分隔符。 "quote strings that may contain spaces" if they are used as filenames - so "%filename%" will access the file Fred Bloggs
if the variable filename
contains the value Fred Bloggs
“引号字符串可能含有空格”,如果它们被用作文件名-所以“%文件名%”将访问文件
Fred Bloggs
如果变量filename
包含值Fred Bloggs
Here's an example: 这是一个例子:
@ECHO OFF
SETLOCAL
:: initial setup
SET $money=700
SET $location=27
SET $lnm=Fred Bloggs
:: (naturally, you could input LNM with a set/p if you wanted to)
:: Save the values in a file
SET $>"U:\%lnm%.pok"
:: Display values - I
echo Name: %$lnm%
echo Money: %$money%
echo Location: Area %$location%
:: Display values - II (for debugging while programming)
SET $
ECHO ============================================
ECHO Now CHANGE values
SET $money=50
SET $location=99
:: Display values - II (for debugging while programming)
SET $
ECHO ============================================
ECHO Now RELOAD values
for /f "usebackqdelims=" %%a in ("U:\%lnm%.pok") do set "%%a"
:: Display values - II (for debugging while programming)
SET $
GOTO :eof
Note that I'm creating a file containing all of the $
variables (on my U:
drive for convenience) 请注意,我正在创建一个包含所有
$
变量的文件(为方便起见,在我的U:
驱动器上)
The usebackq
option is required because the filename is quoted. usebackq
选项是必需的,因为文件名已加引号。
Note how the original values are restored from the file. 请注意如何从文件中还原原始值。 If you
type
the file, it will show lines in an obvious manner. 如果
type
文件,它将以明显的方式显示行。
The key to this method is to use a prefix ( $
) to the variables-of-interest. 该方法的关键是对感兴趣的变量使用前缀(
$
)。
I have created a game similar to this and figured I would share my solution. 我创建了一个与此类似的游戏,并认为我会分享自己的解决方案。 What I did was I had my game as a batch command and I had each player in a seperate folder named "players", to save the progress I would write the batch command to call when starting back up.
我所做的是将游戏作为批处理命令,并且将每个玩家都放在一个名为“ players”的单独文件夹中,为保存进度,我将编写批处理命令以在启动备份时调用。 For example, if I only had the location variable, and money, whenever I would want to have the script save the game such as visiting a town or whatever you want, I would call a function like this each time I wanted to save:
例如,如果我只有位置变量和金钱,那么每当我想要让脚本保存游戏(例如访问城镇或您想要的任何东西)时,每次要保存时都会调用这样的函数:
call :SAVE_GAME_DATA
:SAVE_GAME_DATA
echo(
set location=%location%
set money=%money%
)>.\Players\%name%.bat
then every time you would run the script it would ask for your name and load up the batch file it wrote for your name. 然后,每次您运行脚本时,脚本都会要求您输入名称,并加载为该名称编写的批处理文件。 Like so:
像这样:
:startup
echo Welcome to whatever game it is!
echo.
set /p name=Please enter your name:
call .\Players\%name%.bat
::rest of your code goes here
Of course, you would have to have logic of that if .\\Players\\%name%.bat does not exist then it would run the user through a character creation process and save default variables such as default location and set money=0 for example, then call the save_game_data subroutine to save the default values for the new character. 当然,您必须具有以下逻辑:如果。\\ Players \\%name%.bat不存在,那么它将通过字符创建过程使用户运行并保存默认变量(例如默认位置)并设置money = 0 ,然后调用save_game_data子例程以保存新角色的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.