Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize all save slots and deserialize them without loading a specific slot #305

Closed
sizhongbin opened this issue Jun 27, 2024 · 7 comments
Assignees
Labels
done Issue is resolved enhancement New feature or request

Comments

@sizhongbin
Copy link

Is your feature request related to a problem?
I'm working on a game with cloud save feature based on Supabase database. The process in my plan should be like:
:: Start
User login with useid and password. If successd, goto :: LoadGame

:: LoadGame

  1. User select one of the three save slots and load it from localStorage. Or, upload/download all the three save slots to/from database.
  • Load from localStorage: Save.slots.load(slot)
  • Upload: Serialize all the 3 slots as a JSON string like: {"slot0":"Slot 0 Serialized String","slot1":"Slot 1 Serialized String","slot2":"Slot 2 Serialized String"}, and INSERT/UPDATE the JSON to database with userid.
  • Download: SELECT the JSON by userid from database and deserialize all the 3 slots. Cover the current localStorage. Then goto :: LoadGame again. Now user can select one of the three save slots that just downloaded and deserialized to localStorage.

The problem is that serialize/deserialize feature only works on the current slot. And a load will be immediately processed after deserialize.

Describe the solution you'd like.
Save.serializeAll(): Return a JSON string like: {"slot0":"Slot 0 Serialized String","slot1":"Slot 1 Serialized String","slot2":"Slot 2 Serialized String"}
Save.deserializeAll(string): Deserializes the given save string created via Save.serializeAll() and cover the current localStorage.

Describe alternatives you've considered.
Maybe some other solutions using the current Serialization API?

Additional context.
Nope.

@sizhongbin sizhongbin added the enhancement New feature or request label Jun 27, 2024
@tmedwards
Copy link
Owner

Unless I'm mistaken, this is already part of v2.37.0.

@sizhongbin
Copy link
Author

Unless I'm mistaken, this is already part of v2.37.0.

Thanks for quick reply.
I've checked the 2.37.0 document. Looks like the serialize
ion API is replaced by base64. But I cannot find any difference except the Promise return. I'm still only able to encode the current save slot or load it immediately. I cannot encode/decode ALL the slots. Did I miss something?

@tmedwards
Copy link
Owner

Yes. Check out the documentation on Save.browser.export() and Save.browser.import().

@sizhongbin
Copy link
Author

Yes. Check out the documentation on Save.browser.export() and Save.browser.import().

Oh i figured out. Yes the import/export can backup/restore all the slots to/from a save file. But unfortunately, what i need is a JSON string so that i can SELECT/INSERT/UPDATE it in postgresSQL easily.

I guess I should create my own save/load function instead of using the brower save API, so that I can set/get the JSON string I want to/from localStorage and do something before loading.

@tmedwards
Copy link
Owner

tmedwards commented Jun 28, 2024

I suppose I could add .exportToBase64() and .importFromBase64() methods or something similar.

@tmedwards
Copy link
Owner

Made some changes:

  • Moved Save.browser.export() and Save.browser.import() to Save.disk.export() and Save.disk.import().
  • Added Save.base64.export() and Save.base64.import().

The latter two are what you should need.

@tmedwards tmedwards self-assigned this Jun 28, 2024
@tmedwards tmedwards added the done Issue is resolved label Jun 28, 2024
@sizhongbin
Copy link
Author

Great! Thanks a lot. I'll try it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done Issue is resolved enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants