How do I set a WebStorage.LocalValue name to a variable name

Get help using Construct 2

Post » Tue Apr 05, 2016 11:06 pm

What I have:
A text box where the player will input its ID.

What I need:
A way to store that player score based on its ID.
e.g. >> ID.Text = Logan77 | Score = 100
>> WebStorage.LocalValue(ID.Text+"_Score")
>> will turn into WebStorage.LocalValue("Logan77_Score")
>> then when needed, store Logan's current score to his created WebStorage key.

Maybe a better way to ask it would be: Is it possible to create a variable name based on another variable value? Or even: How can I store data per UserID?

I've experience with languages like C#, JS/jQuery, LUA, Python, F#, etc, so I think that something like this can be done with JSON, but how I started with Construct 2 today, I don't have a clue about what to do here :(
B
4
Posts: 3
Reputation: 206

Post » Wed Apr 06, 2016 2:44 am

You might be able to store information in a Dictionary object or Array, then save it as JSON to the webstorage. Then you can load it from the stored JSON data
B
84
S
22
G
15
Posts: 1,039
Reputation: 15,249

Post » Wed Apr 06, 2016 6:03 am

Yeah, if I could save a JSON file with the UserID as its name (e.g. Logan.JSON) and load test if it exists and then load it or create it depending on the result, that would be easy.
B
4
Posts: 3
Reputation: 206

Post » Wed Apr 06, 2016 7:03 am

When you save stuff you have to name it. You can just put a string inside the name. Something like "Save_File_" & username

I've made a profile system where I used a number instead to differentiate a number of save slots in this way.
B
50
S
14
G
4
Posts: 189
Reputation: 5,508

Post » Wed Apr 06, 2016 4:52 pm

Here's what I would do, others might disagree.

If the ID text box will always be on the screen, you can use Set local key ID.Text & "_Score"
If the text box will not be there when a score is submitted, you will have to save the name to a variable, maybe called CurrentPlayerName. So you would set CurrentPlayerName = ID.Text.

And then when the time came to save the score, you would do the action: Set local key CurrentPlayerName & "_Score"

Later you could retrieve it by the action: Set (some variable or text) to Webstorage.LocalValue(CurrentPlayerName & "_Score")
B
11
S
2
G
1
Posts: 64
Reputation: 1,077

Post » Wed Apr 06, 2016 8:07 pm

Great ideas guys! Many thanks!
Gonna experiment with them and see what I can do ;)

Thanks for your time!
B
4
Posts: 3
Reputation: 206


Return to How do I....?

Who is online

Users browsing this forum: AndreasR, David181, tarek2 and 20 guests