Batch file/scipt array

February 7, 2011 at 15:58:45
Specs: Windows XP/Vista
Is there anyway to use an array in a batch file/script so that I could store different values for different parts of the program. To be more specific I am writing a game in batch and I want to have different monsters set to different rooms. Each room is set with its on location ID number. I have a random number generator that will first choose the room number and then the monster number. I want to store both the monster number and the room number(s) it is assigned to. Any help? If this isn't clear I am sorry kinda in a rush.

See More: Batch file/scipt array

Report •

February 8, 2011 at 15:18:28
@echo off
SetLocal EnableDelayedExpansion

for /l %%a in (1 1 20) do (
    set var[%%a]=!random! {no. %%a}

for /l %%a in (1 1 20) do (
    echo !var[%%a]!

Report •

February 10, 2011 at 09:00:11
Okay thank you but now my question is what goes where. Not that advanced with batch scripts.

Report •

February 10, 2011 at 15:53:45
I'm not sure of exactly what you want.....

The "array" is fairly simple:


Where "basevariable" is just the static name and index is the incrementing number.

It can be set to in the following ways:

rem static number
set basevariable1=whatever

rem standard variable
set basevariable%index%=something

rem delayed var
set basevariable!index!=another thing

rem for var
set basevariable%%a=yet another thing

rem parameter - not very useful, but...
set basevariable%1=yet another thing

To expand the pseudo array:

rem static/std
echo %basevariable1%

rem static/delayed
echo !basevariable1!

rem std/delayed
echo !basevariable%index%!

rem for/delayed
echo !basevariable%%a!

rem param/delayed
echo !basevariable%1!

rem std/nested
call echo %%basevariable%index%%%

rem for/nested
call echo %%basevariable%%a%%

rem param/nested
call echo %%basevariable%1%%

Report •

Related Solutions

February 11, 2011 at 09:01:25
To clairfy what I want is to use the following random code:
echo you rolled a %R%
pause >nul
if %R%==1 goto trollbattle
if %R%==2 goto trollbattle
if %R%==3 goto trollbattle
if %R%==4 goto trollbattle
if %R%==5 goto trollbattle
if %R%==6 goto trollbattle
if %R%==7 goto trollbattle
if %R%==8 goto trollbattle
if %R%==9 goto trollbattle
if %R%==10 goto trollbattle
pause >nul
set tda=3
set thp=30
if %sword%==1 set damage=5
if not %sword%==1 set damage=3
echo advanced combat not ready. just press enter.
pause >nul
set /A Rand=(%Random% %%damage%)+1
echo you deal %Rand% damage
set /A thp= thp - %Rand%
echo troll health is %thp%
pause >nul
if %thp%==0 goto battleend
if not %thp%==0 goto plhp
pause >nul
set /A Rand=(%Random% %%tda%)+1
set /A plhp= plhp - %Rand%
echo troll hits
echo you take %Rand% damage
echo your remaining health is %plhp%
pause >nul
if %plhp%==0 goto battleend
if not %plhp%==0 goto tfight
pause >nul
if %plhp%==0 echo you lose
if not %plhp%==0 echo troll defeated
pause >nul

With this I will generate a random number. This number will be a room with an already assigned value. When the values match the room is then stored in memory and a random number corresponding to a specific monster will be set to that room. After the monster is set in the room, the program will loop back to the room number, but the program will exclude the number of a room if it already has a monster. Is that clear enough?

Report •

February 11, 2011 at 19:33:28
It's clear what your trying to do, not so much for exactly what you need help with.

I'm guessing that %r% is the room number and it looks like you have 10(numbered 1-10) rooms so at the start of the script you can set a psuedo array of monster hit points, like so:

@echo off
SetLocal EnableDelayedExpansion

set monsters[1]=35
set monsters[2]=17


You can even make monster free rooms by setting the initial hp to zero.

Then you could do something like:

if !monsters[%r%]! leq 0 (
   echo There are no monsters in this room^^!
    goto whereever
) else goto trollbattle

Then it's:

set /A monsters[%r%] -= %Rand%

Another thing I would suggest would be swapping the == with leq because you may take away more hp than available which will result in a negative and false condition:

if !monsters[%r%]! leq 0 goto battleend

One tiny last little thing:

set /A Rand=(%Random% %%tda%)+1

Should really be:

set /A Rand=(%Random% %% %tda%)+1


rem this is how it is currently expanding....
set /A Rand=(%Random% %% tda)+1

Report •

Ask Question