i dont really see myself using constants at any time… whats the use for them
i used it to define a variable but i dont want it to be ever be changed, so it’s like, only “get” from it, not “set” it.
cant you just use normal variables?
const number = 10
var playerspeed = number
you can (or should) also be able to do:
var number = 10
var playerspeed = number
theres no need to use constants imo
if you dont realize yet, enum also uses constant as its element
as for const vs var
yes you can argue var can do all the job of const can do, but if you really want to limit what a variable can do like for instance only able to use it for getting its value and never changed thru runtime, you will pick const
but if the constant you make was used for it to not be able to change, why’d you try to change it trough code at all
you can just use a variable as a constant and never change it, i dont see how the code you make would try to change the variable unknowingly
that’s the idea, let’s say you have a 10 seconds delay that you will want to use it for a whole code, you can just specify it like
and just throw in DELAY to wherever code that needs it
this also helps track and search easier
and whats the benifit of using const instead of var? const not being able to change as a value doesent really make it useful in my image, for coding stuff at least… not the api or enum or anything
example of the DELAY, it’s intended not to be changed in the first place, so why dont var delay? it will be read by other ppl or other developer that this variable will still have somewhere being able to change its value, dynamically
unless you working alone for your project, you know what you are reading and where it goes, then it doesnt matter.
so the only usability for constants is like for others to use your code? fair… a feature around it doesent really make sense to me but not that it matters if there is
so it’s more like, to use the correct type of thing or store your value the right way.
even math/physics formulas has constant in it, come to think about it
would be weird that godot dont have it, no?
i mean if theres a use for them then i guess it’d be a problem or something
Ok this could get way more complex, but a const doesn’t benefit ONLY from its immutability. There’s a different memory management for constants, at least in compiled languages like C#. The same happens with the static keyword.
This means that the value will be resolved at compile time. There will be no memory allocation at runtime when the value is consumed, since it’s value will be stored as metadata.
Will your game run faster if you use constants? I don’t know, but it’s always a good practice to treat constants as constants
That’s the point of const, so the interpreter can say to you “Hey, why’d you try to change this var in code? It’s not supposed to change”. Will you remember in 6 months time every var that’s not supposed to be changed? Will your team mates know? Why not let your tools help you. Communicate your intent through code and help the interpreter catch any mistakes you make in the future.
There is a benefit to constants typically. Not sure if gdscript actually leverages const variable, but for performance reasons, every time you use it, the symbol for it should be replaced by a literal value it represents, and will not use any memory commands to retrieve it, as it will be part of the code.
It’s like a “magic” value that you can name and place it in a single convenient location to modify whenever needed.
Almost every programming language has a const feature. C++ specifically has a steroid version called constexpr, that expands into any possible compile time literal value that can be determined at compile time, even if the actual expression can’t be made const you are telling the compiler to try. This method is highly encouraged in performance c++ to get as much gain as possible leveraging the compiler to figure it out for you.
const pi = 3.14
When writing software, future you and past you are also „others“. Declaring it as constant can also help future you understand the intention of it. Higher level code is mainly written for humans to read and understand.
i intended to ask if like it has any uses for scripting but i guess i didnt mention it