Page 1 of 1

How to make a game saving system?

Posted: 03 May 2021 22:32
by Thug Scripting
Hi there, can any of you help me on how to make a game saving system in a .bat RPG game? I am new to this site so sorry if I'm breaking the rules. Thank you very much.

Re: How to make a game saving system?

Posted: 04 May 2021 06:11
by penpen
There are multiple ways to do that, just use the search function to look them all up.
You might for example use the following save/load functions (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
call :init
call :debug

call :save
set "value1=D"
set "value2=E"
set "value3=F"
call :debug

call :load
call :debug
goto :eof


:debug
set "value1"
set "value2"
set "value3"
echo(===
echo(
goto :eof

:init
	set "value1=A"
	set "value2=B"
	set "value3=C"
goto :eof

:save
>"game.sav" (
	set "value1"
	set "value2"
	set "value3"
)
goto :eof

:load
<"game.sav" (
	set /p "value1="
	set /p "value2="
	set /p "value3="
)
goto :eof
penpen

Re: How to make a game saving system?

Posted: 04 May 2021 12:39
by NicholasWhite
Oh you were looking for a way to save too, thanks for the answer, now I will try your scheme ...

Re: How to make a game saving system?

Posted: 04 May 2021 17:26
by T3RRY
Another method, which avoids having to modify save / load functions every time you add a variable to the script is to use a prefix unique to all variables you wish to save / recover.

Example:

Code: Select all

@Echo off

rem /* predefine your key variables prior to this loop */

:login load existing or create new
 Set /P "name=Enter your name: "
 If Not "%name%" == "" (
rem /* If character exists; load */
  If Exist "%~n0_%name%.bat" (
   Call "%~n0_%name%.bat"
rem /* Initialise character save using predefined variables if character new */
  ) Else ( Call :Save )
rem /* Force Assignment of name variable */
 ) Else ( Goto login )
===============================================
::: Script body

::: End of Script
Goto :Eof
===============================================
:Save function
 (For /F "Delims=" %%G in ('Set #')Do Echo/Set "%%G")>"%~n0_%name%.bat"
Exit /B 0