Difference between constant and static constant?

0 favourites
  • 10 posts
  • Is there a particular reason why you can set a constant to be also static? Is there any difference?

  • Static means it will not reset every tick. Constant means it can't be changed at all. If a variable can't be changed, it can't be reset.

  • That's the point. Constant keeps the value every tick anyway that's why I'm asking if there is a purpose to allow checking both checkboxes for constant and static value. Thoutght maybe C2 engine treats constants and static constants somehow different for some unknown to me reason. But I guess it's just a "dummy" option in the editor.

  • Ah, I thought you asked why they can't. Was probably just easier to add checkboxes lol.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Can someone explain the difference to me a bit better? I thought variables didn't reset unless you tell them too. Static means you can change them and they stay to what you changed them too. What is the point of having a variable as a Constant if I never change it and the user won't have access to it anyway?

    I have a few Variables that are Static and I never change them and they never get changed.

    What am I missing on this?

  • Regarding data holding:

    Non static variable keeps it's value only for a one tick. So you can use it for various calculations per tick and you don't have to reset this variable as it gets reset by the engine at the end of current tick.

    Global variable keeps it's value all the time. If you want to reset a global variable then you have to do it manually or use system action to reset all globals.

    [EDIT]: Static variable keeps it's value all the time like globals. Difference is in the scope.

    Constant keeps it's value all the time, but you cannot change it's value programmatically. You may think of it as of a helper feature.

    If you need to use a callback in many places, or some Dictionary or Array index, then you would need to type it manually everywhere. Once your game evolves and you have to change indexes for instance, you would have to change it again in all those places. Now when you create a constant with the index value and use use it instead, you can change only constant's value (one line) and all the indexes will be changed.

    Also Array.At(objectId, ATTACK_DAMAGE) is more human readable than Array.At(objectId, 0).

    Constants help you maintain your code, give better readability and are strongly suggested to use for any data which will remain untouched the whole game. Also they do not affect performance in any way so don't worry about that. They are here to help you, it's wise to use them whenever it's possible.

  • As you have noticed 'static' is ghosted for global variables. Makes no sense for global variables anywayz.

    Because 'static' is meant for local variables. Local variables are used in a subevent, on any sub level. A non 'static' local variable will reset everytime its top-event runs. A 'static' local variable will not reset. You can change a 'static' varaible anytime you want, als long as it is in its scope.

  • I believe static variables are not effected by a change in layout according to the manual:

    If the variable is marked static in the Event Variable dialog it will persist its value permanently, like a global variable.

    yours

    Winkr7

  • Well, when static it does not get a reset when running its top-event. So, yes, then it acts as a global, for that 1 layout. Its not a global as in the sense of 'Global'.

  • I believe static variables are not effected by a change in layout according to the manual:

    If the variable is marked static in the Event Variable dialog it will persist its value permanently, like a global variable.

    yours

    Winkr7

    Yes you are right. I usually set static values on gorup toggling, that's why they "reset" for me, but actually I am reseting them. So correct - they do not reset by default after restarting the layout. Thanks for pointing that out.

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