Page 1 of 2

Calling external file - cmd shuts

Posted: 23 Aug 2012 00:12
by keyboard1333
Hello everyone!

This is game.bat

Code: Select all

echo off
title Game
color 0a
:BEGINNING
cls
if not exist %cd%\Data\ (
   echo ERROR! FILES MISSING
   pause
   exit
) else (
   goto GETVAR
   pause
   exit
)
if not exist %cd%\Story\ (
   echo ERROR! FILES MISSING
) else (
   goto GETVAR
   pause
   exit
)
:GETVAR
echo Initialising...
call Run-time\initialise.bat   
cls
type %cd%\Story\story1.fldr
pause
:CMD
set cmd=null
cls
set /p cmd=Command:
if %cmd% == use (
   %cmd%
   goto CMD
)
if %cmd% == equip (
   %cmd%
   goto CMD
)
goto ERROR
:ERROR
cls
echo Invalid command (%cmd%)
pause
goto CMD


and this is use.bat

Code: Select all

echo %1
pause


But during the :CMD section of the program, if I type use sword (sword is an example) the cmd shuts...

Any suggestions as to why this is happening and how to fix it?
reaper17

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 00:26
by abc0502
I'm not sure but try these two

First
add double quotes arround %cmd% and use

Code: Select all

if "%cmd%" == "use" (


If didn't work then change %cmd% to %cmd:~0,3%
so it take the first 3 charachters which is just the word "use" cause you comapare "use sword" with only "use"
if worked you will have to change the "equib" too

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 00:37
by foxidrive
Try this snippet.

Alter this line
) do if /i "%%a"=="%%c" echo call "%%a" "%%b"&PAUSE&goto :cmd
to this
) do if /i "%%a"=="%%c" call "%%a" "%%b"&goto :cmd
to enable it.


Code: Select all

@echo off
:CMD
set cmd=null
cls
set /p "cmd=Command: "
for /f "tokens=1,2 delims= " %%a in ("%cmd%") do (
for %%c in (
go
walk
run
use
equip
wear
hold
pickup
steal
murder
) do if /i "%%a"=="%%c" echo call "%%a" "%%b"&PAUSE&goto :cmd
)
:ERROR
cls
for /f "tokens=1,2 delims= " %%a in ("%cmd%") do echo Invalid command (%%a)
pause>nul
goto :CMD

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 03:08
by keyboard1333

Code: Select all

%cmd:~0,3%


How can I use that when the length of the command changes?

sword is the parameter so I really need to seperate the command and parameter...
Is there anyway to remove everything after (including) the space)

I'll try what you suggested with the ""...
reaper17

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 18:42
by keyboard1333
Ok, am now using a for loop to split the string, but it isn't working; when you type anything and hit enter, the cmd window just closes -

Code: Select all

echo off
title Game
color 0a
:BEGINNING
cls
if not exist %cd%\Data\ (
   echo ERROR! FILES MISSING
   pause
   exit
) else (
   goto GETVAR
   pause
   exit
)
if not exist %cd%\Story\ (
   echo ERROR! FILES MISSING
) else (
   goto GETVAR
   pause
   exit
)
:GETVAR
echo Initialising...
call Run-time\initialise.bat   
cls
type %cd%\Story\story1.fldr
pause
:CMD
set cmd=null
cls
set /p cmd=Command:
FOR /F %a in (%cmd%) do
if "%a" == "use" (
   %cmd%
   goto CMD
)
if "%a" == "equip" (
   %cmd%
   goto CMD
)
goto ERROR
:ERROR
cls
echo Invalid command (%cmd%)
pause
goto CMD

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 18:47
by foxidrive
Wasn't my solution worth anything?

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 21:06
by keyboard1333
Oh, I'm so sorry foxidrive!!!!
I tried your solution and it worked perfectly (with a few minor changes) :mrgreen:
One more quick question; it's related to the project that this thread concerns so I don't want to start a new thread.

I want to make an inventory of items the user has, but I'm not sure how...
As they discover an item, I need to be able to add it and if they lose or drop it, I need to be able to remove it.
Also, I need to check if they have a particular item in their inventory and I need to display all the items in the inventory.
I hope it's not to bigger ask (I'm not specifacally asking for the code, I'm just asking how to do it).
Thanks so much for all the help you guys have given me, both in this life and the previous (if you know what I mean) ;)
reaper17

p.s. Is there no thanks button on this forum?

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 22:37
by foxidrive
If think you'd store the inventory in an array of environment variables in memory.

You could store it in a file but accessing it would be slower.

Re: Calling external file - cmd shuts

