TGC Codebase Backup



Using Files by TKD guy

20th Jul 2007 13:24
Summary

import/export info via files (level editor) Please note that I didn't originally write this code and take no credit for it. The person who posted it didn't know who wrote it either



Description

Import and export media using an outside file as a medium for two programs. Ideal for the programming of a map editor.
Note, the command "open file to write" actually makes a brand new file, so it won't work if the file that your trying to open already exists. To edit, you'll have to open an existing file with "open file to read", copy that data to a variable in-program then close the file, delete it, the "open file to read" (put filename here).
What's that, you say that's redundant and stupid. Well, so do I, but I don't make the rules. I just use em'. Code On Programmers!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Project: file in out
REM Created: 31/12/2006 22:18:33
REM
REM ***** Main Source File *****
REM
type HealthPack
 File as string
 x as float
 y as float
 z as float
 Rotate as byte
endtype

dim Item(100) as HealthPack

Item(1).File="HealthPack.x"
Item(1).x=223
Item(1).y=223
Item(1).z=0
Item(1).Rotate=1

Item(2).File="AmmoClip.x"
Item(2).x=127
Item(2).y=340
Item(2).z=20
Item(2).Rotate=1

Item(3).File="StopSign.x"
Item(3).x=427
Item(3).y=140
Item(3).z=0
Item(3).Rotate=0

` Check for the data file and delete it
`  In case you want to edit the array and re-run it
if file exist("TestData.dat")
 delete file "TestData.dat"
endif

` Save the data
open to write 1,"TestData.dat"
for t=1 to 3
 write string 1,Item(t).File
 write float 1,Item(t).x
 write float 1,Item(t).y
 write float 1,Item(t).z
 write byte 1,Item(t).Rotate
next t
close file 1

` Clear the data
for t=1 to 3
 Item(t).File=""
 Item(t).x=0
 Item(t).y=0
 Item(t).z=0
 Item(t).Rotate=0
next t

` Show the data (to show that the array is empty)
print "Data cleared"
print ""
for t=1 to 3
 print "Item("+str$(t)+")"
 print " Filename = "+Item(t).File
 print " x coordinate = "+str$(Item(t).x)
 print " y coordinate = "+str$(Item(t).y)
 print " z coordinate = "+str$(Item(t).z)
 print " Rotate = "+str$(Item(t).Rotate)
 print ""
 print ""
next t

wait key

` Read the data
open to read 1,"TestData.dat"
for t=1 to 3
 read string 1,Item(t).File
 read float 1,Item(t).x
 read float 1,Item(t).y
 read float 1,Item(t).z
 read byte 1,Item(t).Rotate
next t
close file 1

` Show the data (after its been loaded from the file)
cls
print "Data loaded from file."
print ""
for t=1 to 3
 print "Item("+str$(t)+")"
 print " Filename = "+Item(t).File
 print " x coordinate = "+str$(Item(t).x)
 print " y coordinate = "+str$(Item(t).y)
 print " z coordinate = "+str$(Item(t).z)
 print " Rotate = "+str$(Item(t).Rotate)
 print ""
 print ""
next t

wait key