const vs global

Started by KAKAN, Sep 24, 2015, 04:37 PM

Previous topic - Next topic

KAKAN

Hello Guys,
I have seen many things in some Global Variables and some in const, what's the difference between them?
Ex of global variable:-
try <- "Hey"Ex of const:-
const Name = "Hell"
oh no

.

#1
Constants are a separate table from the root table where the inserted indexes cannot be altered once created. Thus the name 'const' which is short for constant. Actually they can be modified but not from the currently compiled script because it requires unconventional methods which are not available at compile time. The only way to change them is to alter the indexes directly in the const table through something such as `getconsttable().rawset("Name", "Heaven")`

Global variables on the other hand are simple variables that reside in the root table. And we all know every shitty scripter in here loves to spit values in the root table not knowing (or caring) that some of those names could collide and create a clusterf* of problems for us to debug here on the forum.

In it's current state, your code should fail though. Simply because `try` is a registered identifier by the language itself. Hopefully that's just for the sake of example.

But essentially, the same procedure used with the const table can be used on the root table to alter values `getroottable().rawset("try", "Bye")` That's the same as `"try" = "Bye"` and it's exactly what happens behind the scenes when someone attempts to interact with the values.

I've explained a couple times the fact that Squirrel is basically a hierarchy of tables similar to the JSON documents. And that once you imagine it that way, it starts to make sense.
.

KAKAN

Thanks! Both are needed according to there function
oh no

.

Quote from: KAKAN on Sep 24, 2015, 05:02 PMThanks! Both are needed according to there function

Yeah, but let me show you what's wrong here:
try <- "Yolo";

try {
    print(try);
} catch (e) {
    print(e);
}

Output:
./bootstrap.nut line = (1) column = (6) : error expression expected
[WARNING] Could not load script './bootstrap.nut'
[WARNING] expression expected
[ERROR]   No Squirrel gamemode was specified.

Hopefully you can figure why that will happen.
.

KAKAN

Yea, I know about those try-catch blocks
oh no

Thijn

Quote from: KAKAN on Sep 24, 2015, 05:10 PMYea, I know about those try-catch blocks
That's not what S.L.C. is trying to say :P
"try" can be compared to a const, so setting it like a global will make squirrel go *poof*.

KAKAN

Hmm, Thanks!
Anyways solved
oh no