How do I control the sequence of functions

0 favourites
  • 10 posts
From the Asset Store
Template for dungeon/maze generation, using wave function collapse
  • I appolagize if this has already been answered if so i will delete this post.

    I have a problem of functions not executing in the order their called. In the manual it says the called function does whats in the function, then returns to the point which the function was called in the actions. Unless i'm misunderstanding something, that's not how things are working for me. Here's a simplified example:

    [attachment=1:33tppxu3][/attachment:33tppxu3]

    what should happen is first a new sprite is created, then all sprites are deleted. What actually happens is all sprites are deleted first, then a new one is created.

    So far one way around it is to pause the code in between functions but that's bad practice, it will slow down the game, and if a function lags and takes longer than the paused time will create further problems.

    [attachment=0:33tppxu3][/attachment:33tppxu3]

    I've thought maybe only using using the return value as a way to test if a function is finished before calling the next function, but that doesn't seem to work either.

    What i'm trying to do on my actual game is more complicated, but i'm pretty sure this example sums up the problems i'm having with not being able to control the sequence in which functions execute.

    Thanks!

  • That's very strange... I use functions very often (I'm a programmer, I cannot think in a different way ) and it never happened to me...

    Just a question that for sure isn't the solution... why you used the "for each" in this example? Have you tried without it?

  • Have a variable that is set when all objects have been created and check for that in another event. Trying to sequence events in c2 is tricky and when using functions even more so. As you said, using a wait doesn't usually do what you want.

    And as previous poster mentioned you don't need for each there

  • The function trigger in the good order I would say, just that the newly created object is not picked by the destroy even wit the for each(since functions cancel the current picking, and that the way C2 handles the newly created objects is not straightforward).

  • Aphrodite

    Shouldn't be the function object synchronous?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Understand the picking of newly created/spawn instances since R101 - LINK

    Jeff Skyrunner: Nothing to see with the "Function" behavior, it has to do with the spawning mechanism itself.

    tjjoris: One solution could be to set the wait action from your first screenshot to 0 sec.

    Indeed not the best solution, but since that's the only bit of your project you are presenting us, that is an immediate workaround to this precise problem.

  • Hi KYATRIC. The fix you suggest doesn't seem to fit with the explanation in that link. Could you help me to understand how this works. I have created a capx based on the OP's, reproduced here.

    [attachment=1:3lv3lwuv][/attachment:3lv3lwuv]

    Without the wait(0) at event 4, the log output is as follows, showing that only 9 sprites are destroyed (the 9 in the initial layout, and not the 10th created in the function).

    (9) created a sprite

    createObject()

    created a sprite

    destroyObject()

    (9) destroyed a sprite

    With the wait(0) the output is as follows:

    (9) created a sprite

    createObject()

    created a sprite

    destroyObject()

    (10) destroyed a sprite

    Which shows all 10 sprites being destroyed. However, we have not altered any events, just added a wait. This doesn't fit with the statement that newly created sprites are "... not picked ... until the end of the next top-level event".

    In the above case the 'next top-level event' after event 4 (in which the object is created--via function call) would be event 5.. which we do not reach between the two function calls. So the wait(0) is actually a 'yield' which allows the next tick to occur, and so running subsequent top-level events.

    Finally, if you agree with the above, then perhaps the wait(0) is actually a reasonable solution?

  • The best solution will always be the condition "Pick by UID". It can pick instance that have just been created, even when not in a top level event. As good as this solution is, it's not always practicable.

  • codah: Yes the wait action, when set to "0" second, just let one tick go by and then makes the execution of the actions following it delayed by one tick as well (see "how to use the system wait action").

    So since the "destroy" function is not executed in the same tick as the "create" function then the new instance is well created and is "pickable".

    Anyway, I am not sure what the point of destroying instances in the same tick they are created really is about in the end.

    And that may actually be where the logic of what the OP really wants to do can be improved.

  • Thanks a lot for all the informative responses guys, you've all been a great help

    It does indeed look like the object is not getting picked for destruction after its created. Wait 0.0 seems to be a solution, i'm not sure about "pick by UID" but that might be better than forcing the game to wait 1 tick.

    The reason why i'm creating and immediately deleting sprites is kindof complicated to explain, its not actually necessary. It was more for debug purposes as a way to identify if a variable was changing properly. but now that i understand a little better, the problem is not about the functions not running in sequence but sprites not existing immediately after their created.

    Knowing that I am able to easily solve the problem in my code.

    I don't want to bore you with a long explination of what is happening in the actual game, unless you really want me to. But I appreaciate all the information and help from you guys, thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)