the following is offered as example of saving/loading game/app data as/from a single string along with parsing and assignment of data within appropriately.
meant to be a cross-platform solution, the string will save/load as SharedVariables()/cookies where appropriate.
code is lightly commented:
// Project: Save as String
// Created: 2022-11-02
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Save as String" )
SetWindowSize( 640,360, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1280,720 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
GLOBAL Device$ //Used to determine SaveSharedVariable or WriteLine/String()
Device$ = GetDeviceBaseName()
Type Stats //Some data to save
Health, Armor, Damage
EndType
GLOBAL Player as Stats
Load() // Load & Parse the String if it exists.
//Create Buttons to change data; Clicking will Increase & Save Player Stats
GLOBAL HealthBUT, ArmorBUT, DamageBUT, WriteBUT
HealthBUT = 1 : MakeHealth()
ArmorBUT = 2 : MakeArmor()
DamageBUT = 3 : MakeDamage()
WriteBUT = 4 : MakeWrite()
If Device$ = "html5" or Device$ = "android" //only on Desktop
SetVirtualButtonColor(WriteBUT, 128,0,0)
SetVirtualButtonActive(WriteBUT,0)
EndIf
do
If GetVirtualButtonPressed(HealthBUT) then INC_Health()
If GetVirtualButtonPressed(ArmorBUT) then INC_Armor()
If GetVirtualButtonPressed(DamageBUT) then INC_Damage()
If GetVirtualButtonPressed(WriteBUT) then OpenBrowser(GetWritePath() + "media")
Sync()
loop
Function INC_Health()
INC Player.Health, Random(1,3)
SetVirtualButtonText(HealthBUT,"Health" + CHR(10) + STR(Player.Health))
Save()
EndFunction
Function INC_Armor()
INC Player.Armor, Random(1,3)
SetVirtualButtonText(ArmorBUT,"Armor" + CHR(10) + STR(Player.Armor))
Save()
EndFunction
Function INC_Damage()
INC Player.Damage, Random(1,3)
SetVirtualButtonText(DamageBUT,"Damage" + CHR(10) + STR(Player.Damage))
Save()
EndFunction
Function MakeHealth()
AddVirtualButton(HealthBUT,100,100,100)
SetVirtualButtonText(HealthBUT,"Health" + CHR(10) + STR(Player.Health) )
EndFunction
Function MakeArmor()
AddVirtualButton(ArmorBUT,200,100,100)
SetVirtualButtonText(ArmorBUT,"Armor" + CHR(10) + STR(Player.Armor))
EndFunction
Function MakeDamage()
AddVirtualButton(DamageBUT,300,100,100)
SetVirtualButtonText(DamageBUT,"Damage" + CHR(10) + STR(Player.Damage))
EndFunction
Function MakeWrite()
AddVirtualButton(WriteBUT,500,100,100)
SetVirtualButtonText(WriteBUT,"Open" + CHR(10) + "Write$")
EndFunction
Function Load()
If Device$ = "html5" or Device$ = "android"
ThisSave$ = LoadSharedVariable("SaveGame", "missing")
Else
If GetFileExists("save.dat")
ThisFile = OpenToRead("save.dat")
ThisSave$ = ReadLine(ThisFile)
Else
ThisSave$ = "missing"
EndIf
EndIf
ParseSave$(ThisSave$)
EndFunction
Function ParseSave$(ThisSave$)
//if file/cookie exists, else set defaults and save
If ThisSave$ <> "missing"
Total = CountStringTokens(ThisSave$, "|") //total sets of paired data in string
For x = 1 to Total
ThisStat$ = GetStringToken(ThisSave$, "|", x)
//divide the paired data (stat:value) and assign
If GetStringToken(ThisStat$,":", 1) = "Health"
Player.Health = VAL(GetStringToken(ThisStat$,":", 2))
EndIf
If GetStringToken(ThisStat$,":", 1) = "Armor"
Player.Armor = VAL(GetStringToken(ThisStat$,":", 2))
EndIf
If GetStringToken(ThisStat$,":", 1) = "Damage"
Player.Damage = VAL(GetStringToken(ThisStat$,":", 2))
EndIf
next x
Else
Player.Health = 10
Player.Armor = 10
Player.Damage = 5
Save()
EndIf
EndFunction
Function Save()
//create respective pairs of data (stat:value)
Health$ = "Health:"+STR(Player.Health)
Armor$ = "Armor:"+STR(Player.Armor)
Damage$ = "Damage:"+STR(Player.Damage)
//combine individual data sets separated by pipes |
SaveString$ = Health$ + "|" + Armor$ + "|" + Damage$
If Device$ = "html5" or Device$ = "android"
SaveSharedVariable("SaveGame", SaveString$)
Else
ThisFile = OpenToWrite("save.dat")
WriteLine(ThisFile,SaveString$) //can use WriteString() to make "unreadable"
CloseFile(ThisFile)
EndIf
EndFunction
when exporting to HTML for use @ itch.io or anywhere using cross-site cookies, make sure to apply
THIS CHANGE to the .js player.
tested functional on WIN, Android and HTML with live demo
HERE where you can change values and Refresh to reload saved data.
for a more involved "real" game example, see
HERE with full source available
HERE.