Why is function only firing once?

0 favourites
  • 12 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • CAPX: 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

    <img src="http://cannedessence.com/public_images/functionIssue.jpg" border="0" />

  • 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.

  • 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.

    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.

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • > 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.

    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.

    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.

  • 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.

    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:

    <img src="http://cannedessence.com/public_images/functionIssue2.jpg" border="0" />

    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.

  • 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.

  • 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.

    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 =)

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

    Maybe it helps you

    <img src="http://i.imgur.com/8HOIXFY.png" border="0" />

  • >

    >

    > ...

    >

    >

    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. :)

  • 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.

    <img src="https://dl.dropboxusercontent.com/u/8367729/construct/pics/impliedForEach.png" border="0" />

  • 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:

    <img src="http://cannedessence.com/public_images/impliedForEach2.png" border="0" />

    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!

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