Saving data between sessions?

Let’s say that I want to remember what the player has clicked on between sessions. How would you go about saving that data? I would probably do something with player preferences with Unity, but is there a equivalent with needle?

Original Post on Discord

by user 95852307077406720

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Hi, you’re probably looking for something like Window: sessionStorage property - Web APIs | MDN or Window: localStorage property - Web APIs | MDN

In Needle Engine, to save data between sessions similar to how you would use PlayerPrefs in Unity, you can utilize the localStorage web API directly in your TypeScript code. The localStorage object allows you to save key/value pairs in a web browser and persists even when the browser is closed and reopened.

Here is an example of saving and loading data using localStorage:

// To save data:
localStorage.setItem('myDataKey', JSON.stringify(dataToSave));

// To load data:
const loadedData = JSON.parse(localStorage.getItem('myDataKey') || 'defaultValue');

Remember that localStorage only supports strings, so you need to serialize your data to a string when saving (e.g., using JSON.stringify) and deserialize it back to its original form when loading (e.g., using JSON.parse).

If you plan to save objects or arrays, you need to serialize them into a JSON string before storing them and deserialize them back into objects or arrays when you retrieve them.

1 messages left GPT 4
thread_dNO6QVPhuXdt0jXaNt3AJrev
Thanks for your feedback!

As the bot says ^^

Thank you @marcel :cactus: and bot :slightly_smiling_face:

by user 95852307077406720

feel free to click the green button if it helps :sweat_smile: