forums

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

08-16-2005 01:19

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Can anyone write an example of code that saves the position of an array of sprites, and also loads them?

08-16-2005 11:35

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Writing Array data to a file.

It's not that difficult, it just depends how you are storing your sprite data, but more on this in a minute...
First you need to either create or open the file, and to do this you need to know if it's already there:

openMyFile=File Exists(fileName)

(You don't need to set the directory as I'm assuming this is part of a save game function. If it's not you may want to save it to the 'Game' directory, check the documentation for 'Set Directory' in the 'files' section of the manual.)

writing to file
if openMyFile
fileID=Open File(fileName, true, "data", "Data")
else
fileID=Create File(fileName, "data", "Data)
end if

Set File Position fileID, 0


You are now ready to loop through the sprites, writing the data to the file. This is where your storage thing comes into play. I will give the methods, either a single multi-dimension array with x, y, and img, or three separate arrays, each holding one element (faster):

for spriteNum=0 to maxSprite 'where maxSprite is the last sprite to save -1. So if you have ten sprites, maxSprite will be 9
Multi-dimension (x=0, y=1, img=2 for example)
File Write fileID, spriteData[spriteNum, 0]
Set File Position fileID, File Position(fileID)+4
File Write fileId, spriteData[spriteNum, 1]
Set File Position fileID, File Position(fileID)+4
File Write fileID, spriteData[spriteNum, 2]
Set File Position fileID, File Position(fileID)+4
next spriteNum


For single arrays, replace spriteData[spriteNum, index] with spriteX | ...Y | ...Img[spriteNum]

You're done! Just close the file:

Close File fileID

reading from a file
When reading the data back, you replace 'File Write' with 'File Read Int', but there are a couple of things to be wary of. Make sure you read in the same order as you write. You also need to watch out for array size. If you write different numbers of sprites each time you need to check how many there are in the file first, this is found with:

maxSprite=File Length(fileID)/12

Make sure you check this first and set your arrays to hold that ammount of data. Of course if there are always ten sprites you don't have a problem.
Another point, if you're reading back you wont create a file if 'openMyFile' returns a false, just set to default values.

Hope this helps

Danny (nods)

08-16-2005 14:23

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

I still need a little more help

Here's what I made out of your help:

procedure Save()
Int i,Exists=File Exists("game1")
If Exists
game1data=Open File("game1", true,"text","text") 'Is this ment to be 'text'?
else
game1data=Create File("game1","text","text")
end if
Set File Position game1data,0

For i=0 to 99
File Write game1data, x
Set File Position game1data, File Position(game1data)+4
File Write game1data,y
Set File Position game1data, File Position(game1data)+4
next
Close File game1data
end proc


When it gets to the line where it says ' Set File Position game1data, File Position(game1data)+4' there's an unexpected end of file.

08-16-2005 23:42

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Nearly right...

No, it's not supposed to be 'text', it should be 'data'. You are writing integer values, not 'text strings', an int is four bytes (hence the +4) and a string is initially an unknown length. Very different animals! The file type, the first string, can be pretty much anything you like BTW.
Where are you getting 'x' and 'y' from in your example? What are they? There is no array reference, you don't actually use the 'i' value in the loop so nothing changes. How do you hold the data? One big array? X and Y arrays? If it's the latter, try File Write game1data, x[index] (I can't type 'i' in braces as it thinks I want 'italics!), and set the format string to "data". This should write one long word of data (four bytes) at byte zero (the start of the file), set the file position to the end of those bytes (still within the file, so no EOF), then write the next four. Give this a go, if you still have problems then post again with your actual code and I'll see what I can do.
Good Luck!

Danny (nods)

08-16-2005 23:59

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Oops!

Just noticed you are using a 'Proc', so remember to make the arrays 'Global' or 'Shared'.

08-17-2005 00:50

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Here's all the code:


You might want to copy it into Hieroglyph and run it, be sure to add one graphic to bank 128.


fade down
Graphics mode 1024,768
Global int n,game1,game1data
Global float a,b,x[100],y[100]
Load images 128
fade up
While not pressed(12)
for i=0 to 99
x[n]=x[n]+random(1,8)
y[n]=y[n]+random(1,8)
sprite i,x[n],y[n],1
next

if space then save()
draw frame
Wend
Fade down
text mode
fade up

procedure Save()
Int n,Exists=File Exists("game1")
If Exists
game1data=Open File("game1", true,"data","data")'Error: The file could not be opened because it is the wrong type.
else
game1data=Create File("game1","data","data")
end if
Set File Position game1data,0

For i=0 to 99
File Write game1data, x[n]
Set File Position game1data, File Position(game1data)+4
File Write game1data,y[n]
Set File Position game1data, File Position(game1data)+4
next
Close file game1data
end proc

08-17-2005 11:25

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

All sorted!

Hi Jacob, right, got it sorted. Two problems here, the file being the incorrect type is because you have saved a file with the "text" format. It's in [home]:Library:Preferences. Delete this and you wont get the error (a new file will be created with the correct "data" type and all will be well with the world).
The second 'boob' was mine, Sorry, I was getting a bit too clever. File Write actually advances the file pointer as part of the opperation, so there is no need to set the position manually. The pointer started at zero, a long (4 bytes) was written and the pointer advanced to position 4. I then told you advance it again (to position 8, which is past the end of the file. One Error in the making!).
Sorry about that, my bad.
There were a couple of other little things, unused variables, using floats for the sprite positions and stuff. The complete code (tested and working) follows:

fade down
Graphics mode 1024,768
int n
Global int x[100],y[100]
Load images 128
fade up

While not pressed(12)

for n=0 to 99
x[n]=x[n]+random(1,8)
y[n]=y[n]+random(1,8)
sprite n,x[n],y[n],1
next

if space then save()

draw frame
Wend

Fade down
text mode
fade up

procedure Save()
int game1data, n
Int Exists=File Exists("game1")

If Exists
game1data=Open File("game1", true,"data","data")
else
game1data=Create File("game1","data","data")
end if

Set File Position game1data, file length(game1data)
text mode

For n=0 to 99
File Write game1data, x[n]
'Set File Position game1data, File Position(game1data)+4
File Write game1data,y[n]
'Set File Position game1data, File Position(game1data)+4
next

Close file game1data
end proc

08-17-2005 11:39

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Oopsy... (again!)

Left some of my test code in there. The 'Final' save() proc should be:


procedure Save()
int game1data, n
Int Exists=File Exists("game1")

If Exists
game1data=Open File("game1", true,"data","data")
else
game1data=Create File("game1","data","data")
end if

Set File Position game1data, 0

For n=0 to 99
File Write game1data, x[n]
File Write game1data, y[n]
next

Close file game1data

end proc


This works as is. All you need to do now is put the loader code in place and away the lads! Post here if you need anything else and good luck.

Danny (nods)

08-17-2005 12:23

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

It works!

Thank you for the code.
Now I know how to read and write files. : )

08-17-2005 12:37

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

You're Welcome!

We get there in the end ;)

Danny (nods)

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.