I got pretty good at programming in batch. I know its not made for games but i try. I even got that far that i could create a map where things happen. Problem is, i have to specify each and every block. I need relative coordinates.
Here is an example of my Mapping.
set a1=O
set a2=O
set a3=O
set a4=O
set b1=O
set b2=O
set b3=O
set b4=O
set c1=O
set c2=O
set c3=O
set c4=O
:mapping
echo %a1%%a2%%a3%%a4%
echo %b1%%b2%%b3%%b4%
echo %c1%%c2%%c3%%c4%
This will result in:
OOOO
OOOO
OOOO
With this i can create Maps. So for a block to change i have to do "set a2=X"
resulting in
OXOO
OOOO
OOOO
But when creating large games it gets very intense when trying to map.
Is there any way i could get relative coordinates to work?
I was able to create Flappy Bird with this! Here is a Pastebin link to the code of it: http://pastebin.com/h34xdSd8
You can look at it and try it out if you want.(If you do so then exclude the line "if %run% neq true exit", change .exe in line 227 to .bat and create a file named Keyboard.bat with this code: http://pastebin.com/J3EkMR4q.) Its hand written too.
and with that code you can see how hard it is..
Please help, i need relative coordiantes.
Edit:
for %%a in (a b c d e f g) do for /L %%i in (1,1,21) do set %%a%%i=0
This Code worked for me. It will Setup all my Blocks.
One more Problem though. Is there any easy way to use this to move objects? Like when pressing left that my Characted moves one block to the left without me having to specify that block, so relative.
for coordinates you can try:
set /a x=12
set /a y=12
set "q_%x%_%y%=X"
this sets q_12_12 to X.
You can use for for the definition and echoing of the variables (try for /? for help with for).
Related
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.
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.
I've got a pretty simple batch script, but for some reason the conditional isn't working like I'm expecting:
SET RIG=%1
IF /I RIG==Polar (
SET SQLSERVER=SQL01
SET MONITOR=SQL02
SET TESTSETTING="test"
SET TESTCONTAINER="test2
) ELSE (
ECHO Your input was %1.
)
When I pass the following command: testrun.bat Polar I get the ECHO statement returned: Your input was Polar."
I've tried putting the %1 in quotations, messing with the way parentheses are laid out, and I've reviewed ss64.com's syntax for batch script IF statements, but I just can't figure this one out.
RIGNAME is never going to be equal to Polar
Now you could try assigning the value to rigname rather than rig.
Even then, yu'd need to compare %rigname% to Polar to access the content of rigname rather than the string RIGNAME.
Alright, I need help here. I have done this before where you have variable1 (let's say it's eat1=apple), variable2 (this is eat2=orange), and variable3 (appaleorange=apple and orange). I need it to do this:
echo Apple:%eat1%
echo Orange:%eat2%
echo Apple & Orange:%eat1%%eat2%
Now, you can see my problem. That above script wouldn't show the word and, only appleorange. That isn't my script and the reason I need this is because I have multiple variables with numbers in them. I have done this before and I forgot how... I know you can do a call and then multiple %'s.
In this case I want fterm variable to be fterm (not sure how to have it in there and not be a variable) and stermnum as a number that will be changed often on other parts of the script.
My code:
set stermnum=1
call set exsternum=%%fterm%%stermnum%%%
echo Selected term:%stermnum% ^(%exsternum%^)
Does anyone know what to do?
Thanks and sorry it was long :P
~Edit:I found it out... If it helps anyone I did:
call set exsternum=%%fterm%stermnum%%
Sorry for posting this even though I figured it out so fast
The OP appended a solution to the question, but it does not relate to the original question scenario, and it still has a bug.
Here is the OP's solution in terms of the original scenario:
set "eat1=apple"
set "eat2=orange"
set "appleorange=apple and orange"
call echo %%%eat1%%eat2%%%
For the actual code, I believe the OP wants to access an array of variables named fterm1, fterm2, fterm3, etc. And the number suffix is in a variable named stermnum.
call set exsternum=%%fterm%stermnum%%%
If fterm is itself a variable containing the base name of the array, then the solution becomes:
call set exsternum=%%%fterm%%stermnum%%%
But CALL is inefficient - Probably not noticeable with a single CALL, but it becomes painfully slow if executed thousands of times in a loop.
There is a much faster solution using delayed expansion. Delayed expansion must be enabled prior to being used.
Original scenario:
setlocal enableDelayedExpansion
set "eat1=apple"
set "eat2=orange"
set "appleorange=apple and orange"
echo !%eat1%%eat2%!
Actual code, interpretation 1:
setlocal enableDelayedExpansion
REM additonal code ...
set exsternum=!fterm%stermnum%!
Actual code, interpretation 2:
setlocal enableDelayedExpansion
REM additonal code ...
set exsternum=!%fterm%%stermnum%!
Within my batch file I have a variable that contains a file path:
SET VAR1=C:\Folder1\Folder2\File.txt
I would like to extract on the directory structure and retreive:
C:\Folder1\Folder2\
I have read threads like this where I need to use %~dp0 where 0 I believe is passed as a parameter. I have tried %~dpVAR1 but that doesn't work. How can I get the output I'm looking for, but with a variable containing the file path?
Also, to make matters difficult, I have to perform all of this within an IF condition which means that once the variable is declared, I will need to refer to it with ! instead of % (I have declared setlocal enableextensions enabledelayedexpansion at the beginning of my script to allow for this).
Any help is much appreciated!
Thanks!
Andrew
You are attempting to use parameter expansion syntax on an environment variable - that cannot work. But it is relatively easy to do what you want.
Using a CALL (relatively slow):
(...
call :getPath "!var!" var
...
)
exit /b
:getPath
set "%2=%~dp1"
exit /b
Using FOR, assuming the variable does not contain any wildcards (fast)
(...
for %%F in ("!var!") do set "var=%%~dpF"
...
)
Using FOR, if the variable may contain wildcards (also fast)
(...
for /f "delims=" %%F in ("!var!") do set "var=%%~dpF"
...
)
Note 1: If the variable does not contain the full path, then all the solutions will attempt to resolve the name into an absolute path and will return the full absolute path. For example, if var contains foobar\test.txt, then the solutions will include the full path to the current directory, even if the file is not found. Something like c:\pathToCurrentDirectory\foobar\.
Note 2: All solutions above will remove all quotes from the path.
Note 3: A path could include the ! character, which will cause problems when expanding %~dp1 or %%~dpF because you have delayed expansion enabled. The delayed expansion will corrupt both ^ and ! if the value contains !. There is a solution that involves protecting both ! and ^. Here is a demonstration applied to the last solution above. The protection requires normal expansion, and since you are within a code block, it requires at least one CALL. It could be done without a subroutine, but it is easier with a subroutine. The subroutine assumes the variable is named var.
(...
call :getPath
...
)
exit /b
:getPath
set "var=!var:"=!"
set "var=!var:^=^^^^!"
set "var=%var:!=^^^!%" !
for /f "delims=" %%F in ("!var!") do set "var=%%~dpF" !
exit /b
I do believe (once again) many questions are on the same topic (string constraints, or splitting strings).
Instead of giving you the whole code, I'm going to give you a template and explain why %~dpVAR! didn't work.
Firstly, why %~dpVAR! did't work.
Before I get into modifiers, let's discuss parameters. You may know that batch files can parse parameters to each other. These parameters can be called by using a single percent sign (%) in front of the numbers 0-9. As far as I'm aware (someone might have made a way for more to be parsed), only 9 parameters can be parsed. You may think that is wrong (there's 10 parameters right?). Parameters 1-9 are parsed to the batch file (or function within one), %0 is the file path of the batch file (or function name). If you look, %~dp0 shares some (not really) resemblance to %0. This will be discussed below.
Secondly, the term %~dp0 has modifiers in it. Modifiers are things that modify variables (only in the case of parameters and those declared in for loops, you know the ones with double percent signs like %%i) and parameters. The modifier d expands the parameter to a drive letter only while p expands the parameter to a path only. You may think that these would contradict themselves, but parameters can be combined to create extremely wacky formats.
So, as you can see, you attempt at replacing 0 with your variable name failed because it's not specified for those sort of things.
Now, on to the template.
You can constrain variables (and put them into other variables) like this:
set variable=!variable:~offset,amount!
Don't worry if that seems confusing, I'm about to explain the components.
Firstly, notice that there is no /a switch. This is because this is not a mathematical function (don't really know why I added this). So, before I explain it, here's an example of what it would do to a variable name numbers that has the value of 0123456789.
set numbers=!numbers:~5,1!
By using that line of code, numbers would now equal 5. This is because it is recreating the variable with a smaller version of the original value (gee this is hard to explain). As you can see, there is a 5 where offset was on the template above. This is because it is skipping the first 5 characters and setting the variable as the next amount, or 1 character (I really hope you're getting this).
So basically, it sets a variable as a shorter value of a different (or the same) variable determined by the offset and the amount of characters to contain in it.
I really hope this helps because I probably wouldn't understand a word of this.
Can someone redirect this poor guy to a link explaining this better (I tried, ok!)?
Complete example of extracting paths from variable:
#echo off
set /p Fullpath="Specify full path: "
call :getPath %Fullpath% filename folder
echo %filename%
echo %folder%
pause
exit /b
:getPath
set "%2=%~nx1"
set "%3=%~dp1"
exit /b
Would this work:
SET VAR1=C:\Folder1\Folder2\File.txt
echo %var1%
Where Echo is the name of your exe.
%CD% may work as well: Echo %CD%