Any advantage to using consts?

greatguys1

Epic Adventurer
MSC Developer
Warriors of the North
MSC Archivist
Joined
Apr 20, 2013
Messages
339
Reaction score
60
Age
26
Location
Yes
Is there any particular advantage to using consts? Does it use less resources? Any neat tricks you can do with it? Is it just good practice?
 
Last edited:

Thothie

Administrator
Staff member
Administrator
Moderator
MSC Archivist
Joined
Apr 8, 2005
Messages
16,342
Reaction score
326
Location
lost
In scripts, Constants allow your top script to override values in the lower scripts in a fixed fashion. Thus you can have a base_script with a bunch of defaults, and said base_script can be #included in another script, but all its values can be altered by merely re-entering its Constants before the #include line. If you instead use setvards, the reverse behavior becomes the norm - the setvards in the top script will be overridden by those in the lower scripts, due to order of execution.

For example:

base_saysomething.script:
Code:
{
    const SAY_STRING "I r gonna say this"
}

{ game_spawn
    saytext SAY_STRING
}
say_wow.script:
Code:
{
    const SAY_STRING "Wow!"
}
#include base_saysomething
say_wow.script would say "Wow!" on spawn.

...but if we did that with setvards instead:

base_saysomething.script:
Code:
{
    setvard SAY_STRING "I r gonna say this"
}

{ game_spawn
    saytext SAY_STRING
}
say_wow.script:
Code:
{
    setvard SAY_STRING "Wow!"
}
#include base_saysomething
say_wow would instead return "I r gonna say this".

Constants are present during the pre-load phase, so you can also use them to define precaches and the like. Setvards, on the other hand, only happen at run time.

Unlike most languages, constants don't save anything in the way of resources, but they do let you easily change any values by simply #including the script into another one or otherwise altering them. Beyond that, yes, it's just the old "good coder's practice" of always having everything declared ahead of time.
 

greatguys1

Epic Adventurer
MSC Developer
Warriors of the North
MSC Archivist
Joined
Apr 20, 2013
Messages
339
Reaction score
60
Age
26
Location
Yes
Good info, thanks :)
 

greatguys1

Epic Adventurer
MSC Developer
Warriors of the North
MSC Archivist
Joined
Apr 20, 2013
Messages
339
Reaction score
60
Age
26
Location
Yes
all its values can be altered by merely re-entering its Constants before the #include line.
I feel the need to emphasize that, I just spend 5 hours trying to figure out why my constants weren't overriding and this was why :I
 
Top