0 Favourites

How do I use constant variables?

  • 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?

  • 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.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • so basically, while i don't change it in the code any variable is a constant :I

  • 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.

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