How do I control the sequence of functions

Get help using Construct 2

Post » Thu Jul 31, 2014 7:43 am

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:

functionSequence1.JPG


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.


functionSequence2.JPG


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!
You do not have the required permissions to view the files attached to this post.
B
4
Posts: 8
Reputation: 341

Post » Thu Jul 31, 2014 8:18 am

That's very strange... I use functions very often (I'm a programmer, I cannot think in a different way :P) 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?
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
31
S
13
G
4
Posts: 141
Reputation: 4,857

Post » Thu Jul 31, 2014 8:20 am

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
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jul 31, 2014 9:41 am

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).
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
22
G
18
Posts: 2,123
Reputation: 17,150

Post » Thu Jul 31, 2014 10:53 am

@Aphrodite

Shouldn't be the function object synchronous?
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
31
S
13
G
4
Posts: 141
Reputation: 4,857

Post » Thu Jul 31, 2014 11:21 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Thu Jul 31, 2014 12:08 pm

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.

create_destroy.png


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?
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jul 31, 2014 1:36 pm

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.
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Thu Jul 31, 2014 2:01 pm

@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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Fri Aug 01, 2014 1:28 am

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!
B
4
Posts: 8
Reputation: 341


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 17 guests