Idea: make async easier with "Then" event

Discussion and feedback on Construct 2

Post » Tue Apr 14, 2015 5:29 pm

I was thinking about the new Local Storage plugin and how it's a little tricker to use with the new asynchronous style of access. Previously with WebStorage you could just directly retrieve a value in an expression:

Image

This is really easy to use, but has the disadvantage of being synchronous, i.e. it pauses and waits for "MyValue" to be read from storage. If the system is busy or it's a large value, this could take say 100ms, and the game freezes for this time (jank).

Local Storage solves this with asynchronous storage. You use the 'Get item' action, which then triggers 'On item get':

Image

Now if it takes 100ms to read, it continues running the game while reading from storage in parallel, and when finished it triggers 'On item get'. The new problem is now the reading logic is in a trigger event. This event could be far away from the action that started it (e.g. in a different sheet). It could also be inconvenient if there are multiple 'On item "MyValue" get' triggers for different purposes, and they all trigger at the same time based on this one action. This gets more complicated if you want to do something else async afterwards, e.g. set a value, which involves more triggers.

This can be solved with good organisation, but it would be nice if we could have events that are both async and as easy as the sync version. In Javascript "Promises" go a long way to help making this easy, so I was thinking of a similar type of feature for the event system - a "Then" condition, which triggers whenever the previous event's asynchronous action finishes (mockup):

Image

The "Then" trigger is tightly coupled with the previous event, in the same way the "Else" event is based on the event directly preceding it rather than working by itself (and that also means there are valid and invalid places to put it). It works the same as the previous async example, but:

- you didn't have to find the right trigger to use
- you didn't have to type in "MyValue" again for the trigger, it knows it's triggering for that value from the previous event
- it guarantees the thing to be done afterwards is in the next event, not somewhere far away, keeping events tidy
- it only fires the one "Then" trigger, not every trigger of that kind in the event sheet, making it easier to use if the value is loaded for different reasons in different places
- this might end up being a C3 feature, not sure it would make C2

I think to make it clear the asynchronous action needs to be clearly marked - I just scrawled in a red asterisk as a placeholder representation.

This kind of asynchronous "Then" event could be applied to all sorts of objects which already implement asynchronous features:

- AJAX "Request URL" could be followed by "then" which is fired when the response is received, and would mean you don't need to worry about getting tags right
- "Request user media", "Request fullscreen", "Request location" or other user-prompting actions can fire "then" after they are approved/get data
- Pathfinding could fire "then" after the path is found
- third party plugins could integrate with the "then" trigger
- anything we add in future could more easily use asynchronous actions, making it easier to add parallel features that make use of multiple cores

A few other thoughts:
- the "Then" condition would have the caveat that you can only have a single async action in the previous event. If there were two, it wouldn't know which to fire "then" for. So in that case you'd probably have to go back to using the existing trigger system.
- "Then" triggers could be chained. So the "Then" event could contain another async action, and be followed by another "Then" trigger, making a clear sequence of actions which run in parallel.
- Async actions can fail, e.g. AJAX "On error", Pathfinding "Path not found" etc. I guess there would need to be a kind of error version of "Then", which could also come after the "Then" event. Javascript calls this "catch".
- I'm not sure the name "Then" is the best name - it's the term Javascript uses, but it could confuse beginners who often want to figure out how to make an "If - then" type logic (which is basically just a normal event - "if conditions true, then run actions"). It would be especially confusing that "Then" is a trigger. Other trigger names could be "Next", "After", "On done"...

A different idea I had was to make asynchronous actions like the "Wait" system action, except instead of just waiting, it would be processing some async action. I think this is limited though, since it makes it difficult to run asynchronous actions in parallel - if an event had 3 async actions, this method would force them to run one after the other in sequence, rather than starting them all at once and firing triggers as each completes (important for best performance and multi-core usage). So I think the "Then" event is a better idea.

Does anyone have any thoughts about this? Good idea or bad idea? Perhaps there's an even better way it could be approached?
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Tue Apr 14, 2015 5:44 pm

An additional idea raised by talking to @Kyatric:

Have another kind of "All completed" trigger for events which start multiple async actions in one event. So if you start 5 async actions in "On start of layout", and follow it with "All completed", the next event runs when the 5 async actions in "On start of layout" have all finished.
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Tue Apr 14, 2015 5:44 pm

