Page 1 of 1

saving and loading to a separate file using batch

Posted: 09 Jul 2013 10:24
by vvomble
Right so now I'm getting to grips with the fun that is batch programming I wanted to know if the following ideas are possible.

Can I create a text adventure which will have an inventory. The inventory should ideally be another text document or something similar maybe another batch file.

e.g Inventory list

9023 cash
1 spanner
3 screws
8 nails

I want to know how I'd make it so that when something is picked up it saves to another file and when inventory is loaded it loads that file or edits the file when an item is used or money is spent.

The other thing would be to have the ability to save your progress and load progress that you've made within the game.


Are these possible within batch and how would I code these. I don't need the whole code written I can normally figure these things out with simple examples.

Any help is much appreciated.

Re: saving and loading to a separate file using batch

Posted: 09 Jul 2013 14:14
by penpen
vvomble wrote:Can I create a text adventure which will have an inventory.
This depends on how complex your game should get.
A simple text adventures should be possible, if you mean the same with "text adventure" as i do.

vvomble wrote:The inventory should ideally be another text document or something similar maybe another batch file.

e.g Inventory list

9023 cash
1 spanner
3 screws
8 nails

I want to know how I'd make it so that when something is picked up it saves to another file and when inventory is loaded it loads that file or edits the file when an item is used or money is spent.
It bases on your game design, if this is really ideal.
The best in most cases is to store some things to a file and have others in memory.
It sounds that you should try to avoid disk access on these operations:
"picking up things", and "using items".

But nevertheless here are some examples for doing what you have asked for:

Code: Select all

:: saved file associations
set "PLAYER_PATH=playerPath"
set "INVENTORY_FILE=%PLAYER_PATH%\inventory.txt"
set "PICKUP_FILE=%PLAYER_PATH%\pickup.txt"

:: append text to the pickup file when "1 piece of something" has been picked up using your file syntax
(echo 1 something) >> PICKUP_FILE

:: edit the inventory file when 10 cash are spent:
set /a "CASH_AMOUNT=-10"

   :: assumed you can hold the following items in 32 bit signed integer only:
set /a "cash=0"
set /a "spanner=0"
set /a "screws=0"
set /a "nails=0"

   :: load inventory file and add to inventory
for /f "tokens=1,2" %%a in ('findstr "^" %INVENTORY_FILE%') do (
   set /a "%%b+=%%a"
)

   :: load pickup file and add to inventory
for /f "tokens=1,2" %%a in ('findstr "^" %PICKUP_FILE%') do (
   set /a "%%b+=%%a"
)

   :: change cash amount (10 cash spent)
set /a "cash-=CASH_AMOUNT"

   :: save inventory file
   set /a "%%b+=%%a"
(
   echo %cash% cash
   echo %spanner% spanner
   echo %screws% screws
   echo %nails% nails
) > %INVENTORY_FILE%

   :: delete pickup file, as all is in inventory file now
del %PICKUP_FILE%

vvomble wrote:The other thing would be to have the ability to save your progress and load progress that you've made within the game.
This is similar to edit the inventory.

penpen

Re: saving and loading to a separate file using batch

Posted: 09 Jul 2013 14:33
by Aacini
There are several ways to save and load a set of values to/from a disk fle. Perhaps the simplest one is save a set of variable asignments in a .bat file, because in this case nor the save code neither the load one must follow a certain order, that must be done when you use a text file instead. For example, to save the inventory:

Code: Select all

(
echo set cash=%cash%
echo set spanner=%spanner%
echo set screws=%screws%
echo set nails=%nails%
) > inventory.bat

To load the inventory saved by previous code:

Code: Select all

call inventory


Antonio

Re: saving and loading to a separate file using batch

Posted: 09 Jul 2013 14:35
by penpen
Btw: You should do something like object oriented programming.
This cannot be done fully, as batch has not real objects, but you can name variables as if you are programming this way: this increases clarity.
For example if your text adventure has multiple characters that you may controll, you should store their attributes in this way:

Code: Select all

set "CHARACTER.length=2"
set "CHARACTER[0].NAME=vvomble"
set "CHARACTER[0].HEALTH=100"
set "CHARACTER[0].STRENGTH=4"
set "CHARACTER[0].LUCK=8"
set "CHARACTER[1].NAME=vvombles pal"
set "CHARACTER[1].HEALTH=100"
set "CHARACTER[1].STRENGTH=3"
set "CHARACTER[1].LUCK=9"


And additionally you should make use of other batch files, functions, or when more skilled macros.
Aacini has showed how to use batch files.
How to use functions is explained in the DOS Batch - Function Tutorial available at this internet site.
If you want to use macros you should search this forum using the keyword macro.

penpen

Re: saving and loading to a separate file using batch

Posted: 10 Jul 2013 04:34
by vvomble
Thanks for all your help hopefully this will help me get started with what I'm aiming for. Which is a street racing text adventure where you can buy and tune up a car and race.

The plan is to build up the adventure with different batch files initially in separate parts and to help me keep track of the story line. Kinda like the example below.

(1) Story start
/ l \
(2a) option 1 (2b) option 2 (2c) option 3
/ \ / \ / \
(32a1) option 1 (32a2) option 2 (32ba1) option 1 (32ba2) option 2 (32ca1) option 1 (32ca2) option 2


And so on and so forth. Once each branch of the story is coded I'll then put each individual batch file into 1 batch file and have those save and load to another batch file and an inventory batch.

does the inventory have to load from a batch or text file just to be sure.

Re: saving and loading to a separate file using batch

Posted: 10 Jul 2013 05:18
by penpen
This depends on your decision how to load it:
If you want to do it as proposed by Aacini, then you have to load the inventory from a batch file (*.bat or *.cmd), and
if you want to do it as proposed by me, then you may use any file type you want.

penpen

Re: saving and loading to a separate file using batch

Posted: 17 Jul 2013 11:08
by Samir
vvomble wrote:Thanks for all your help hopefully this will help me get started with what I'm aiming for. Which is a street racing text adventure where you can buy and tune up a car and race.
I can sooooo help you test this as I run a car enthusiast web site, among other things. 8)