Code archives/File Utilities/Simple File Save, File Load

This code has been declared by its author to be Public Domain code.

Download source code

Simple File Save, File Load by wizzlefish2004
This is my first entry to the code archives, and it will be very simple. This code saves the camera's x, y, and z location as integers, and loads it too.
[code]
;Function SaveGame(camerahandle)
;Saves the game
;Perimeters:
;camerahandle - the handle of the camera you want to save the position of.
Function SaveGame(camerahandle)
  
  ;set up the screen
  Cls
  Locate 0,0
  ;let user choose what to save game as
  filesave = Input("Save game as - ")
  filesave = filesave + ".dat"

  ;write files to dat file
  fileout = WriteFile(filesave)
  x = EntityX(camerahandle)
  y = EntityY(camerahandle)
  z = EntityZ(camerahandle)
  ;enter any other variables included in any games here.

  x = WriteInt(fileout)
  y = WriteInt(fileout)
  z = WriteInt(fileout)
  ;for extra variables, use "WriteInt," "WriteString," and "WriteByte" accordingly.
  
  ;close file
  CloseFile(fileout)

End Function
  
;Function LoadGame(camerahandle)
;Loads a previously saved game
;Perimeters:
;camerahandle - handle of the main camera
Function LoadGame(camerahandle)

  ;set up the screen
  Cls
  Locate 0,0
  
  ;Let user load game
  fileload = Input("Load file - ")
  fileload = fileload + ".dat"

  ;Open file for reading
  filein = ReadFile(fileload)
  
  ;load info
  x = ReadInt(filein)
  y = ReadInt(filein)
  z = ReadInt(filein)
  ;read all the other data you saved
  ;make sure it is in the same order you saved it as
  
  ;close the file
  CloseFile(filein)
  
  ;position camera
  PositionEntity camerahandle, x, y, z

  SetUp() ; some function to set up everything for your game.

End Function

;(c)2004 No Enemies Games (not really)

Comments

N2004
1. Write the X/Y/Z positions as floats. That's how they're stored internally (maybe they're stored as doubles, but I sure as hell know that they aren't integers).

2. WriteInt/Float/Short/etc. do not return anything of value. Don't bother doing 'x = WriteInt(file,val)', just make the call to the function and forget about assigning the variables a value.

3. This code will not work. Plain and simple, you apparently didn't test it before uploading. Write functions take two arguments at minimum: stream and value. In your case, you're writing an integer without passing the value of the integer to the function.


wizzlefish2004
Yeah, nothing I do works. Oh well.


Code Archives Forum