Ooops
February 27, 2021, 03:40:12 AM

Author Topic: [bb] Simple File Save, File Load by wizzlefish [ 1+ years ago ]  (Read 568 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Simple File Save, File Load
Author : wizzlefish
Posted : 1+ years ago

Description : 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 :
Code: BlitzBasic
  1. [code]
  2. ;Function SaveGame(camerahandle)
  3. ;Saves the game
  4. ;Perimeters:
  5. ;camerahandle - the handle of the camera you want to save the position of.
  6. Function SaveGame(camerahandle)
  7.  
  8.   ;set up the screen
  9.   Cls
  10.   Locate 0,0
  11.   ;let user choose what to save game as
  12.   filesave = Input("Save game as - ")
  13.   filesave = filesave + ".dat"
  14.  
  15.   ;write files to dat file
  16.   fileout = WriteFile(filesave)
  17.   x = EntityX(camerahandle)
  18.   y = EntityY(camerahandle)
  19.   z = EntityZ(camerahandle)
  20.   ;enter any other variables included in any games here.
  21.  
  22.   x = WriteInt(fileout)
  23.   y = WriteInt(fileout)
  24.   z = WriteInt(fileout)
  25.   ;for extra variables, use "WriteInt," "WriteString," and "WriteByte" accordingly.
  26.  
  27.   ;close file
  28.   CloseFile(fileout)
  29.  
  30. End Function
  31.  
  32. ;Function LoadGame(camerahandle)
  33. ;Loads a previously saved game
  34. ;Perimeters:
  35. ;camerahandle - handle of the main camera
  36. Function LoadGame(camerahandle)
  37.  
  38.   ;set up the screen
  39.   Cls
  40.   Locate 0,0
  41.  
  42.   ;Let user load game
  43.   fileload = Input("Load file - ")
  44.   fileload = fileload + ".dat"
  45.  
  46.   ;Open file for reading
  47.   filein = ReadFile(fileload)
  48.  
  49.   ;load info
  50.   x = ReadInt(filein)
  51.   y = ReadInt(filein)
  52.   z = ReadInt(filein)
  53.   ;read all the other data you saved
  54.   ;make sure it is in the same order you saved it as
  55.  
  56.   ;close the file
  57.   CloseFile(filein)
  58.  
  59.   ;position camera
  60.   PositionEntity camerahandle, x, y, z
  61.  
  62.   SetUp() ; some function to set up everything for your game.
  63.  
  64. End Function
  65.  
  66. ;(c)2004 No Enemies Games (not really)


Comments :


N(Posted 1+ years ago)

 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.


wizzlefish(Posted 1+ years ago)

 Yeah, nothing I do works. Oh well.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal