whats a constant? there usally all in Upper case and have underscores when there are multipul words
here is an example:
local AMONG_US = 'AMOGN US'-- amung us beat boxing in the background
what are they for?
whats a constant? there usally all in Upper case and have underscores when there are multipul words
here is an example:
local AMONG_US = 'AMOGN US'-- amung us beat boxing in the background
what are they for?
Constants are basically just read-only variables. Lua doesnât have any though so this is more just an indication not to set this variable
These are called variables. They are things that can be used in code for easy access to specific text/code/anything else. They are essentially shortcuts.
Hereâs an example:
local map = game.Workspace.Map
print(map.Name)
you mean not to change sorry i have -1 brain cell
i know what a variable is lol i am no noob
dude then why did you ask
did you srsly ask for no reason
If youâre talking about the underline: I suppose itâs just personal preference
Usually some people would define their variables like these:
local RUN_SERVICE = game:GetService("RunService")
local MARKETPLACE_SERVICE = game:GetService("MarketplaceService")
I think itâs because theyâre used to using a whole different programming language, such as C+ & Python but I could entirely be wrong
Although this is valid, you still should be careful when using underlines such as _VERSION
and etc since those are reserved
i am asking what a constant is i know its a variable i am asking why is it called a constant maybe because its a specials variable idk so that why i ask
A constant is literally the exact same thing as a variable; it just has different wording
Yeah, the underline is more personal preference. Also, I think some people prefer to use variable names in all caps for settings or something similar.
why it called a constant then?
Actually a quick search shows this about a constant:
âA fixed value. In Algebra, a constant is a number on its own, or sometimes a letter such as a, b or c to stand for a fixed number. Example : in âx + 5 = 9â, 5 and 9 are constantsâ
So I suppose theyâre just basically values that canât be changed
so constants are just an indication this variable is not to change
Lua doesnât have any constants, that was what I was saying
I suppose so, although there might be some instances where you could use constants in functions
@robloxcommunityplus Really? Are you sure that you couldnât just define a variable as
local A = 20
And not change its value?
True, but there arenât really any true constants in Lua. (Unless youâre talking about metatables, but in that case, you can still just use rawset() unless youâre using newproxy or something)
As others have said, but just to be clear:
There is technically no difference in Roblox between a constant and a normal variable, but constants are just variables which shouldnât be changed during the execution of the script.
Theyâre usually used as like a âpropertyâ for parts of the script. E.g. in my rain module, one of the constants is RAIN_DENSITY. Youâd set RAIN_DENSITY to whatever number you want, and no other part of the script changes it.
Theyâre usually put in CAPS to signify that theyâre a constant.
thx so much i am so happy my constant will never change
local BEST_GAME = "AMOGN US"-- i know it should be roblox but...
this devforum thread is making me rethink everyting
Iâm sorry for bumping, but Iâm going to make an essay about what are constants and the history behind them being noted with UPPER_SNAKE_CASE
for future readers having this question.
If we see the meaning of the word âconstantâ itself, it means Something that does not or cannot change or vary.
How is that applicable to programming?
You see, maybe you have some variable that holds a certain value and you donât wanât to change it no matter what, or perhaps you donât need to make it non-constant but the language offers optimizations for them.
Lua doesnât support constants, and to differentiate between non-constants and constants most people use the UPPER_SNAKE_CASE
naming convention.
But hereâs what happens if you try reassigning a constant variable in TypeScript:
UPPER_SNAKE_CASE
?It was first used in Assembly programs when Unix was widely used (and it was considered a âlower case OSâ), so the chads that programmed during those ancient times used this convention as a way to differentiate those two.
But thatâs not it! Things actually really heated up with C.
So in C we have what we call preprocessors
. Before compilation, the compiler will read those through and do whatever those are supposed to do.
There is this preprocessor called #define
, whose job is to replace any mentions to the name given to it to the value it holds. Consider the following snippet:
#include<stdio.h>
#define someValue 1
int main() {
printf("%d", someValue);
}
So, what happens is: the compiler will read through the file, see that we have a #define
, get itâs name and value and cache it. When it sees a mention of the name, it replaces it with the value.
But, there is a catch; consider the following program:
#include<stdio.h>
#define someInt 10
struct test {
int someInt;
char someString[50];
}
int main() {
struct test = {
.someInt = someInt,
.someString = "Great!"
};
}
Seems normal? Yup, it does.
But you wanna know how the file looks after the compiler finishes preprocessing?
#include<stdio.h>
#define someInt 10
struct test {
int 10;
char someString[50];
};
int main() {
struct test = {
.10 = 10,
.someString = "Great!"
};
}
WAIT, what happened to the someInt
member of the test
struct???
So you see, #define
s donât really act as variables. The compiler will literally replace the name by the value no matter what the context is!
So, where does UPPER_SNAKE_CASE
come in?
Well, by using BIG_SHOUTY_NAMES
, the chances of accidentally reusing the name drastically decreases!
#include<stdio.h>
#define SOME_INT 10
struct test {
int someInt;
char someString[50];
};
int main() {
struct test = {
.someInt = SOME_INT,
.someString = "Great!"
};
}
And that sums up everything.
I hope this was an interesting read!