Broken local variables?

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Pardon me my great ignorance if I simply didn't fully understand the way local variables work but, is this normal? (see capx)

  • There is nothing wrong, the reason it appears to not work, is because it gets reset to 0 all the time. You can see this if you run it in debug mode and pause it, and just keep pressing step.

    You have to move the "Text Set Text.." below the "set variable to round..". And optimal move the local variable to the top of the event where you need it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Or you can make the local variable "static" so it saves it's value.

  • nimos100 : I just tried putting everything into the same group, but it keeps not working (see capx). Why does the variable get reset to 0 anyway?

    ( R0J0hound : Yes I know, and then it would show in the debugger. But I need the variable to change over time.)

  • Ashley and Tom work hard on not only Construct 2 but something just as magical...

    ...and if you type "Local Variable" to search in the Manual you get ....

    ...and if you scroll down to where it says "Static and constant variables" you get...

    "By default, local variables reset to their initial value whenever entering their scope (usually every tick), like local variables in programming languages. If the variable is marked static in the Event Variable dialog it will persist its value permanently, like a global variable."

    Good luck. <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":-)" title="Smile">

  • I had read that page. More than once on top of that. Last time was today, and I had purposely avoided reading the paragraph about "static and constant variables" because it was of no interest in my case and I wanted to keep working on resolving my problem. Thanks for pointing it out though.

    Another thing you can read, in the "edit profile" page, is :

    "Don't forget, let's all be nice to each other in the forum!"

    Not quite sure if "cynical" and "mocking" is compatible with "nice", but let's say that none of us can remember every single thing that's written on this website.

    Thanks everyone.

  • [quote:3v0i9pu2]I just tried putting everything into the same group, but it keeps not working (see capx). Why does the variable get reset to 0 anyway?

    Not really sure how to explain why it resets. But the way you can look at it, is that a local variable only keep the assigned value during a single cycle (tick) if its in an event. So at the end of the event the variable is "destroyed".

    For a function it only live during the function call. So if you call a function several times within the same cycle, the variable will still be reset to it initial value each time you call it.

    So its a "Use and throw away variable" where global is "Keep and reuse variable" that stays the same until you change it. So it will never reset to its initial value unless you do it manually.

    Not sure if that makes it easier to understand. But not sure how to explain it otherwise

  • Ok now I see where I got lost. I confused "static" and "constant". I don't need constant variables right now, but I could use some static variables.

    Now it works! Thanks nimos100 !

  • However I meant to post a screenshot to show you how to do it, but my dropbox had a melt down. But got it working.

    But what I meant you could do was like this.

    It doesn't really help you to put the "Set text.." in an every tick, if it only updates once every 1 sec (60 ticks).

    A good idea I think is to use Variable objects instead of local and global variables. Because when you get a lot of them you will end up with a huge list, that you have to scroll through, which gets annoying.

    So instead you can just make a small sprite (16x16) and place it outside the layout and call it "Game_settings" or whatever. And then you add all the variables you need to this one instead. That way you can easily find them when you need them and you can use Booleans as well. Personally I rarely use anything other than those for global and local variables. And if you need them in other layouts, you can just turn the object global in the properties settings.

  • Yeah, sometimes I use objects too, but when you use a lot of variables into a single expression, the expression starts getting really long. Example : Variable1 + (Variable2*Variable3) / Variable4 feels better for the eyes and the mind than variable_object.Variable1 + (variable_object.Variable2*variable_object.Variable3) / variable_object.Variable4

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