checking the value of a variables variable in Windows batch - variables

Well, I'm trying to make a basic RPG in batch (Don't judge me) and I'm in a bit of a predicament with equiping items. It's a bit hard to explain so here's some code:
set inv1=sword
...
set inv9=empty
echo which item do you want to equip?
echo.
echo 1) %inv1%
...
echo 9) %inv9%
echo.
set /p c="> "
set invt=inv%c%
set type=%invt%_type
if "%%type%%"=="equipable" (
set itemslot=%invt%
basically, I need to replace %%type%% with the value of the value of 'type'. Is there any way to do this? if so, it would save me many lines of code and make it FAR easier to implement new items. THANKYOU to anybody who can help. (and, yes, I have searched the internet, this forum and other forums but no help :( )

Is this going to work in your case?
call set "t=%%type%%"
if /i "%t%"=="equipable" (
set itemslot=%invt%
edit This is the same as the comment by halfbit

Related

How do I add a variable's number to another variable in batch?

So, I'm making a game with income (variable) and every turn the money variable goes up, as much as the income variable is for example:
set income=350
set /a "money=money+INCOME"
If I use the normal %VARIABLENAME% to call the variable it doesn't work and the money value stays the same. Please help!
Your issue could be caused by delayed expansion.
This alternate script should work for you:
#echo off
set money=20
set income=30
set /a money+=%income%
pause

How can I save variables longterm in Batch?

I've seen it done before on someone else's Batch program, so I'm sure it's possible. Every time I launch my program I run a series of lines of code such as:
set/a num=0
set/a tog=1
set/a ran=%random% %%10 +1
However, I would like for certain variables to stay around, even after the program is closed. I need scripts that can:
Write in a separate document the values of certain variables
Check that document for values at any point in any usage session
Thanks in advance!
You need a save system like so:
(
echo %VARIABLE1%
echo %VARIABLE2%
echo %VARIABLE3%
echo %VARIABLE4%
echo %VARIABLE5%
) > LOGS.prgmsav
The file extension can be .txt or something weird like .nerfguns, to load LOGS.prgmsav:
< LOGS.prgmsav (
set /p VARIABLE1=
set /p VARIABLE2=
set /p VARIABLE3=
set /p VARIABLE4=
set /p VARIABLE5=
)
The VARIABLE is any variable, you can save as many as you want! I hope I helped!

Is it possible to set a dynamic variable name?

Is it possible to do something like this?
set wordNum=1
set word=Test
:: ^
::This stuff is just so that if people want to
::test it, they have all the variables they need.
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
If so, what am I doing wrong?
In context, I'm trying to remind the computer of the last word it recorded.
Almost done.
setlocal enabledelayedexpansion
set wordNum=1
set word=Test
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
To use delayed expansion you need to enable it

How to make a variable minus from a number. Batch File

Basically I want to use 2020 minus a variable to make another variable.
This is the code I have at the moment.
echo Type Your Year, then press enter.
set/p "myear=>"
echo Type the year of the person's stuff you want to copy, then press enter.
set/p "yearofv=>"
cls
set/p "myyear="2020-%myear%"
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
can anybody run make any suggestions as how to help.
Thanks
Sam Inc.
A quick search yielded the following relevant answer. Use "set /a".
Calculating the sum of two variables in a batch script
It would be easier to identify your question if your provided code example was legible.
You can make it legible by highlighting it and clicking on the brackets.
I'm sure I'll be able to provide what you need if I could see what you're working with.
Thanks for taking the time to make your example more legible.
I believe your confusion is located where I have placed a "rem". That may be where you want to perform your calculation (or some other variation similar).
I placed an example for performing the calculation, then placed a goto statement so that you can look at the results.
This might help you to get a grip of resolving your issue:
#echo off
echo Type Your Year, then press enter.
rem set/p "myear=>"
set /p myyear="Enter myyear: " %=%
echo Type the year of the person's stuff you want to copy, then press enter.
rem set/p "yearofv=>"
set /p yearofv="Year of v: " %=%
cls
echo This is the difference between 2020 and "myyear..."
set/a difference=2020-%myyear%
echo %difference%
goto skip
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
:skip
By the way, I placed an "#echo off" at the top to make it easier to read the flow when executing the script.
Also, you might consider removing the "cls" that you have until you have it debugged. While debugging you need to be able to view the history of what has happened.

Batch IF statement variable inside another variable

I'm having a problem with a framework for a batch game I'm trying to create. I want to check if a variable is something other than a blank space. However, which variable I'm trying to check is in itself defined by two variables. For example:
if not %px%xplayerlocation%y%yplayerlocation%%==%blank% goto wherever
As you can see, the variable to be checked is determined by the values of %xplayerlocation% and %yplayerlocation%. To my knowledge, only the outermost %% signs are being read as being a variable and the inner ones are being read as literal percent signs. Does anyone know a way around this problem? I'll give any additional information if anyone needs it. Thanks.
#ECHO OFF
SETLOCAL
SET "blank= "
SET xplayerlocation=3
SET yplayerlocation=4
ECHO test with blank================
SET px3y4=%blank%
CALL SET varval=%%px%xplayerlocation%y%yplayerlocation%%%
if not "%varval%"=="%blank%" ECHO goto wherever - NOT blank
ECHO test with "Q"================
SET px3y4=Q
CALL SET varval=%%px%xplayerlocation%y%yplayerlocation%%%
if not "%varval%"=="%blank%" ECHO goto wherever - NOT blank
GOTO :EOF
This should get you out of trouble.
You have recognized the source of the problem, but your description of the behavior is incorrect. The parser will attempt to expand variables named px and y, and it will convert the final %% into %.
The Magoo solution will work, but using CALL is quite slow. That may not be a problem for many small scripts, but for a batch game it can kill performance.
You want delayed expansion. Include setlocal enableDelayedExpansion near the beginning of your script. Then use the following:
if not !px%xplayerlocation%y%yplayerlocation%!==%blank% goto wherever
Normal %var% expansion occurs early at parse time, and !var! expansion occurs late at execution time, so you get the proper result.
The above will not work if the value of %blank% is a space. The simplest solution would be to use delayed expansion for !blank! as well.
You might find yourself in a situation where the coordinate values need to be set and expanded within the same block of code, like in a FOR loop or IF statement:
setlocal enableDelayedExpansion
...
REM This does not work
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
if not !px%xplayerlocation%y%yplayerlocation%!==!blank! REM doSomething
...
)
The above will not work because %var% expansion occurs at parse time, and the entire parenthesized block of code is parsed before any code is executed. So the expanded value is constant - it will expand to the value that existed before the loop started.
The solution is to transfer the coordinate values to FOR variables using delayed expansion:
setlocal enableDelayedExpansion
...
REM This works
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
for %%X in (!xPlayerLocation!) do for %%Y in (!yPlayerLocation!) do (
if not !px%%X%y%%Y!==%blank% REM doSomething
)
...
)
or
setlocal enableDelayedExpansion
...
REM This also works
for ... in (...) do (
...
set /a "xPlayerLocation+=xChange, yPlayerLocation+=yChange"
for /f "tokens=1,2" %%X in ("!xPlayerLocation! !yPlayerLocation!") do (
if not !px%%X%y%%Y!==%blank% REM doSomething
)
...
)
If you are serious about developing a high quality game using batch, then you may be interested in studying the techniques I used in developing SNAKE.BAT - an arcade style game using only native batch commands. The beginning of the post is a bunch of code, but afterwards I describe a number of techniques I used for improving performance. It is advanced stuff, so don't try to absorb everything at once. Absorb what you can, and then revisit the post later on after you gain more experience.