.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SpyGuyTBM
Posts: 2
Joined: 04 Sep 2016 13:41

.

#1 Post by SpyGuyTBM » 04 Sep 2016 13:58

.
Last edited by SpyGuyTBM on 31 May 2022 15:55, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with "saving" technique!

#2 Post by foxidrive » 04 Sep 2016 17:34

In this section I added pause commands with a letter. This is meant to help you to debug your problem.

Use this in your game and see which point is being reached and you will find out which line is causing the problem.

Using double quotes in your compares like this example below protects the code from crashing when things like two words are encountered in a variable, and space characters which are typed somewhere by accident can also be debugged.

if "%L%"=="1" goto

Code: Select all

:SAVE_GAME_FILES
echo set username1=%username1%
echo set password1=%password1%
echo set Hair=%Hair%
echo set Face=%Face%
echo set E=%E%
echo set /a Power=%Power%
echo set /a Hp=%Hp%
echo set rival=%rival%
echo set /a rivalhp=%rivalhp%
echo set L=%L% 
echo A & pause
if %L%==1 goto MENU
echo B & pause
if %L%==2 goto folo
echo C & pause
if %L%==3 goto rolo
echo D & pause
if %L%==4 goto levelup1
echo E & pause
if %L%==5 goto 23
echo F & pause
:login

SpyGuyTBM
Posts: 2
Joined: 04 Sep 2016 13:41

.

#3 Post by SpyGuyTBM » 04 Sep 2016 19:15

.
Last edited by SpyGuyTBM on 31 May 2022 15:55, edited 1 time in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Help with "saving" technique!

#4 Post by elzooilogico » 05 Sep 2016 06:15

Hi SpyGuyTBM

First of all, as foxidrive has pointed out, you would better enclose string variables in quotes

Code: Select all

set "Hair=%Hair%"
for

Code: Select all

set Hair=%Hair%
may have spaces in the end that you can't see. Also

Code: Select all

set Hair = %Hair%
won't be what you expected, as it will produce a variable named Hair(space) that evaluates to (space)whichever Hair contents are.


You must understand how the flow of batch processing is. Batch files are executed top-down sequentially. When you want to transfer execution point to another part of the batch file, you must use

Code: Select all

call :label
or

Code: Select all

goto :label
Call jumps to the given label and returns when finds an exit /B or goto :eof command. Goto jumps to the given label and continues from that point. It doesn't return. So any series of commands between :label and exit /B is what we call a function or subroutine depending if it returns a value or not.


The problem you have is in the way you call the SAVE_GAME_FILES label

Code: Select all

call :SAVE_GAME_FILES >%username1%.bat
All output is redirected to the file as you expected, but you NEVER return, so when any goto is reached the flow of the batch jumps to another point of the file, but output is STILL REDIRECTED to the file.

change

Code: Select all

:SAVE_GAME_FILES
echo set username1=%username1%
echo set password1=%password1%
echo set Hair=%Hair%
echo set Face=%Face%
echo set E=%E%
echo set /a Power=%Power%
echo set /a Hp=%Hp%
echo set rival=%rival%
echo set /a rivalhp=%rivalhp%
echo set L=%L%
if %L%==1 goto MENU
if %L%==2 goto folo
if %L%==3 goto rolo
if %L%==4 goto levelup1
if %L%==5 goto 23

to

Code: Select all

:SAVE_GAME_FILES
echo set username1=%username1%
echo set password1=%password1%
echo set Hair=%Hair%
echo set Face=%Face%
echo set E=%E%
echo set /a Power=%Power%
echo set /a Hp=%Hp%
echo set rival=%rival%
echo set /a rivalhp=%rivalhp%
echo set L=%L%
exit/B

Now SAVE_GAME_FILES is a subroutine. When you call it, batch's flow jumps to the label and return when it hits the exit/B statement. All redirections use when calling the subroutine, end when returning from it.
The flow of the batch file will continue in the next line where the call was made, so another benefit is that you don't need the gotos anymore.

But take into account that if sequential flow of the batch enters any subroutine without being called, when the command processor reaches the exit/B, it will return to the caller or, if there isn't, will end the batch file itselt and return to wherever it was started.


So you may write batch files as a collection of functions, as i.e

Code: Select all

@echo off

:main
cls
echo This is the main part.

call :function_1

set /p "param1=Write a word and press ENTER: "
set /p "param2=Write another one and press ENTER: "

call: function_2 "%param1%", "%param2%"

if %ERRORLEVEL% EQU 0 echo %param1% and %param2% are equal.

exit/B & rem End of :main. This exit ends batch processing

:function_1
echo this is a function
echo.
echo We're going to compare to string variables
echo.
exit/B & rem return to caller

:function_2
echo this is another function
rem compare first and second parameters without quotes and case insensitive (no error checking)

if /I "%~1" EQU "%~2" (
  set/a retVal=0
) else (
  set/a retVal=1
)
exit/B %retVal% & rem return to caller (if any) setting ERRORLEVEL environment variable to retval content


All commands have its help, I suggest you use it as SETLOCAL /?, CALL /?, IF /? ...

Post Reply