saving and loading to a separate file using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vvomble
Posts: 9
Joined: 08 Jul 2013 07:07

saving and loading to a separate file using batch

#1 Post by vvomble » 09 Jul 2013 10:24

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: saving and loading to a separate file using batch

#2 Post by penpen » 09 Jul 2013 14:14

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

Aacini
Expert
Posts: 1888
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: saving and loading to a separate file using batch

#3 Post by Aacini » 09 Jul 2013 14:33

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: saving and loading to a separate file using batch

#4 Post by penpen » 09 Jul 2013 14:35

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

vvomble
Posts: 9
Joined: 08 Jul 2013 07:07

Re: saving and loading to a separate file using batch

#5 Post by vvomble » 10 Jul 2013 04:34

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: saving and loading to a separate file using batch

#6 Post by penpen » 10 Jul 2013 05:18

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

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: saving and loading to a separate file using batch

#7 Post by Samir » 17 Jul 2013 11:08

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)

Post Reply