Difference between constant and static constant?

Discussion and feedback on Construct 2

Post » Wed Jan 27, 2016 5:54 pm

Is there a particular reason why you can set a constant to be also static? Is there any difference?
Image
B
27
S
14
G
54
Posts: 853
Reputation: 31,110

Post » Wed Jan 27, 2016 7:05 pm

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.
Image
B
234
S
27
G
13
Posts: 1,784
Reputation: 18,274

Post » Wed Jan 27, 2016 9:46 pm

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.
Image
B
27
S
14
G
54
Posts: 853
Reputation: 31,110

Post » Thu Jan 28, 2016 9:27 am

Ah, I thought you asked why they can't. Was probably just easier to add checkboxes lol.
Image
B
234
S
27
G
13
Posts: 1,784
Reputation: 18,274

Post » Sun Jun 19, 2016 9:01 am

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?
B
50
S
30
G
12
Posts: 203
Reputation: 11,006

Post » Sun Jun 19, 2016 9:56 am

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.
Last edited by BackendFreak on Wed Jun 22, 2016 9:52 pm, edited 1 time in total.
Image
B
27
S
14
G
54
Posts: 853
Reputation: 31,110

Post » Sun Jun 19, 2016 4:14 pm

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.
B
26
S
15
G
23
Posts: 2,045
Reputation: 17,081

Post » Wed Jun 22, 2016 5:15 pm

@BackendFreak

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
B
26
S
7
G
3
Posts: 152
Reputation: 2,971

Post » Wed Jun 22, 2016 5:46 pm

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'.
B
26
S
15
G
23
Posts: 2,045
Reputation: 17,081

Post » Wed Jun 22, 2016 9:50 pm

winkr7 wrote:@BackendFreak

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.
Image
B
27
S
14
G
54
Posts: 853
Reputation: 31,110


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests