How do I use math with WebStorage?

Get help using Construct 2

Post » Wed Feb 25, 2015 11:12 pm

So I have a problem where I need to use WebStorage, but for a event I only need it to add a number. (The stored key is an integer).
Tried making so:
Click on Button -> Set local key "StoredKey" to "WebStorage.LocalAt(StoredKey) + 1"
But it doesn't seem like C2 accepts me putting in the "+" sign. How would I do if I wanted to add 1 to the stored value?

IE: If key "StoredKey" would currently be 3, when I click my button it would add 1 and it'd be 4
Website Twitter Indie Developer
B
20
S
5
G
4
Posts: 206
Reputation: 2,904

Post » Wed Feb 25, 2015 11:27 pm

It tells me "+ does not work with 'string' and 'integer'"
Website Twitter Indie Developer
B
20
S
5
G
4
Posts: 206
Reputation: 2,904

Post » Wed Feb 25, 2015 11:31 pm

Use int() to convert a string to an integer and str() to convert a number to a string.
The value in webstorage is always saved as a string.
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,305

Post » Wed Feb 25, 2015 11:37 pm

Click on Button -> Set local key "StoredKey" to str((int(WebStorage.LocalAt(StoredKey)) )+1)

Something like this


Also you could use a global variable to intermediate this.
B
52
S
21
G
3
Posts: 599
Reputation: 6,506

Post » Thu Feb 26, 2015 1:29 pm

Thanks, fixed it! :)

kossglobal wrote:Also you could use a global variable to intermediate this.

I tried playing around with global variables but it seemed much more easier and better to use local storage directly instead of having it to save the variable to a storage and then load it, I felt like it may be cleaner just to do it like this, but thanks! :D

Thread can be closed
Website Twitter Indie Developer
B
20
S
5
G
4
Posts: 206
Reputation: 2,904


Return to How do I....?

Who is online

Users browsing this forum: pcprice76, smlyldz and 9 guests