Feature Request : Improve/New Local Storage

Discussion and feedback on Construct 2

Post » Fri May 20, 2016 7:15 pm

The idea is save all the events possible and do all in the properties and in an interface similar to the "instance variables".

Every time i have to use the Local Storage plugin i have to read the doc and the examples, not's much intuitive at least for me and a bit frustrating to do some things.

I want to do some kind of achievements/trophies to my game , i posted here (viewtopic.php?f=147&t=173064) but put here a pic of what i mean:
Image

So, why not if we can get all in a "Local Storage" interface like the "instance variables" and properties in the object to automatize the process and avoid all this events.

I made some interface using the actual things to show the ideas:
Image

So, we can add variables and set what value in case of missing, maybe 0, maybe a text saying "locked" or whatever and the moment we want this one gets saved.

In the properties we have when load the storage keys. In this case on start the layout "Gameplay" load all the storage keys, in case some ones are missing we already have set the number/text. Other possibilities can be added, and if somebody needs to be more specify always can continue use the events. Or if there are some keys that we don't want to load maybe add another tab in the interface for each key saying "Yes/No" to be loaded using events for any reason.

So, we have all this configured in a moment and without events, now is time to set/save values in the local storage... or not!. If we are using a Global Variable called Best_Score , we can link/pin to a Key in the "local Storage" and in their interface we already set when save it like On Change/On Start Layout/Every tick , etc....

If you link a Global Variable to a Key in the local Storage, when the Keys in the Local Storage are loaded they also set the global variables. After that, when a global variable changes will change the key linked.

Maybe there are things that can't be done for some reason, but as i said the idea is avoid all this events of: Read if exists, get item, set value, if key missing, blablabla.... get something more visual/intuitive and easy to access/use.
B
24
S
10
G
3
Posts: 553
Reputation: 4,483

Post » Sat May 21, 2016 10:28 am

A common technique is just to load and save an entire Dictionary object's JSON which covers most of this.

LocalStorage is asynchronous (i.e. setting/getting fires a trigger after a moment, rather than being immediately available) which means it can't really be mapped to anything synchronous (like setting/getting global variables). If a "get global variable" or similar turns in to a "get localstorage value", that value is not immediately available, and may fail as well. So that's not actually possible, without some kind of internal caching mechanism, which is basically the same as doing the save/load dictionary object technique.
Scirra Founder
B
397
S
236
G
88
Posts: 24,422
Reputation: 194,558

Post » Sat May 21, 2016 11:19 am

matriax wrote:The idea is save all the events possible and do all in the properties and in an interface similar to the "instance variables".
...

To be honest, you can just forget working with it if you plan to use it on multiple platforms.
Each platform comes with surprises that break the plugin in some weird way,
without Ashley having a possible chance to fix it once and for all.

So if you'd ask me, keep on using the old webstorage plugin for as long as you can, before it gets depreciated by the browsers.
Both plugins work fine but the sync plugin saves a lot of events and stress on our side.
ImageImageImage
B
63
S
23
G
78
Posts: 659
Reputation: 44,931

Post » Sat May 21, 2016 11:33 am

Ashley wrote:A common technique is just to load and save an entire Dictionary object's JSON which covers most of this.

LocalStorage is asynchronous (i.e. setting/getting fires a trigger after a moment, rather than being immediately available) which means it can't really be mapped to anything synchronous (like setting/getting global variables). If a "get global variable" or similar turns in to a "get localstorage value", that value is not immediately available, and may fail as well. So that's not actually possible, without some kind of internal caching mechanism, which is basically the same as doing the save/load dictionary object technique.


Then, instead of using linked global variables use Storage Keys as variables to use in events.

So i create a key called BestScore in the storage. In events i can say that when the Player_score is greater than Key_BestScore set new.

And the load/save/if missing,etc... the Local Storage keys is already covered in properties without events or use any other plugin like Dictionary or create events to load/save json,etc...

Or if not is possible either the Local Storage variables add a instance variables properties in the Local Storage object that can be loaded/saved with some basic options as in my pics. And as always if somebody needs more deep things can use events.
B
24
S
10
G
3
Posts: 553
Reputation: 4,483

Post » Sun May 22, 2016 10:55 am

I don't think any of that changes the basic fact that you can't pretend asynchronous code is synchronous. It breaks assumptions like the events will run in any particular order, which the existing system makes obvious by making you use triggers in a separate event. Anything which looks like a simple comparison/access but really is asynchronous in the background would be even worse than we have now - it would just silently break or fail to work as you expect in random situations!

