How to add an HP (health) variable to a batch game - batch-file

So, I'm making a little batch CYOA style game, and I want to make an HP variable.
I used the code : set /p hp=100 which just shows up as 100 in the file when it's run.
I also can't use the code %hp% to make it visible when I want it to be!
Anybody got any simple ways to add a variable that can't be changed by the file's user, but can be changed if the code calls for it, and can be shown when the %hp% command is typed in?

Don't use /p. That's for getting an answer back from the user, given a prompt string. Example:
set /p age=How old are you?
Whatever the user enters will get set to age and can be used with %age%.
In your case, just do set hp=100, and then you can say echo HP: %hp% to display it. Done.
Also, you can type /? (as in set /?) in cmd to get help for a command.

Related

Issue with my code echoing "OFF" despite no reason for it to do so, to my knowledge

I have some code in batch for taking in user input, and outputting the inputed info using echo, yet it only outputs "ECHO is OFF".
I've copy and pasted the small tidbit of code into its own program and have figured out that is revolves around this specific bit of code. I've spent the last few days sparsely in my free-time trying to figure out why this isn't working, and I'd love to post the full programs worth of code, but its a tad confidential right now. I've simplified the code into a functional "program" so you guys can just copy and paste to test it yourself, but I'm having issues with it and have no idea why.
#echo off
SETLOCAL EnableDelayedExpansion
set /p "%UserMSGInput%= >"
echo %UserMSGInput%
pause
I'm expecting it to simply output the user input value, but instead it just outputs 'ECHO IS OFF'.
I believe what you're trying to do is:
set /p UserMSGInput=" >"
The line:
set /p "%UserMSGInput%= >"
means "prompt for input, using the value of the environment variable UserMSGInput as part of the prompt." But there's no environment variable name in that command.
Since %UserMSGInput% doesn't have a value, the prompt becomes just ">". The echo command is trying to print an environment variable that doesn't exist, so it just sees:
echo
which means "print the current state of the echo command" - in this case, "ECHO is off."

Redirect and apply color from text file

I just started coding with Batch a couple weeks ago, a couple hours a day while at work.
I'm writing a script to auto-launch and auto-create dated files. I thought it would be nice to try something "fun".
What I'm trying to do is have my main script save a configuration file with the Batch color code to call upon later/first start and change itself to the previously saved color code. Preferably with a text file.
I've tried using the global variable thing. I think our IT has that disabled as I cannot share variable states between scripts. I haven't tried to enable it. I should also mention we are on Windows 7.
I have since gone the route of creating a colors.bat with the below example code within. I have applied the redirection to my main script to save the file after asking the user for a color code that gets applied to a variable. Right now this Batch file is called upon at a first start in the main script, later I will add a statement to check if the file exists or not.
color 5e
Instead I would prefer a text file that can be called upon or perhaps even a file without a container to prevent backend editing.
However I can't seem to get my script to apply the code inside the text file properly. Below are examples that have failed to apply the color change. Inside the "colors.txt" file is just the Batch compatible color code of "5e" and nothing else.
type colors.txt | color
echo Test
color <colors.txt
echo Test
The above code I have tried with varying placements of the spaces as well.
I've been at this for a few hours now reading up on Batch and Redirection and really anything else of interest. I tried at one point putting the color code as the file name and then trying the Parameter stuff like " %~n1 ". I couldn't figure that out at all.
I hope none of that was confusing. But I'll reiterate, the script as it is right now works. I want to use a .txt container instead of .bat or better yet no container at all. Or even get the global variables enabled, whichever is the best route. I want to call upon this file at script start to apply the color code.
If you have any other questions let me know.
Thanks.
It is fairly straight forward
#echo off
setlocal enabledelayedexpansion
if exist colors.txt (
goto setcolor
) else (
set /p color="Choose color: "
echo !color! > colors.txt
attrib colors.txt +r
endlocal
goto setcolor
)
:setcolor
for /F %%i in (colors.txt) do color %%i
echo Test
pause
The above code check if colors.txt exists, if not it will prompt you for a preferred colour and background colour. Save the code to file and then set it, next time the file will be found and it will just set the colour without re-prompting. We also set attrib +r to make file read-only, if you want to prevent people from editing it, there are ways around it though.
EDIT:
enabledelayedexpansion do cmd /? and you will see
/V:ON
Enable delayed environment variable expansion using ! as the
delimiter. For example, /V:ON would allow !var! to expand the
variable var at execution time. The var syntax expands variables
at input time, which is quite a different thing when inside of a FOR
loop.

