Why is function only firing once?

Get help using Construct 2

Post » Mon May 20, 2013 1:52 pm

[QUOTE=CannedEssence] [QUOTE=vee41]



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
Posts: 756
Reputation: 7,322

Post » Mon May 20, 2013 7:17 pm

Why is this:

Event: Ball -> Is isSpiky

Any different than

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


In the first example the actions run once if there are any spikey balls. In the second the actions run once for each spikey ball.

For example if you have 5 balls:
global count = 0
Ball -> Is isSpiky
    Ball -> Set X to Self.X + 1
    System -> Add 1 to count
After that runs, count = 1.
Ball -> Is isSpiky
System -> For each Ball
    Ball -> Set X to Self.X + 1
    System -> Add 1 to count
After this, count = 5.


It's the 'ball: Set X' action that has the implied loop. The function action doesn't have any implied loop.

Posts: 1,520
Reputation: 25,790

Post » Wed May 22, 2013 6:14 am

Thank you for all your responses =), especially ramones-- you look like you put a lot of effort into that answer. I'm glad to join such a helpful community.

I think I see the pattern now. I'd be grateful if anyone can confirm the truth about the following assumptions concerning the behavior of Construct 2 Events.
Quick definitions: "context of action" refers to left side of action statement; "instruction" refers to right side of action statement.

- The context of the action (e.g. "Ball") will reference all the instances filtered (or "picked") from the condition(s) of the event (generating a "selected object list"), or simply all existing instances of the object type if not referenced in the condition(s).
- An arrayMap of the instruction will be applied to the context.
- If the instruction references the context object (or more accurately the SOL for the context object type), then the properties (instance variables) for the instance of the object in the instruction will line up with each object in the SOL when the instruction is performed on each instance in the SOL.
- If the instruction references an object type that doesn't match up with the context, then the properties of that object will reference only the first object in the all-existing-objects list for that type, or SOL if that object type was referenced in the condition(s) of the event.
- For static classes like "Function" or "System" as the context, there is only one instance, so the instruction will only perform once no matter what, unless you use a foreach condition.
- If more than one object type is referenced in the conditions for the event, then a separate SOL will be generated for each object type.
- Using a foreach condition will override the default arrayMap behavior of the context object. See image below:

Note that the ball action would ordinarily arrayMap the instruction (or create a for each in this code translation), but here if we add a foreach condition, it overrides that and instead the context becomes just the current instance of the context object in the outer foreach loop.

Is this all correct?

If it is, then I hope that other people in the future see this thread, because I had trouble finding these clarifications elsewhere. The foreach and SOL part of the manual is a bit scant, but I guess I understand, since it's probably a bit advanced for the target user of the tools.

Thanks again!

CannedEssence2013-05-22 06:17:25
Posts: 55
Reputation: 807


Return to How do I....?

Who is online

Users browsing this forum: Google [Bot] and 65 guests