Was about to ask about something like that, cause new localsotage kind of breaks my workflow xD

"Then" is a nice idea but i think using only the name "then' will confuse a lot of users. Maybe this little green right arrow could be change to something different, like arrow pointing up or whatever. Just to differentiate it better from standard events and so when you see that event you now right away why is it there and what is doing. (Like i'm using blank events instead of "every tick" - I can tell without reading that this event will run every tick)
ImageImageImageImage
B
157
S
66
G
41
Posts: 2,599
Reputation: 34,825

Post » Tue Apr 14, 2015 6:20 pm

Your second post was my question i had in mind :).

I had A LOT of webstorage functions in my game and i now am trying to tune them to LocalStorage.

Somehow i do not get it working in any way. I can't even get a value to be written ( checking with debug LocalStorage shows empty ).

Could you tell me why this isn't working:
Image
I use the "setup" key to set all values the very first time the game runs.
P.s. Sorry if i am hi-jacking your topic. :oops:
Last edited by Allardje on Tue Apr 14, 2015 6:42 pm, edited 2 times in total.
B
10
S
4
G
3
Posts: 165
Reputation: 2,511

Post » Tue Apr 14, 2015 6:35 pm

Perhaps it should automatically appear and conjoin to an event that uses asynch so that the user can't get confused/realizes it's part of the command.

Image
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
116
S
41
G
17
Posts: 2,204
Reputation: 19,545

Post » Tue Apr 14, 2015 6:46 pm

@Jayjay, just what I was about to post. I like the idea of the then statement. However, I think that the tag system should still be in place optionally. Given the example of 5 async actions being run at the start of layout, the "On all complete" covers the basics of performing these actions. But what if you want to know when just one of the 5 has happened? I mean, you could just call that one after the first 4 as an action of the "On all complete event". But if the user wants to optimise, then the tag method would work better, where only the one action is given a tag and then picked up by a tagged 'Then' event, and the "On all complete" event fires after all 5.
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Tue Apr 14, 2015 6:47 pm

Jayjay wrote:Perhaps it should automatically appear and conjoin to an event that uses asynch so that the user can't get confused/realizes it's part of the command.

Image

But if i want it to fire not only after "On Space pressed"? Let's say i have multiple events with Get Item?
B
22
S
9
G
7
Posts: 421
Reputation: 6,543

Post » Tue Apr 14, 2015 6:48 pm

Isn't this kind of like "wait for signal"?
Image ImageImage
B
169
S
50
G
169
Posts: 8,286
Reputation: 108,216

Post » Tue Apr 14, 2015 6:59 pm

Allardje wrote:Your second post was my question i had in mind :).

I had A LOT of webstorage functions in my game and i now am trying to tune them to LocalStorage.

Somehow i do not get it working in any way. I can't even get a value to be written ( checking with debug LocalStorage shows empty ).

Could you tell me why this isn't working:
Image
I use the "setup" key to set all values the very first time the game runs.
P.s. Sorry if i am hi-jacking your topic. :oops:



Well, as far as I can tell, you should have an action "Check item "setup" exists" in the "Start of layout" event, that would then trigger the "On item "setup" missing" event.

Also, the new LocalStorage is asynchronous. It means, there is a delay between the moment the action is encountered, and the action is executed.
In your first event, you likely restart the layout before any of the values are set to be written.

Consider them like AJAX requests, they may vary in the time and order taken for them to complete.
So here, that's where a "All requests complete" condition like Ashley is suggesting would be nice as Event nb 2 to which the action would be "Restart Layout".

Currently, I'd try to have a "On item "setup" set" condition/event nb 2 that would contain the restart layout action.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
289
S
112
G
94
Posts: 7,333
Reputation: 69,293

Post » Tue Apr 14, 2015 7:59 pm

Sisyphus wrote:But if i want it to fire not only after "On Space pressed"? Let's say i have multiple events with Get Item?


That's where the "on all complete" trigger Ashley was talking about would come into play, but maybe the Then feature can be turned on and off in the same way that you can right-click an event and select "OR" mode.
"Construct 4 lets YOU make advanced games! (but not play them)" Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
116
S
41
G
17
Posts: 2,204
Reputation: 19,545

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 8 guests