0

I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:

:Setup
@echo off
title Dictator
color 0a

:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10

:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load

:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main


:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
    call saves\%pin%.cmd
    echo %pol%
    echo LOADED
    pause >nul
) else (
    echo INCORRECT PIN
    pause >nul
)
goto main
Vansh03
  • 47
  • 9

1 Answers1

0

At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%. In the end, your code should look like:

:Setup
setlocal enableextensions enabledelayedexpansion
@echo off
title Dictator
color 0a

:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10

:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load

:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main


:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
    call saves\!pin!.cmd
    echo %pol%
    echo LOADED
    pause >nul
) else (
    echo INCORRECT PIN
    pause >nul
)
goto main
Matthew Horvath
  • 186
  • 1
  • 12
  • Also, in your code, you misspelled 'Business Men' as 'Buissnes Men'. – Matthew Horvath Apr 11 '16 at 02:48
  • thanks for help with the typo ;P but the loading function in my game still doesn't seem to be working. I even pasted all of your code over mine and still don't have it working. The one ting that did make it work is when I changed %pol% to !pol!. Any suggestions to why this is the case or how to get around it? – Vansh03 Apr 11 '16 at 03:26
  • @MatthewHorvath you changed `%pin%` to `!pin!`, but that isn't neccessary. The only variable in this code, that needs delayed expansion is `pol` within the `:load` subroutine – Stephan Apr 11 '16 at 05:55