How do I use constant variables?

Just started using Construct 2? Post your questions here

Post » Sun Jan 15, 2017 2:35 pm

I really don't understand what's the point of a constant variable, if i don't want a variable to change, then i don't modify it in the script, so, how do i use constant variables? in wich case i need to tell the engine that i don't want to modify this variable even when i'm using an action to change it? or why i would do that?
What's the advantage of a constant over a normal global?
B
19
S
3
Posts: 93
Reputation: 1,009

Post » Sun Jan 15, 2017 3:14 pm

Constants are meant as a convenience to avoid having hard coded numbers in your code. Instead of having something like a = b/60, you'd have your constant set as 60, and write a=b/constant. The advantage is that if you need that value in multiple places, and decided to change it later, you only have to change one value, rather than searching for it everywhere it is used.

It's up to you if you want to use a constant or not.
ImageImageImage
B
71
S
21
G
230
Posts: 3,688
Reputation: 128,847

Post » Sun Jan 15, 2017 3:15 pm

so basically, while i don't change it in the code any variable is a constant :I
B
19
S
3
Posts: 93
Reputation: 1,009

Post » Sun Jan 15, 2017 3:25 pm

No, a constant *can not change*. Whether a variable does or doesn't change doesn't make it a constant, just because it didn't happen to change. As I said, if you don't find value in it, don't worry about it.

Edit: it's also a matter of identifying the value. In a=b/60, what does 60 mean? If I have FPS=60; a=b/FPS, that has more meaning (assuming it's clear that FPS is frames-per-second). The named constant helps the next person who looks at the code to understand what is going on, rather than just having numbers scattered around.
ImageImageImage
B
71
S
21
G
230
Posts: 3,688
Reputation: 128,847


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests