Arrrrg why local variable doesn't work?

0 favourites
  • 9 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • So I have a sprite animation's frame set to a local variable, and each click of the mouse is supposed to increase said variable by 1, and thus progress the frame shown by the sprite (comic book bubble updates).

    HOWEVER, this works ONCE and then stops working.

    If I make the variable global, it works as intended...

    Why does it have to be global to work?

    The project

    Thanks for any help!

  • Because every time you run the event with a local variable it resets to its default value 0

  • But why does it reset?

    And how do I prevent it from doing so?

    (and it does not reset when it is global?)

  • That is just how they work, if you want to retain the value then you can use a global variable as you mentioned

  • Really!?

    local variables constantly reset!? Do you know the logic of why this is so?

    this makes them rather limited and forces me to use global when there isn't really a need for it to be global other than this weird limitation...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Try setting your local variable to "Static". This should preserve its value and stop the reset every tick. (Double-click on the variable and tick the Static box).

  • brushfe

    That did it!

    Thanks!

    Every day I learn something new about this program...

  • Really!?

    local variables constantly reset!? Do you know the logic of why this is so?

    this makes them rather limited and forces me to use global when there isn't really a need for it to be global other than this weird limitation...

    mutajon, to answer why; in programming, a local variable is a variable that only exists at the time a method runs. Each time the program calls the method, the variable gets created and when the method ends, the variable gets destroyed. The variable is "local" to the method. Every tick in construct, the actions and events are run as separate instances. In essence, the system is calling each of these actions like methods which causes a local instance of the variables to be created then.

    I hope that helps you understand these local variables a little better.

  • InDWrekt

    It sure does.

    And thank you for taking the time to explain it so clearly :)

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