Construct-bugs icon indicating copy to clipboard operation
Construct-bugs copied to clipboard

Wait for previous action fails with recursive async functions

Open skymen opened this issue 4 years ago • 6 comments

Problem description

Let's say I have an async function, and I want that async function to call itself recursively. Now I want whatever even called that function to be able to way for my async function to return before continuing executing events.

Well that doesn't work

Attach a .c3p

LoadURLAsyncLoop.zip

Steps to reproduce

  1. Press play and open console
  2. Disable the last "Wait for previous actions" in the function
  3. Play again, and see how the canvas gets cleared even though the async function isn't done

Observed result

In the first case, the game throws an error TypeError: Cannot read property 'pauseCount' of undefined

In the second case, the actions of my "on button clicked" event resume as soon as the first layer of recursion is called.

Expected result

The wait for previous actions should work as expected, and to be fair, it shouldn't be needed at all since the function is async, and it should count towards the actions count that are waited for when the main "wait for previous actions" is called, all the way up in the "on button clicked" event.

More details

Affected browsers/platforms: doesn't seem like a browser related issue

First affected release: No idea, this is the first time I ever tried this

System details

View details

PASTE HERE

skymen avatar Apr 25 '21 21:04 skymen

This also affects me :(

JeFawk avatar Apr 25 '21 22:04 JeFawk

Minimal repro: issue4761.zip

In your case though, why not use a loop? That seems like a more obvious way to iterate through a list. You don't need recursion for that.

AshleyScirra avatar Apr 26 '21 10:04 AshleyScirra

Loops can't be waited for in individual steps as far as I am aware. This is maybe not the best way to make what I was trying to do, I was experimenting with different ways to get that effect

skymen avatar Apr 26 '21 11:04 skymen

Encountered this issue once again when interpreting a custom scripting language In this case, I have a setup like this:

A scenario is a list of actions I can do. I have a special keword that defines the next actions as a function and adds it to a scope object.

Each action is ran by the "Action" function, one by one, and Action waits for a signal to resume

In the case of a simple scenario like this one:

function myFun
scroll_to poi water
upgrade_button "Water room" 500 water
end

nowait scroll_to poi ship 0
anim_ship
myFun

this is what happens: "Scenario" is called. It parses the string above, and exports two object:

actions = ["nowait scroll_to poi ship 0", "anim_ship", "myFun"]

scope = { myFun: 'function myFun scroll_to poi water upgrade_button "Water room" 500 water end' }

then it goes over each action and calls "Action" on them. Action then calls each function defined by the first keyword of the action and waits for a signal. The function "nowait" however calls "Action" back with whatever it's given but signals the end of the function immediately (so it doesn't wait for the action to finish) The function myFun, being a user defined function calls Scenario once again, and it's all recursive.

Anyway, so in this case "Action" and "Scenario" are both async functions that can call themselves recursively and that can require to wait for themselves to finish essentially.

I can send a C3P if you need to look into my code, but I hope this is enough to prove that this issue can be met in a real world scenario

skymen avatar May 12 '21 13:05 skymen

This happened to my custom JavaScript code also. It worked fine with the classic script mode but not with the new script imports on c3. I got it to work when using my PC's on windows but when on ios and android, it doesn't wait for the recursive function to finish. The only solution was a while loop.

listpull avatar Oct 26 '21 18:10 listpull

To be clear, this issue is talking about event sheet functions, not Javascript. You might be referring to a different issue

skymen avatar Oct 27 '21 07:10 skymen