I have noticed a lot of requests about how to save your data and retrieve it. So I thought I would post a little example to try to show how easy it can be. The following code is a very basic program that lets you move a square around the screen. Press Enter and you will save your position. Exit the game reload it and hey presto you are in the same position you left it at!
Rem Project: load save example
Rem Created: Sunday, November 07, 2010
Rem ***** Main Source File *****
set display mode 1024,768,32,1
sync on
sync
cls rgb(255,255,0)
get image 1,0,0,32,32,1
sprite 1,0,0,1
dim map(2)
cls 0
x=100
y=100
if file exist("map.dat")=1
load array "map.dat",map()
x=map(1)
y=map(2)
ENDIF
do
gosub keychecks
gosub update
sync
loop
keychecks:
if returnkey()=1 and rep=0
save array "map.dat",map()
ENDIF
if upkey()=1
dec y
endif
if downkey()=1
inc y
endif
if leftkey()=1
dec x
endif
if rightkey()=1
inc x
endif
if scancode()=0
rep=0
ENDIF
return
update:
sprite 1,x,y,1
map(1)=x
map(2)=y
return
You can do this using other methods, such as open to write. But if you are struggling to get your head round opening and closing files and reading them in, arrays are far easier, and slightly quicker to setup and use. All I do in this program is to first create my image and data components, then check if the map.dat file exists. If it doesn't it uses default settings and runs the game. If it does it loads the array, puts the arrays values into the x and y variables, and runs the game. Deleting the array file (map.dat) manually or with a little more code will cause automatic reset, so can be used for reset purposes within the game also.
Obviously you can expand on that array or add more of them, just remember to load all your array data back into the required variables and vice versa in game. Or just use the array directly instead of using temp variables, which I prefer not to do on the whole.
I hope this helps anyone struggling with saving their games. Any questions or comments, I will try to help if anyone does not understand my attempt to explain the basics.
http://s6.bitefight.org/c.php?uid=103081