1.

Solve : Creating a game in DOS - Saving game to a file??

Answer»

Hi guys, just for the *censored* of it i WANT to make a text-based adventure game in MSDOS, I was wondering, is there a way I can save the users game progress to a file?

For example:
I have a piece of code which asks the user to pick a Class, so how could I now save this to a file and then call upon it later?

Thank you! save the variables to a file
Code: [Select]>>savedgame.txt echo CLASS=%CLASS%
reload the variables from the file
Code: [Select]FOR /F "delims=" %%G in (savedgame.txt) do SET %%GThis caught my interest... not hijacking this thread, but just wanted to inquire for additional info on this which might help Tomwatts with his game design.

When dealing with multiple variables written for a single file to write to, what is the best method of reading back the data in batch?

Such as if you had:

Class = Human
Level = 3
Gold = 2400
WeaponMinDamage= 5
WeaponMaxDamage= 10
Armor = 23
XLocation = 6
YLocation = 84


and had a file appended such as SavedGame.log with

Quote

Human
3
2400
5
10
23
6
84

Would it be better to be dealing with delimited data such as
Quote
Human,3,2400,5,10,23,6,84

In which you know that the whole of the data contained between comma delimiters should be used so that you don't have to read in an unknown length of characters?

And if the best method is to use a delimited data file, could an example be given on how to best implement this? As well as if just appended data is best to have each variables VALUE on a separate line and it doesnt matter of length because of character line isolation from the following lines in which there is an instruction to read an entire line to end of line such as maximum allowed character length per line, an example of implementing that?

If you are going to use delimited data then you need to make sure all separated values also have quotes because the FOR command will drop empty tokens.
I prefer the method I used above.  MAKES it REAL easy to define the variable.

But you could also do a flat file with one line for each variable and redirect the file into a code block.

Code: [Select]< savedgame.txt (
set /p class=
set /p level=
set /p gold=
set /p weapondamage=
)Thank you so much for that! That's worked a treat!

Another thing - How could I create some field validation for the character selection page?

set /p input=
if %input% == 1 goto Warrior
if %input% == 2 goto Hunter

so at the bottom i'd have something like..

if %input% == 3> echo Invalid Selection

Any ideas? Or am i thinking into this too much?And thank you very much guys!Use the CHOICE command if you are using the batch file on Windows versions greater than XP.  Windows XP did not have the choice command.  The choice command will not continue until you enter in a valid choice.Hello!

Firstly my APOLOGIES for the rubbish reply, been so busy with work the past few days!

The CHOICE command works perfectly, such a nice solution to the problem I was having! I'll be implementing this!

It's great having a community to talk to about this, i'm sure when i'm done you'd like a copy? If so, i'll make you get one.

The next thing I am going to do is implement a data file so I can have an 'Inventory' of sorts too!

I'm not using Windows XP btw, i'm using Win 7, but for some reason my profile says Win XP, i need to change this.

Regards,
Tom



Discussion

No Comment Found