How do I use recently spawned objects?

  • I would like to create and position a few objects. Here is my event sheet:

    The story starts with the button. When clicked it logs the number of Pawns. At first, this should be 0.

    Then I spawn many Pawns and log the number of Pawns in each loop. This should increase at each loop. At the same condition, I call my move function and log the number of pawns there too. I would expect to log the same number that I logged just after having spawned the pawns. But it logs 0.

    As there are no Pawns, clearly my SetPawnPosition function won't find the Pawn with the given `idx` even though it was spawned and set up a second before the move call.

    Still, when I click the button for a second time, the correct number of pawns is shown, and the counter does not reset, even though I call Pawns.destroy for every pawn in the first line of "Setup"

    pawn count 0

    pawn sub 1

    pawn move 0

    pawn sub 2

    pawn move 0

    ...

    pawn sub33

    pawn move 0

    pawn sub34

    pawn move 0

    pawn count 34

    pawn sub 35

    pawn move 35

    pawn sub 36

    pawn move 36

    Is this a bug or I screw up something?

    Tagged:

  • Here is an even simpler event sheet to demonstrate the issue:

    The log shows:

    <button clicked>

    At start 0

    Spawned 1

    pawn once 0

    <button clicked>

    At start 1

    Spawned 2

    pawn once 2

    <button clicked>

    At start 1

    Spawned 2

    pawn once 2

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • nagyv

    it is not a bug, but the object count doesn't get updated until after the next top level event after the object gets created.

    so, all your code executes just fine, you just can't depend on the .count being accurate at the time you are displaying it...

    in event 6, call the function first, add a "Wait 0 seconds" and then log the Pawns.count

    and you should get the correct result.

    (The "Wait 0 seconds" forces the eventsheet to execute the actions after it at the end of the current tick, and by then the count has been updated internally.

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