How to make a game saving system?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Thug Scripting
Posts: 1
Joined: 03 May 2021 08:11

How to make a game saving system?

#1 Post by Thug Scripting » 03 May 2021 22:32

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to make a game saving system?

#2 Post by penpen » 04 May 2021 06:11

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

NicholasWhite
Posts: 1
Joined: 04 May 2021 12:35

Re: How to make a game saving system?

#3 Post by NicholasWhite » 04 May 2021 12:39

Oh you were looking for a way to save too, thanks for the answer, now I will try your scheme ...

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: How to make a game saving system?

#4 Post by T3RRY » 04 May 2021 17:26

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

Post Reply