Typable Variable Prompt In A Batch File?

Here is what my batch currently looks like:
srcds.exe +hostname "[FAST-DL]Ultra-Realistic Apocalypse Roleplay CustomRP ALPHA" +maxplayers 8 -console -authkey 8F1B5086400AC592380B5F303778D008 +gamemode darkrp +map gm_atomic
How would I make it so when the window opens, I can enter a custom variable for +map. | (optional) or I can continue using the variable I previously used last time the batch was run (in the case of the sample code, it would be gm_atomic) |
set "map="
set /p "map=Some prompt for map "
You can then detect whether someone entered a map using
if defined map (something) else (something_else)
or, perhaps
if defined map set "map=+map %map%"
then
srcde.exe ...whatever... %map%
which will add nothing if map was not entered, or +map whateverwasentered is there was an entry made.
You could supply a default if you wish:
set "map=whateveryourdefaultvalueis"
set /p "map=Some prompt for map [default:%map%]"
So your users need only press Enter to use the default you supply... (which may, with a little code, be derived from a file - the possibilities are limited by your imagination...)

Batch File: Output variable to text file

I would like to output a variable to a text file. I have done this in my code, but I have one output within an if statement that I can't get to work.
if not exist "%TuningLog%" (
set Title=Tuning Parameters Saving Log
set LogTitle=--------- %Title% ---------
echo %LogTitle%>> "%TuningLog%"
)
All its supposed to do is check first for the existense of a log file and if it doesn't exist then I want to in the first instance append a title.
But all I get in the log file is " echo is off." If I don't use a variable and just place text there then it works fine.
Can anybody spot the problem? Thanks for reading and for any help!
UPDATE:
I changed my code to. I don't know much about delayed expansion, but I tried this and it happened to work...
if not exist "%TuningLog%" (
setlocal enabledelayedexpansion
set Title=Tuning Parameters Saving Log
set LogTitle=--------- !Title! ---------
echo !LogTitle!>> "!TuningLog!"
endlocal
)
If anyone can provide a reason as to why the first attempt didn't work and this did, then please inform this thread for the purposes of learning. Thank you!
because or early expansion. your variable gets replaced with its value at the moment the block starts. at this time, it is still undefined and thus empty. if you have echo on, you can watch this happening on screen. if you enable delayed expansion, as you did in your second example, it gets only expanded at the moment it is used, so it works as you expect variables to work in "real" programming languages.
EnableDelayedExpansion
causes Variables to be expanded in simple language it causes the system to treat the value of variable and not the variable name itself

how to clear a variable which is set from command line by "set /p var1="

In Windows batch files, I accept variable from user input by set /p var1=, after var1 is used, I don't know how to reset/clear its value.
If I don't reset/clear its value, when user meets set /p var1= again, and user enter directly. the previous input value will be still there. I don't want it, How to reset it for new user input?
To clear a variable, regardless how it was set:
set "var1="
rare issue, but you can try this:
set "var=%var*=%"
Different answers to original question were already provided above. One additional hint: within batch-script you can use the scopes:
setlocal
...
endlocal
This ensures that the variables are only accessible within your batch script and not affecting variables outside of your batch script (i.e. your variables are also not accessible outside of the batch script). By exiting of the batch script - the variables used locally get automatically "cleared".

Resources