I wrote up an idea for a "then" event to try and make things like this easier, but it's not supported and I'm not sure how much it would really help anyway: https://www.scirra.com/forum/idea-make-async-easier-with-quot-then-quot-event_t128870
Scirra Founder
B
397
S
236
G
88
Posts: 24,422
Reputation: 194,558

Post » Sun May 22, 2016 12:55 pm

Once you figure out how local storage works, and keep in mind it is ASYNC, it is not that hard. You can write methods to make it easy as pie, or follow a certain technique.

Your biggest challenge if you are not use to ASYNC methods, is to keep in mind the AYNC techniques. Once you understand that, it is easy. There are things to keep in mind, such as when to fire them, what to do if it takes awhile (blocking vs non blocking UI which has pros and cons), among other things.
B
43
S
18
G
8
Posts: 500
Reputation: 8,445

Post » Sun May 22, 2016 1:26 pm

Ashley wrote:I don't think any of that changes the basic fact that you can't pretend asynchronous code is synchronous. It breaks assumptions like the events will run in any particular order, which the existing system makes obvious by making you use triggers in a separate event. Anything which looks like a simple comparison/access but really is asynchronous in the background would be even worse than we have now - it would just silently break or fail to work as you expect in random situations!

I wrote up an idea for a "then" event to try and make things like this easier, but it's not supported and I'm not sure how much it would really help anyway: https://www.scirra.com/forum/idea-make-async-easier-with-quot-then-quot-event_t128870


Then a trigger on the event sheet on you say "load storage" and this event loads all the keys and sets to the variables you previously defined and in the case they are missing (first time running) sets to 0 or whatever you defined previously.

And for save another unique event. Now i have to do "get item" event and after a "set item" event, or there is another way to do in a unique event? :P. If not, will be great simply when X happens set Key.BestScore(The local storage) to BestScore(Global variable).

This is what i'm doing, is correct?
Image

There is no way to save the "get item" event? and in the comparision say something like:
LocalStorage.key.BestScore
B
24
S
10
G
3
Posts: 553
Reputation: 4,483

Post » Sun May 22, 2016 1:53 pm

OP, why don't you just collect all external data on the start of your app and stored them inside dictionaries and arrays. Compute them there at blinding speed with zero problems. On exit, save or set interval, slot back the data into the local storage objects.

It's pretty easy, no fuss and pretty much how all other people do it,.
B
36
S
18
G
11
Posts: 248
Reputation: 8,694

Post » Sun May 22, 2016 3:53 pm

matriax wrote:
Ashley wrote:I don't think any of that changes the basic fact that you can't pretend asynchronous code is synchronous. It breaks assumptions like the events will run in any particular order, which the existing system makes obvious by making you use triggers in a separate event. Anything which looks like a simple comparison/access but really is asynchronous in the background would be even worse than we have now - it would just silently break or fail to work as you expect in random situations!

I wrote up an idea for a "then" event to try and make things like this easier, but it's not supported and I'm not sure how much it would really help anyway: https://www.scirra.com/forum/idea-make-async-easier-with-quot-then-quot-event_t128870


Then a trigger on the event sheet on you say "load storage" and this event loads all the keys and sets to the variables you previously defined and in the case they are missing (first time running) sets to 0 or whatever you defined previously.

And for save another unique event. Now i have to do "get item" event and after a "set item" event, or there is another way to do in a unique event? :P. If not, will be great simply when X happens set Key.BestScore(The local storage) to BestScore(Global variable).

This is what i'm doing, is correct?
Image

There is no way to save the "get item" event? and in the comparision say something like:
LocalStorage.key.BestScore



I think you need to take a step back and understand the fundamentals of async calls. what you are trying to do won't work. A quick google will bring you many results on the differences.

The way you are trying to use it is how the old storage worked, it was a sync call. New standards of browsers that is deprecated as I understand it and you need to use async methods.

Once you step back, and understand how/why it works, it is not bad at all.
B
43
S
18
G
8
Posts: 500
Reputation: 8,445

Post » Mon May 23, 2016 3:40 am

The request could be separated into two parts,
1. enumerate the variables in GUI like global/local/private variables
2. save/load in synchronization mode

Using a dictionary like Ashley said, could solve request2 only.
B
108
S
26
G
271
Posts: 4,471
Reputation: 151,777

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 22 guests