Advanced game related topics
User avatar
DanLJr
 
Posts: 54
Joined: Wed Jul 04, 2018 4:24 am
Location: USA

Using #PB_LocalStorage with files??? (RESOLVED)

by DanLJr Fri Sep 20, 2019 10:04 pm

I'm having trouble utilizing local storage to save and load file data. I have it working perfectly with Local File saving a downloaded ,txt file (using the requester to re-upload) but when I try to do it through the browser cache (using #PB_LocalStorage) and not going through the requester, it SEEMS to create my file when I save (my code reports it worked - no errors - in the callback) but when I try to load using a fixed filename and the #PB_LocalStorage, it does not seem to reload my file at all...it's like it's just not there.

Code: Select all
If CreateFile(0, #WorkingSavedGamesFilename$, @SavedGamesWriteCallback()) ; we create a new text file and run the callback to populate and export
  WriteString(0, WorkingFileData$) ;dump content into file
  ExportFile(0, "text/plain")
  CloseFile(0)
  ProcedureReturn "Ok"
EndIf


The above code creates a download file just fine.

Code: Select all
Procedure SavedGamesReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
   
    Protected WorkingImportText$ = ReadString(0, #PB_Ascii + #PB_File_IgnoreEOL)
   
    CloseFile(0)
    CloseWindow(#WindowFileRequester)
           
    WriteUserTextConsole(SaveGameS(#LoadFromSavedGamesDownloadFile$, #False, WorkingImportText$))
           
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure OpenFileRequesterCallback()
  If NextSelectedFile()
    OpenFile(0, SelectedFileID(), @SavedGamesReadCallback(), #PB_LocalFile)
  EndIf
EndProcedure
 
Procedure Event_ChooseFile()
  OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure

Procedure SelectSavedGameFileToRestore()

  OpenWindow(#WindowFileRequester, 0, 0, 300, 50, "Load " + #GameTitle$ + " Saved Games from File", #PB_Window_ScreenCentered)
    ButtonGadget(#ChooseFileButton, 10, 10, 280, 30, "Choose a file...")
 
    BindGadgetEvent(#ChooseFileButton, @Event_ChooseFile())
 
EndProcedure


The above code loads a previously downloaded file just fine. (Requires the user to pick the file from the dialog, and then it proceeds to load it, no problem.)

HOWEVER, If I modify the above code to use the same fixed file name both for reading and writing along with #PB_LocalStorage and no file selector dialog for loading, it simply does not load the file from the cache.

Could someone give me a working example of reading and writing a fixed filename (will always be the same file; can just overwrite each time) using #PB_LocalStorage so that the data is stored within the browser cache? I've tried every possible iteration of this following the help files and such for SB, and while I was able to get it working with the Local Files (which actually required more code/callbacks, etc. I was never able to make it work with the #PB_LocalStorage way.) Currently, my save slots exist in RAM, and I can manually export and import those save slots as a file (like a backup/archive) but I cannot persist in the cache, which is what I REALLY need to be doing here. (The downloading and uploading is more for archive/backup of save slots. I need "real-time" browser caching to really make this practical, so if they close, they can reload from saved slots without having to have made a manual export/download file to reload.)

Any help would be highly appreciated!
Last edited by DanLJr on Sat Sep 21, 2019 9:15 pm, edited 1 time in total.
-Dan L.
:: falling on my face a lot lately; learning even more because of it! ::
User avatar
DanLJr
 
Posts: 54
Joined: Wed Jul 04, 2018 4:24 am
Location: USA

Re: Using #PB_LocalStorage with files???

by DanLJr Sat Sep 21, 2019 9:14 pm

I eventually went "back to the drawing board" and re-wrote a test version from scratch. (Starting with a few essentials from my other code and the help files.) I got it to work.

Here is a working standalone example of using LocalStorage to read and write data using the browser cache (it's a simple "write it then read it" example, but it proves the concept!):

Code: Select all
EnableExplicit

#WorkingSavedGamesFilename$ = "skytest"
Global WorkingFileData$ = "This is a test."
#FileToWrite = 0
#FileToRead = 1

Procedure SavedGamesReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
   
    Protected WorkingImportText$ = ReadString(#FileToRead, #PB_File_IgnoreEOL)
    CloseFile(#FileToRead)
           
    Debug WorkingImportText$
           
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure SavedGamesWriteCallback(Status, Filename$, File, SizeRead)
 
  Select Status
    Case #PB_Status_Saved
      ; File correctly saved
      Debug "The file: '" + #WorkingSavedGamesFilename$ + "' has been created!"
           
      OpenFile(#FileToRead, #WorkingSavedGamesFilename$, @SavedGamesReadCallback(), #PB_LocalStorage)     
     
    Case #PB_Status_Error
      Debug "The file: '" + #WorkingSavedGamesFilename$ + "' could not be created."
  EndSelect
   
EndProcedure

Procedure Main()
  If CreateFile(#FileToWrite, #WorkingSavedGamesFilename$, @SavedGamesWriteCallback(), #PB_LocalStorage) ; we create a new text file and run the callback to populate and export
    Debug "Initialized creation of file: '" + #WorkingSavedGamesFilename$ + "'. Success! Awaiting callback..."
    WriteString(#FileToWrite, WorkingFileData$) ;dump content into file
    ;ExportFile(#FileToWrite, "text/plain")
    CloseFile(#FileToWrite)
  Else
    Debug "Could not create file: '" + #WorkingSavedGamesFilename$ + "'. Failed."
  EndIf
   
EndProcedure

Main()

If 1 = 2
  SavedGamesReadCallback(0, "", 0, 0)
  SavedGamesWriteCallback(0, "", 0, 0)
EndIf
-Dan L.
:: falling on my face a lot lately; learning even more because of it! ::
Return to Game Programming

Who is online

Users browsing this forum: No registered users and 0 guests