local variables overwritten inside one event

From the Asset Store
Footsteps SFX One contains 400 sounds of steps and jumps on different surfaces.
  • Hi,

    I have made a CAPX of a larger example inside a game to demonstrate a problem.

    If I touch one of the sprites, the sprites instance variable is stored in a local variable, it tweens (rotating) in 2 seconds, and waiting till it is finished, it is stored back in the sprite instance variable with no change. This works how I want it to.

    However if I touch all 3, 1 after the other, the local variable will take on the value of the last touched event and all 3 sprites will end up with the same instance variable value.

    I had always thought local variables were independently set depending on when the event was run. This doesn't seem the case. So whats the best way to store the value and retrieve after each event has finished.

    Hope I am explaining that well.

    capx.

    drive.google.com/file/d/1NZNCqAZ86YSF0hUVsmhkJRYLujkgOQvX/view

  • Is this for practice? Because that variable wouldn't do anything

  • If you want to store unique values per each instance, you need to use instance variables. So define storePoints variable on the sprite.

  • It's just an example. In reality I am storing the value of the sprites instance variable in a local variable, rotating the sprite, once rotated it is deleted and then a new sprite is created. At that point I want to put the variable value back into the instance variable.

    So the reason I am not storing the value in the instance variable is I am deleting the sprite so I need to store it temporarily while that happens.

  • Sounds problematic, the picking of the new instance, it might not be available yet in the same event. You would have to pick all instances again and then define the new one somehow maybe on last created, but the code may not see it. Better to just store as a global variable then all the problems go away.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the comments.

    In the end I put in a new event that triggered on when tween is finished.

    However I was surprised in my original script the local variable was being overwritten by the second time the sprite was being pressed.... maybe I am missing something.

    e.g.

    1.

    press sprite 1

    'storePoints' becomes 0

    2.

    after 0.5 secs (it takes 2 secs to rotate 90 degrees)

    press sprite 2

    'storePoints' becomes 90

    3.

    after 2 secs from start

    sprite 1 is fully rotated

    sprite 1 'sprite1.points' is changed to 90, not 0 as originally stored.

    4.

    after 2.5 secs from start

    sprite 2 is fully rotated

    sprite 2 'sprite2.points' is changed to 90

    I would have expected 'sprite1.points' to be 0, not 90.

  • what I do in cases like that is use a family for the sprites. pick the one to destroy by the family, create a new instance by the sprite name and transfer the variable to the new instance and destroy the family one.

  • Thanks for the comments.

    In the end I put in a new event that triggered on when tween is finished. This fixed it however....

    I was surprised in my original script the local variable was being overwritten by the second time the sprite was being pressed. It would suggest the local variable is static but isnt.... maybe I am missing something.

    e.g.

    1.

    press sprite 1

    'storePoints' becomes 0

    2.

    after 0.5 secs (it takes 2 secs to rotate 90 degrees)

    press sprite 2

    'storePoints' becomes 90

    3.

    after 2 secs from start

    sprite 1 is fully rotated

    sprite 1 'sprite1.points' is changed to 90, not 0 as originally stored.

    4.

    after 2.5 secs from start

    sprite 2 is fully rotated

    sprite 2 'sprite2.points' is changed to 90

    I would have expected 'sprite1.points' to be 0, not 90.

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