Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#1
Post
by j0y » 10 Oct 2015 08:28
Hi. I am trying to make a small command line puzzle game and I am trying to put a load feature on it. Here is my code for it:
Code: Select all
:load
if not exist save.sav goto loaderror
< save.sav (
set /p name=
set /p level=
set /p health=
set /p money=
set /p exp=
set /p expmax=
set /p str=
set /p def=
set /p wep=
set /p wepdis=
set /p arm=
set /p armdis=
set /p hpots=
)
cls
echo Game loaded!
pause >nul
goto main
However it just says: set was not expected at this time, when it "succesfully loads" the game!
How do I fix this?
Please I need help quick because this a homework task and it's due on monday 12/10/15!
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#2
Post
by penpen » 10 Oct 2015 09:13
Your posted code sniplet is error free.
penpen
-
TheHunterManX
- Posts: 54
- Joined: 14 Aug 2015 05:59
#3
Post
by TheHunterManX » 10 Oct 2015 09:20
Here is some help (change numbers to how you wish):
Code: Select all
@echo off
:main
call :Load
REM Make stuff here!
pause >nul
exit /b 0
:Save
(
echo set name=%name%
echo set level=%level%
echo set health=%health%
echo set money=%money%
echo set exp=%exp%
echo set expmax=%expmax%
echo set str=%str%
echo set def=%def%
echo set wep=%wep%
echo set wepdis=%wepdis%
echo set arm=%arm%
echo set armdis=%armdis%
echo set hpots=%hpots%) >Save.bat
goto main
:Load
if exist Save.bat (
goto Loading) else (
goto new)
:Loading
call Save.bat
echo Game loaded!
pause >nul
goto :EOF
:new
set /p name=What is your name? Type here:
set level=1
set health=30
set money=300
set exp=0
set expmax=100
set str=2
set def=3
set wep=0
set wepdis=0
set arm=0
set armdis=0
set hpots=0
goto Save
Last edited by
TheHunterManX on 10 Oct 2015 09:26, edited 1 time in total.
-
TheHunterManX
- Posts: 54
- Joined: 14 Aug 2015 05:59
#4
Post
by TheHunterManX » 10 Oct 2015 09:25
And btw your code is wrong as it has saving when it is loading and the set /p means to type somthing to set that variable to that somthing you wrote.
Also you should use call and change save.sav to save.bat a you can use:
When loading. Also it should have saving like this:
so that the data that has been saved in a file.
EDIT: Mistakes have been made on my behalf, and I am sorry for saying it was wrong, as I have only used this code as it is easier to me.
Last edited by
TheHunterManX on 10 Oct 2015 09:49, edited 1 time in total.
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#5
Post
by penpen » 10 Oct 2015 09:46
TheHunterManX wrote:And btw your code is wrong as it has saving when it is loading and the set /p means to type somthing to set that variable to that somthing you wrote.
You are wrong: The above code is loading data from a file called "save.sav"
This is done by redirecting the STDIN (standard input stream) to a file input stream connected to "save.sav".
So the "set/P" does not read from the shell, but from file, you may test it using this sample "save.sav":
Code: Select all
name_sample
level_sample
health_sample
money_sample
exp_sample
expmax_sample
str_sample
def_sample
wep_sample
wepdis_sample
arm_sample
armdis_sample
hpots_sample
In addition:
The load-save-implementation may not be replaceable (-> "this is a homework task"), and
there is no real need to change the load-save-implementation, as the loading part is working error free and the save-part is not needed (although i assume there is a working save-part in the homework task, but not contained in the posted code snippet).
penpen
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 10 Oct 2015 11:36
HunterMan,
Executing a second batch file just to load the variables is a decent solution but you could save yourself the overhead by doing it differently.
When saving to the SAVED game file you could just do this.
Code: Select all
(
echo health=%health%
echo money=%money%
)>savedgame.txt
Then you could read in the variables like this to load the game.
Code: Select all
FOR /F "TOKENS=1,2 delims==" %%G in (savedgame.txt) DO SET %%G=%%H
You could also set them all on one line without the variable names if you know what order all the variables are in.
Code: Select all
echo "%health","%money%","%level%" >savedgame.txt
Code: Select all
FOR /F "TOKENS=1-3 delims=," %%G in (savedgame.txt) do (
set health=%%~G
set money=%%~H
set level=%%~I
)
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#7
Post
by j0y » 10 Oct 2015 12:10
This is what I have tried to do:
Code: Select all
:save
(
echo set level=%level%
echo set health=%health%
echo set money=%money%
echo set exp=%exp%
echo set expmax=%expmax%
echo set str=%str%
echo set def=%def%
echo set wep=%wep%
echo set wepdis=%wepdis%
echo set arm=%arm%
echo set armdis=%armdis%
echo set hpots=%hpots%
) > save.bat
cls
echo Game Saved!
pause >nul
goto main
Code: Select all
:load
if not exist save.bat goto loaderror
call save.bat
echo Game loaded!
pause >nul
goto main
:loaderror
cls
echo No save file found...
pause >nul
goto menu
I think im getting here but when I tried this it says:
Game Loaded!
Then it says
0 was unexpected at this time
and exits the command prompt
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#8
Post
by j0y » 10 Oct 2015 12:30
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#9
Post
by Squashman » 10 Oct 2015 12:32
Hard to troubleshoot a batch file when you are only showing us part of the batch file.
As you were already told, your original CODE had no syntax errors. So the problem with your original code is some where else in your batch file.
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#10
Post
by j0y » 10 Oct 2015 12:43
Here is all of it currently:
Code: Select all
title AWB
color a
@echo off
:menu
color a
cls
echo =========================
echo Arena of the White Bear
echo =========================
echo.
echo 1)Start
echo 2)Load
echo 3)Exit
echo.
set /p input=Enter:
if %input% == 1 goto start
if %input% == 2 goto load
if %input% == 3 exit
goto menu
:start
set level=1
set health=100
set money=50
set exp=0
set expmax=100
set str=10
set def=0
set wep=0
set wepdis=None
set arm=0
set armdis=None
set hpots=2
set spl=0
set spldis=None
cls
echo Your name is Rosanna. You are a slave, but the Jarl of the North's End has
echo offered you a chance to gain freedom, in exchange you make an interesting and
echo gruesome display in the Arena of the White Bear. Go on, it is your chance,
echo and nothing is going to wrench this away from you.
pause >nul
cls
goto main
:main
if %exp% GEQ %expmax% goto levelup
if %money% LSS 0 set money=0
if %wep% == 1 set wepdis=Whip
if %wep% == 2 set wepdis=Axe
if %wep% == 3 set wepdis=Greatsword
if %wep% == 4 set wepdis=Bow
if %wep% == 5 set wepdis=Flintlock Pistol
if %wep% == 6 set wepdis=Hunter's Rifle
if %wep% == 1 set str=13
if %wep% == 2 set str=16
if %wep% == 3 set str=19
if %wep% == 4 set str=21
if %wep% == 5 set str=25
if %wep% == 6 set str=30
if %arm% == 1 set armdis=Leather
if %arm% == 2 set armdis=Iron
if %arm% == 3 set armdis=Titanium
if %arm% == 4 set armdis=Werewolf Silver
if %arm% == 1 set def=2
if %arm% == 2 set def=3
if %arm% == 3 set def=5
if %arm% == 4 set def=7
if %spl% == 1 set spldis=Fireball
if %spl% == 2 set spldis=Thunder Bolt
if %spl% == 1 set spl=5
if %spl% == 2 set spl=10
cls
echo AWB
echo -----------------------------------------------
echo Rosanna Level:%level% Money:$%money%
echo Hp:%health%/100 Xp:%exp%/%expmax%
echo Weapon:%wepdis% Armour:%armdis% Spell:%spldis%
echo -----------------------------------------------
echo 1)Fight
echo 2)Shop
echo 3)Save
echo 4)Exit
echo.
set /p input=Enter:
if %input% == 1 goto fight1
if %input% == 2 goto shop
if %input% == 3 goto save
if %input% == 4 goto exit
goto main
:load
if not exist Save.bat goto loaderror
< Save.bat (
set /p name=
set /p level=
set /p health=
set /p money=
set /p exp=
set /p expmax=
set /p str=
set /p def=
set /p wep=
set /p wepdis=
set /p arm=
set /p armdis=
set /p hpots=
set /p spl=
set /p spldis=
)
:loaderror
cls
echo No save file found...
pause >nul
goto menu
:fight1
set enhealth=70
:fight
if %health% GTR 100 set health=100
if %health% LSS 1 goto lose
cls
echo FIGHT
echo -------------------------------------------------
echo Rosanna Hp:%health%/100 Level:%level%
echo Weapon:%wepdis% Armour:%armdis% Spell:%spldis%
echo -------------------------------------------------
echo Enemy
echo Hp:%enhealth%/70
echo.
echo 1) Attack
echo 2) Drink Potion
echo 3) Spell
echo 4) Flee
echo.
set /p input=Enter:
if %input% == 1 goto attack
if %input% == 2 goto potion
if %input% == 3 goto spell
if %input% == 4 goto flee
goto fight
:attack
set num=%random:~-2%
if %num% GTR %str% goto attack
if %num% LSS 0 goto attack
if %num% == 00 set num=0
if %num% == 01 set num=1
if %num% == 02 set num=2
if %num% == 03 set num=3
if %num% == 04 set num=4
if %num% == 05 set num=5
if %num% == 06 set num=6
if %num% == 07 set num=7
if %num% == 08 set num=0
if %num% == 09 set num=7
cls
echo You take %num% health from
echo the enemy.
pause >nul
set /a enhealth= %enhealth% - %num%
set /a exp= %exp% + %num% * 2
goto enattack
:enattack
if %enhealth% LSS 1 goto win
set num=%random:~-1%
if %num% GTR 7 goto enattack
if %num% LSS 0 goto enattack
set /a num= %num% - %def%
if %num% LSS 0 set num=0
cls
echo The enemy takes %num% health
echo from you.
pause >nul
set /a health= %health% - %num%
goto fight
:spell
set num=%spl%
if %num% GTR %str% goto attack
if %num% LSS 0 goto attack
if %num% == 00 set num=0
if %num% == 01 set num=1
if %num% == 02 set num=2
if %num% == 03 set num=3
if %num% == 04 set num=4
if %num% == 05 set num=5
if %num% == 06 set num=6
if %num% == 07 set num=7
if %num% == 08 set num=0
if %num% == 09 set num=7
cls
echo You cast a spell and take %num% health from
echo the enemy.
echo.
pause >nul
set /a enhealth= %enhealth% - %num%
set /a exp= %exp% + %num% * 2
goto enattack
:win
set num=%random:~-2%
if %num% GTR 35 goto win
if %num% LSS 15 goto win
cls
echo Congratulations, you killed
echo the enemy!
echo.
echo You found $%num%
pause >nul
set /a money= %money% + %num%
goto main
:lose
cls
color c
echo Oh no, you died!
echo.
echo You will have to start again! Your save file has been deleted!
del save
pause >nul
goto menu
:potion
cls
echo 1)Drink Potion
echo 2)Back
echo.
set /p input=Enter:
if %input% == 2 goto fight
if %hpots% LSS 1 set goto nopots
if %input% == 1 set health=%health%
if %input% == 1 set /a hpots= %hpots% - 1
if %input% == 1 set /a health= %health% + 35
if %input% == 1 goto fight
goto potion
:nopots
cls
echo You have no potions left!
pause >nul
goto fight
:flee
cls
echo You run away!
echo.
echo -$30
pause >nul
set /a money= %money% - 30
goto main
:shop
cls
echo SHOP
echo --------------------------------------
echo Rosanna
echo Money:$%money% Level:%level%
echo --------------------------------------
echo [a)Weapons]b)Armour]c)Potions]d)Spells
echo.
echo 1) Whip $30 Lvl: 1
echo 2) Axe $70 Lvl: 3
echo 3) Greatsword $150 Lvl: 6
echo 4) Bow $200 Lvl: 7
echo 5) Flintlock Pistol $250 Lvl: 7
echo 6) Hunter's Rifle $300 Lvl: 8
echo 7) Back
echo.
set /p input=Enter:
if %input% == 7 goto main
if %input% == a goto shop
if %input% == b goto shop2
if %input% == c goto shop3
if %input% == d goto shop4
if %money% LSS 30 goto nofunds
if %input% == 1 set /a money= %money% - 30
if %input% == 1 set wep=1
if %input% == 1 goto main
if %level% LSS 3 goto nolev
if %money% LSS 70 goto nofunds
if %input% == 2 set /a money= %money% - 70
if %input% == 2 set wep=2
if %input% == 2 goto main
if %level% LSS 6 goto nolev
if %money% LSS 150 goto nofunds
if %input% == 3 set /a money= %money% - 150
if %input% == 3 set wep=3
if %input% == 3 goto main
if %input% == 4 set /a money= %money% - 200
if %input% == 4 set wep=4
if %input% == 4 goto main
if %level% LSS 7 goto nolev
if %money% LSS 200 goto nofunds
if %input% == 5 set /a money= %money% - 250
if %input% == 5 set wep=5
if %input% == 5 goto main
if %level% LSS 7 goto nolev
if %money% LSS 250 goto nofunds
if %input% == 6 set /a money= %money% - 300
if %input% == 6 set wep=6
if %input% == 6 goto main
if %level% LSS 8 goto nolev
if %money% LSS 300 goto nofunds
goto shop
:shop2
cls
echo SHOP
echo --------------------------------------
echo Rosanna
echo Money:$%money% Level:%level%
echo --------------------------------------
echo [a)Weapons]b)Armour]c)Potions]d)Spells
echo.
echo 1) Leather $50 Lvl: 1
echo 2) Iron $90 Lvl: 3
echo 3) Titanium $200 Lvl: 6
echo 4) Werewolf Silver $275 Lvl: 8
echo 5) Back
echo.
set /p input=Enter:
if %input% == 5 goto main
if %input% == a goto shop
if %input% == b goto shop2
if %input% == c goto shop3
if %input% == d goto shop4
if %money% LSS 50 goto nofunds
if %input% == 1 set /a money= %money% - 50
if %input% == 1 set arm=1
if %input% == 1 goto main
if %level% LSS 3 goto nolev
if %money% LSS 90 goto nofunds
if %input% == 2 set /a money= %money% - 90
if %input% == 2 set arm=2
if %input% == 2 goto main
if %level% LSS 6 goto nolev
if %money% LSS 200 goto nofunds
if %input% == 3 set /a money= %money% - 200
if %input% == 3 set arm=3
if %input% == 3 goto main
if %input% == 4 set /a money= %money% - 275
if %input% == 4 set arm=5
if %input% == 4 goto main
if %level% LSS 8 goto nolev
if %money% LSS 275 goto nofunds
goto shop2
:shop3
cls
echo SHOP
echo --------------------------------------
echo Rosanna
echo Money:$%money% Level:%level%
echo --------------------------------------
echo [a)Weapons[b)Armour[c)Potions]d)Spells
echo.
echo 1)Health Potion $30
echo 2)Level Potion $1000
echo 3)Back
echo.
echo.
set /p input=Enter:
if %input% == 3 goto main
if %input% == a goto shop
if %input% == b goto shop2
if %input% == c goto shop3
if %input% == d goto shop4
if %money% LSS 30 goto nofunds
if %input% == 1 set /a money= %money% - 30
if %input% == 1 set hpots= %hpots% + 1
if %input% == 1 goto main
if %money% LSS 1000 goto nofunds
if %input% == 2 set /a money= %money% - 1000
if %input% == 2 goto main
goto shop3
:shop4
cls
echo SHOP
echo --------------------------------------
echo Rosanna
echo Money:$%money% Level:%level%
echo --------------------------------------
echo [a)Weapons[b)Armour[c)Potions]d)Spells
echo.
echo 1) Fireball $50 Lvl: 1
echo 2) Thunder Bolt $150 Lvl: 2
echo 3) Back
echo.
echo.
set /p input=Enter:
if %input% == 3 goto main
if %input% == a goto shop
if %input% == b goto shop2
if %input% == c goto shop3
if %input% == d goto shop4
if %money% LSS 50 goto nofunds
if %input% == 1 set /a money= %money% - 50
if %input% == 1 set spl=5
if %input% == 1 set spldis=Fireball
if %input% == 1 goto main
if %level% LSS 1 goto nolev
if %money% LSS 150 goto nofunds
if %input% == 2 set /a money= %money% - 150
if %input% == 2 set spl=10
if %input% == 2 set spldis=Thunder Bolt
if %input% == 2 goto main
if %level% LSS 2 goto nolev
goto shop4
:nofunds
cls
echo You don't have enough money
echo to purchase this item.
pause >nul
goto main
:nolev
cls
echo Your level isn't high enough
echo to purchase this item.
pause >nul
goto main
:save
(
echo set level=%level%
echo set health=%health%
echo set money=%money%
echo set exp=%exp%
echo set expmax=%expmax%
echo set str=%str%
echo set def=%def%
echo set wep=%wep%
echo set wepdis=%wepdis%
echo set arm=%arm%
echo set armdis=%armdis%
echo set hpots=%hpots%) >Save.bat
goto main
cls
echo Game Saved!
pause >nul
goto main
:exit
cls
echo All unsaved progress will be
echo lost, are you sure? (Y/N)
set /p input=
if %input% == y exit
if %input% == n goto main
goto exit
pause >nul
exit
:levelup
set /a level= %level% + 1
set exp=0
set /a expmax= %expmax% * 150 / 100
cls
echo Congratulations!
echo You are now level %level%!
echo.
pause >nul
goto main
I have fixed the problem with it exiting the terminal, but now it does create a save.bat file, however, when I try to load it, it goes to :loaderror
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#11
Post
by Squashman » 10 Oct 2015 12:46
You can't use a MIX of both sets of CODE!
Code: Select all
:load
if not exist Save.bat goto loaderror
< Save.bat (
set /p name=
set /p level=
set /p health=
set /p money=
set /p exp=
set /p expmax=
set /p str=
set /p def=
set /p wep=
set /p wepdis=
set /p arm=
set /p armdis=
set /p hpots=
set /p spl=
set /p spldis=
)
Code: Select all
:save
(
echo set level=%level%
echo set health=%health%
echo set money=%money%
echo set exp=%exp%
echo set expmax=%expmax%
echo set str=%str%
echo set def=%def%
echo set wep=%wep%
echo set wepdis=%wepdis%
echo set arm=%arm%
echo set armdis=%armdis%
echo set hpots=%hpots%) >Save.bat
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#12
Post
by j0y » 10 Oct 2015 12:48
If I do either of the ones individually, I still get an error. And mixing both of these has fixed half of my problems! The only problem now is to fix how when I try to load the game it doesn't detect the save.bat file even if both of these .bat files are in the same directory.
-
TheHunterManX
- Posts: 54
- Joined: 14 Aug 2015 05:59
#13
Post
by TheHunterManX » 10 Oct 2015 12:49
IF by any chance you get a error about my code it is your code that is causing this as i have tested this code from .bat and .cmd.
Also which version of computer are you using (xp,7,8,10,2000,me)
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#14
Post
by j0y » 10 Oct 2015 12:51
Im using windows 8.1
-
j0y
- Posts: 26
- Joined: 10 Oct 2015 08:22
#15
Post
by j0y » 10 Oct 2015 12:58
What do I do?