Reputation:
I'm making an RPG game in Batch and I need to make it so that if the player hasn't bought a certain weapon it doesn't show up as an option to use. Currently, I have a battle system in which you press a key to use a weapon you have. However, I don't know how to make it so that if you don't own a certain weapon, you can't use it.
I also want it so that even if the text doesn't show, the user still can't use it if they don't have it. For example, if you don't have a sword but you know that to use it you press s, I don't want it to work unless you have it. That way people can't just cheat.
Please help!
Here is the code:
if %sword1% equ true echo 2-Tachi
set /p e="Weapon to equip:"
pause
I don't want the user to just type 2 and have the Tachi. I want it so that they have to have the weapon to use it.
Upvotes: 0
Views: 52
Reputation: 56180
@echo off
setlocal enabledelayedexpansion
set w1=sword
set w2=bow
set w3=axe
set w4=machinegun
set w5=warbow
set w1?=true
set w2?=false
set w3?=true
set w4?=false
set w5?=false
:loop
echo ----------
for /L %%i in (1,1,9) do (
if !w%%i?!==true echo %%i - !w%%i!
)
set /p e="Weapon to equip: "
if !w%e%?!==true (
echo you have choosed: !W%e%!
) else (
echo you don't have one.
goto :loop
)
echo now, that you have a !W%e%!, go and fight!
echo Bamm - Wush - Ouch
if %random% lss 10000 (
echo Congratulations, you have found a %w4%.
set "w4?=true"
)
pause
goto :loop
EDIT (explanations to the code)
the first block of set
just sets the names of the weapons.
The second block of set
sets it they are available to the player. (in gameplay, usually there is only one available in the beginning, others can be found or bought during the game).
The ?
in the variable names is nothing special, only a char like any letter. If it confuses you, you can also use something like B
(like b
oolean): set w5B=false
The for
-loop is the heart of this code. It displays all available weapons (it tries w1
to w9
. You can adapt it to process hundreds or even thousands of items (it starts from 1
, increments by 1
until 9
))
If it is available (true
), it echoes the number (%%i
) and the weapon-name.
!w%%i?!
is a bit tricky. %%i is the "loop-counter" (1,2,3,...,9), so w%%i?
will translate to (for example) w4?
(and w%%i
to w4
.
Because you need the variable with that name, you'll have to put it between %
, but as %%i
is already part of the variable, the interpreter would get confused. For that, we use delayed expansion
(search for it in stackoverflow, there are more findings than you can read) and therefore can use !
instead of %
, so the complete variable is named !w%%i?!
(remember: the ?
is just a letter). (don't be depressed, if you don't get it the first time, just play around with it and it will make sense to you some day).
The if
after the set /p e="Weapon to equip: "
checks, if the weapon is available (true
) if yes, fine, if no then error/try again.
The if %random%
... part shows you how to make an item available (true
). You can also make one unavailable by setting the flag to false
.) Wheather you do that like me as a finding in a fight, or in a shop (buy/sell), the doing is the same.
I'm sure, you already guessed, that you'll have to replace echo Bamm - Wush - Ouch
with some code for a fight.
Upvotes: 1
Reputation: 11
I find it hard to come up to a solution since the user will be able to edit the .bat file anyway? Wouldn't it be better to write it in C++ or C#?
Upvotes: 0