forums

TNT Basic Forums > Programming Q&A
Saving Game Feature
< Last Thread     Next Thread >
Author
Thread        Post A Reply

11-03-2005 22:42

Posted by:
Maher Haddad

Find more posts by Maher Haddad

Hi,

I need to add a Save Game Feature to an Adventure Game that I've been working on but I'm bad at programming when it comes to working with extra files.

What I want to do is create a file (or replace one if it exists - for when resaving the game) that stores the values for all the integers being used in the game.

Then I need to know how I can load the file and use all the integers for the game.

Please let me know what needs to be done for that.

Thank You.

Maher
www.Handini.com

11-03-2005 23:00

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

hi, here you can find about how file IO is dealt with in TNT. I'm not 100% sure this can be done in TNT, but i guess you could just open the file, use the write command to append vaiables to your file - and do the same thing to reopen your game, just by reading the variables in the same order you created them (i've not done that much IO in TNT so i'm not 100% sure it ca be done so easily- probably someone else could confirm)

11-03-2005 23:05

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

i meant:
in the same order you added them

not:
in the same order you created them



(it would be great if we could edit posts!!!)

11-04-2005 02:54

Posted by:
Maher Haddad

Find more posts by Maher Haddad

Thank you for your speedy reply.

My problem is that when I try using these commands they don't work.

I wish there was an example of how to use them rather than just have the syntax listed.

11-04-2005 07:45

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Try here...

There was a similar question a while back, it was about saving an array of sprite data, but the theory is the same. You'll need to read the full thread though (I kind of messed up a couple of times... my bad). Hope this helps. The thread can be accessed by clicking here.
Have fun.

Danny (nods)

11-04-2005 15:36

Posted by:
Maher Haddad

Find more posts by Maher Haddad

Thank you for showing me where that post is. I played around with it but could not figure out how it works and when trying to manipulate it to integers was unsuccessful.

Here is some code that I've been trying to test out:

int Test
int Result
int Reg = 10
int Deg = 10

set directory user preferences

if file exists ("Testing") = true then Print "True"
if file exists ("Testing") = false then Print "False"

Print Reg
Print Deg

if file exists ("Testing")
Test=open file("Testing",true,"pref")
else
Test=create file ("Testing","pref","data")
end if

Result = File Read Int (Test)

Print Reg
Print Deg

file write Test, Reg
file write Test, Deg


To make sure it works I set the integers Reg and Deg to the same value and run it.

What I'm trying to do with this test is have it save the values of the integers. Then if I were to replace their values and run the code again it should print both the old values (that should have been saved) as well as the new values.

When I run this code it just prints the new values and doesn't seem to either save or load (or both) the integers into the file that is created.

11-04-2005 17:45

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

OK...

Well I can sort of see what you're trying to do. You're getting the calls right, you're just not getting the logic bit right.

Here, in pseudocode, is what I think you should be trying to do:

declare an int, and set it to 10.
test for the existance of a file.

if file exists, print "exists" and open the file.
else print "nope!" and create the file.

print int, should be "10".
write int to file.
set int to 20.
print int, should now be "20"
set file pointer to start of file (as the 'write' will advance the pointer).
read int.
print int, should now be "10" again.
close file.


This will prove that the values are being written, and read back, correctly. Give this a go, take each of the lines of pseudocode and translate them to basic (using the calls you correctly used last time). You should get the results that I think you're trying to achieve.

Good luck,

Danny (nods)

11-05-2005 05:11

Posted by:
Maher Haddad

Find more posts by Maher Haddad

Thank you for the suggestion. It seems similar in style to some code found in the following forum: http://tntbasic.com/community/forums/showthread.php?threadid=1029

I modified this code to save two integers to test it out and got the following:

int bestand
int Q = 5
int R = 5
Print Q
Print R
set directory user preferences
if file exists ("Testing")
bestand=open file("Testing",true,"pref")
else
bestand=create file ("Testing","pref","data")
end if

bestand=file read Int (Q)
bestand=file read Int (R)

Print Q
Print R

file write bestand, Q
file write bestand, R

When I check the syntax everything is fine but when I run it I get the following error:

*** Runtime exception caught ***
Line# : 13
Code : bestand=file read Int (Q)
Error : The file is not currently open.

I'm not sure what is wrong since the file is supposed to be open.

11-05-2005 11:59

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Nearly right...

You've got a couple of errors there. The read code should be:


Q=file read int(bestand)
R=file read int(bestand)

NOT

bestand=file read Int (Q)
bestand=file read Int (R)


Your second error is harder to spot, but just as important. If you go through the code and see what it's doing, you'll notice that if the file does not exist, you create one. This file is empty, but the next thing you try to do is read two ints from it. It's not going to work. Go back to my pseudocode example and see the difference. Set the values, print, save to file and change the values, print, load the old values (remembering to move the file pointer) and print again.

Finally, a word of warning. When you are finished with a file you should close the pipe. Use 'close file bestand' after you're done.

So you're nearly there, next one should do it! ;)

Danny (nods)

11-05-2005 13:55

Posted by:
Maher Haddad

Find more posts by Maher Haddad

Thank You! - It Now Works

Thank you for helping me with files. I looked at your corrections and was able to make it work.

Here is the finished code:


int Q = 50
int R = 50
int bestand

Print Q
Print R

if file exists ("Testing")
bestand=open file("Testing",true,"pref")
Print "Exists"
else
bestand=create file ("Testing","pref","data")
Print "Nope"
end if

print Q
print R

file write bestand, Q
file write bestand, R

Q = 20
R = 20

print Q
print R

bestand=open file("Testing",true,"pref")

Q=file read int(bestand)
R=file read int(bestand)

print Q
Print R

close file bestand



What I did to make sure this worked is that I first ran the file.

I then changed the integers at the very top and deleted the following code to test that it did indeed save the integers to the file:

file write bestand, Q
file write bestand, R

Then, when I ran it again the last couple set of integers were indeed the one's that were saved to the file.

Thank you for your help with getting the code straightened out.

Maher

11-05-2005 14:02

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

You're welcome!

No worries, anything else you need just drop us a line.

Danny (nods)

11-06-2005 20:02

Posted by:
Maher Haddad

Find more posts by Maher Haddad

No Problem.

Maher

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.