Posted: 23 Aug 2012 23:54
by keyboard1333
I'd actually prefer to store it in a file; I'm not that concerend about how slow it is...
If you store one item per line, how would you do the three tasks I mentioned? (adding, removing, checking for existance).
I guess for adding, you could just echo a new line to the file, but then there'd be no order to them...
I'd like to group object types; weapons, armor, etc...
Oh, and one more thing; how'd you stop a user opening up the file and editing away?
Any help?
p.s. thanks for all the help guys/gals :D

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 00:10
by foxidrive
reaper17 wrote:I'd actually prefer to store it in a file; I'm not that concerend about how slow it is...
If you store one item per line, how would you do the three tasks I mentioned? (adding, removing, checking for existance).


removing could be done like this:

Code: Select all

set item=sword
type file.txt | findtr /r /i /v "^%item%$" > newfile.txt
move newfile.txt file.txt >nul


checking for existence:

Code: Select all

set "gotit="
for /f "delims=" %%a in (file.txt) do if "%%a"="armour" set gotit=1
if defined gotit echo you have the armour.



I'd like to group object types; weapons, armor, etc...


You would need to mainpulate the file, or just use different files for the different classes of items.

You can use sort to sort them if you need to:

Code: Select all

sort <file.txt >newfile.txt
move newfile.txt file.txt >nul


Oh, and one more thing; how'd you stop a user opening up the file and editing away?


Cheaters will always cheat. Don't bother trying to match them in a battle of wits.
The good part of using plain text is that other people can build on your work or learn themselves how to do it.

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 00:46
by keyboard1333
I know there's no way to stop anyone who really wants to edit it.
I'm compiling the batch files into exe's.
Now, you may say that there's no point, but I disagree;
1: It stops the "honest people" from changing stuff; only those with the right tools can do it.
2: It stops someone accidently deleting something in one of the files...
Same goes for the inventory file. But I suppose if there's no way around it, I could just leave it as plain text...

One more question -
My file structure looks like this:

Game {
game.bat
Data {
ObjectData {
Armor {
Leather_Armor.bat
etc
}
Enemy
(None as of yet)
}
Object
(None as of yet)
}
Weapons
Bronze_Axe.bat
Bronze_Dagger.bat
etc
}
}
}
Plugins {

}
}
}
(there are more folders but they're un-relevant to the quesiton)


Inside Bronze_Axe.bat is this code -

Code: Select all

set weapon=Bronze_Axe
set weaponinfo=An axe made of bronze.
set weaponattack=9
set weapontype=melee
set weaponbonusattacktype=crush
set weaponrange=1


When someone types

equip Bronze_Axe

It'll check whether that file exists, then call it so that all the relevant information is updated.
What I'd like is for Bronze_Axe.bat to be Bronze_Axe.txt (or such) and for it to look like this -

Code: Select all

Bronze_Axe
An axe made of bronze.
9
melee
crush
1


then the equip.bat to loop through all the information and assign it to the neccasary variables.
This would decrease file size!
Also, I'd like to have a plugin system where the ObjectData folder is also under plugins, so people can put their own weapons, armor, etc into the game.
The only problem with this is that with the original method, people could put whatever batch code they want into the files... (now they can't)

How would you go about setting up the for loop to assign the data to the variables? (keeping in mind that it would need to check which folder the file is in because there are different variables for the armor files and so on).

THANKS SO MUCH FOR ALL THE HELP YOU GUYS HAVE GIVEN ME!!!!!!!!!!!!!!
reaper17







EDIT -

One more problem I've found -

drop.bat

Code: Select all

cls
IF NOT %1 == "" (
REM type Data\inventory.fldr | findtr /r /i /v "^%item%$" > Data\swap.fldr
REM move Data\swap.fldr Data\inventory.fldr >nul
echo YAY
pause
) else (
echo No item defined
pause
pause
)


If you don't send it a parameter, the cmd window closes?
reaper17

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 01:18
by foxidrive
Use this:

IF NOT "%~1"=="" (

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 01:30
by keyboard1333
Also -

Code: Select all

cls
IF NOT "%~1"=="" (
set "gotit="
for /f "delims=" %%a in (Data\swap.fldr) do if "%%a"="armour" set gotit=1
if defined gotit echo you have the armour.
pause
) else (
echo drop what?
pause
)


Even if I define a parameter, it still closes the window...
The other files work fine though.

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 01:41
by foxidrive
This is the most straightforward. It creates the file and then sets the variables.

Trying to save a few KB in a batch game isn't worth the effort of making the code more difficult.

Code: Select all

@echo off
(
echo weapon=Bronze_Axe
echo weaponinfo=An axe made of bronze.
echo weaponattack=9
echo weapontype=melee
echo weaponbonusattacktype=crush
echo weaponrange=1
)>Bronze_axe.txt

for /f "delims=" %%a in (Bronze_axe.txt) do set "%%a"

set weapon
pause

Re: Calling external file - cmd shuts

Posted: 24 Aug 2012 01:43
by foxidrive
reaper17 wrote:Also -

for /f "delims=" %%a in (Data\swap.fldr) do if "%%a"=="armour" set gotit=1

Even if I define a parameter, it still closes the window...
The other files work fine though.


You missed a character.