TGC Codebase Backup



Saving Code by JMScomp

2nd Dec 2007 1:09
Summary

A code for saving and loading values in an external file.



Description

I originally created this code to save and load my program settings for use in different game levels. Then I modified it to show how it works.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem set save file location
File$ = "c:\text.txt"

rem make a blank file if none exists
If File Exist(file$) = 0 Then Make File "c:\text.txt"

rem load values from file
Open To Read 1,file$
Read String 1,value$
Read Byte 1,value2
Close File 1

rem print loaded value
Print "Current value:"
Print value$
Print value2


Print "Press a and s to change value1"
Print "Press d and f to change value2"
Print "Press q to run saver program"

rem begin program loop
Do

rem press q to initiate save function and a or s to change value
If Inkey$() = "q" Then gosub _saver_
If Inkey$() = "a" Then value$ = "1111"
If Inkey$() = "s" Then value$ = "2222"
If Inkey$() = "d" Then value2 = 1
If Inkey$() = "f" Then value2 = 0

rem end program loop
Loop

rem ******saver subroutine******
_saver_:
Cls

rem delete existing file
If File Exist(file$) = 1 Then Delete File file$

rem write new file
Open To Write 1,file$
Write String 1,value$
Write Byte 1,value2
Close File 1

rem print saved value
Print "Saved value:"
Print value$
Print value2

rem read new file and value
Open To Read 1,file$
Read String 1,value$
Read Byte 1,value2
Close File 1

rem print loaded value
Print "Loaded value1:"
Print value$
Print "Loaded value2:"
Print value2


Return