Why is function only firing once?

Get help using Construct 2

Post » Mon May 20, 2013 3:08 am

CAPX: https://iu.box.com/s/kkldjgprvbys5hk97fs1

I've created this sample to isolate a problem.
Essentially, when more than one objects are picked in an event, and I try call a function with the UID as the argument, it seems to only call the function once.

Thanks, any help would be appreciated.

UPDATE: Added image below in case you can't see CAPX

CannedEssence2013-05-20 03:42:12
B
8
S
1
Posts: 49
Reputation: 615

Post » Mon May 20, 2013 3:50 am

Hi, I'm having some difficulty understanding what you're trying to achieve here, but it looks like the function will only run once because you have it set to on mouse click. If you wanted it to keep running you would need to set up a variable and if it was true then run the function every tick, if it was false stop the function.
B
47
S
16
G
3
Posts: 134
Reputation: 4,982

Post » Mon May 20, 2013 5:35 am

[QUOTE=signaljacker] Hi, I'm having some difficulty understanding what you're trying to achieve here, but it looks like the function will only run once because you have it set to on mouse click. If you wanted it to keep running you would need to set up a variable and if it was true then run the function every tick, if it was false stop the function.[/QUOTE]

The hasMoved variable is an instance variable, so the event should pick all objects with their hasMoved set to true. If you uncomment the disabled action, you'll see that it does in fact successfully do that.

This generic project is designed to isolate the function call as the problem. If I hadn't used that onrightclick trigger, but a different means of picking all the objects, it would do the same it is doing now.

So it appears that it is picking all the objects, but only calling the function once, passing the first object's uid.CannedEssence2013-05-20 05:35:44
B
8
S
1
Posts: 49
Reputation: 615

Post » Mon May 20, 2013 6:20 am

If you see "green arrow" in condition field it would fire once time. Use global variable or method do not have green arrow, it would do action in every tick.
B
96
S
25
G
20
Posts: 3,054
Reputation: 22,642

Post » Mon May 20, 2013 6:36 am

[QUOTE=CannedEssence] [QUOTE=signaljacker] Hi, I'm having some difficulty understanding what you're trying to achieve here, but it looks like the function will only run once because you have it set to on mouse click. If you wanted it to keep running you would need to set up a variable and if it was true then run the function every tick, if it was false stop the function.[/QUOTE]

The hasMoved variable is an instance variable, so the event should pick all objects with their hasMoved set to true. If you uncomment the disabled action, you'll see that it does in fact successfully do that.

This generic project is designed to isolate the function call as the problem. If I hadn't used that onrightclick trigger, but a different means of picking all the objects, it would do the same it is doing now.

So it appears that it is picking all the objects, but only calling the function once, passing the first object's uid.[/QUOTE]

It does pick all, but like you said it passes only single value to function as it is just single variable. Put for each sprite loop at the condition where you call the function. This will call the function for each picked sprite.
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Mon May 20, 2013 7:17 am

[QUOTE=Joannesalfa] If you see "green arrow" in condition field it would fire once time. Use global variable or method do not have green arrow, it would do action in every tick.[/QUOTE]

Thanks for the suggestion. However, as I said in my previous reply, it's not the event condition that's keeping the function from firing for each instance of the "Sprite" object. For example, look here:



Notice how I got rid of the "green arrow" trigger condition. However, if you try this out in the capx, you'll see that it is still only calling the function once per tick, passing only the first instance.

Once again, if I did not have that function call, it would perform the action for the whole batch. It is entirely related to the function call from what I can tell, but I don't know why.
B
8
S
1
Posts: 49
Reputation: 615

Post » Mon May 20, 2013 7:38 am

In Event number 5, you call "ResetHasMoved" many times in every tick because it doesn't fire once time.

You should enable Mouse button, BUT it doesn't pick instance, it just does click from system and the first instance would work, not other instances.


You could add "On Right Button on (Sprite)" It can pick unique instances.
B
96
S
25
G
20
Posts: 3,054
Reputation: 22,642

Post » Mon May 20, 2013 7:44 am

[QUOTE=vee41]

It does pick all, but like you said it passes only single value to function as it is just single variable. Put for each sprite loop at the condition where you call the function. This will call the function for each picked sprite.[/QUOTE]

Thank you, I feared it would have to resort to something like that.

When an event picks more than one instance of an object, don't the actions work like a foreach loop themselves?

For instance, say there's a game where there are balls, and some of them are spiky, and you want to move all of the spiky balls one pixel to the right.

Construct 2 logic:

Event: Ball -> Is isSpikey
Action: Ball -> Set X to self.X + 1

loose programming translation:

foreach (Ball ball in listOfBalls)
if (ball.isSpiky)
ball.X += 1;

----------
Doing this with functions:

Construct 2 logic:

Event: Ball -> Is isSpikey
Action: Function -> Call "MoveOnePixelRight" (Ball.UID)

Event: Function -> On "MoveOnePixelRight"
Sub-Event: Ball -> Pick instance UID Function.Param(0)
Action: Ball -> Set X to self.X + 1

loose programming translation:

foreach (Ball ball in listOfBalls)
if (ball.isSpiky)
MoveOnePixelRight(ball);

void MoveOnePixelRight(Ball ball)
{ ball.X += 1; }

---------
Do you see how this makes sense?
I don't see why I have to add a foreach to Construct 2 when it appears that that's what the event logic is doing by default.

Why is this:

Event: Ball -> Is isSpiky

Any different than

Event: Ball -> Is isSpiky
Sub-Event: System -> For each Ball

?

Thanks for the help =)

B
8
S
1
Posts: 49
Reputation: 615

Post » Mon May 20, 2013 7:52 am

I don't know what's your goal of this game.

Maybe it helps you

B
96
S
25
G
20
Posts: 3,054
Reputation: 22,642

Post » Mon May 20, 2013 1:52 pm

[QUOTE=CannedEssence] [QUOTE=vee41]

...

[/QUOTE]

You are correct, if you do not use functions it will work that way. I recommend you check out some FAQ entries about SOL (selected objects list) and picking in general, it should clear it up in case there is still something that needs clarification. :)

The major difference here is that parameters of function call take only single UID (the first one) from SOL, even if there are more instances in SOL. Function is called only once, unless you specify it to be targeted by all instances currently on SOL. Construct 2 engine really has no way of knowing your intentions, so if you wish to call functions for all objects on the SOL, you'll need to specify it.

Oh, I wouldn't worry about performance if that is what you are afraid. The bottlenecks are generally else where with C2 engine. :)vee412013-05-20 13:54:58
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Next

Return to How do I....?

Who is online

Users browsing this forum: totoe and 21 guests