1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Any advantage to using consts?

Discussion in 'Scripts' started by greatguys1, Oct 10, 2017.

  1. greatguys1

    greatguys1 Developers

    132
    2
    Apr 20, 2013
    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: Oct 10, 2017
  2. MaximusPrimus

    MaximusPrimus New Member

    8
    1
    Sep 6, 2017
    Male
    It makes you horny.
     
  3. Thothie

    Thothie Administrator Staff Member

    14,637
    12
    Apr 8, 2005
    psycho-oligist
    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 likes this.
  4. greatguys1

    greatguys1 Developers

    132
    2
    Apr 20, 2013
    Yes
    Good info, thanks :)
     

Share This Page