You are here

Constants Usage & Scripting (Tutorial)

Genisys / Guile
Old Vault Category: 

Whenever you open up a configuration script, like (gen_tres_config) for example, you'll usually find bizaare variable names in all capital letters....

const int GEN_TRES_RARE_FREQ = 24; // Default = 24 (24% chance of spawning in a rare item)

These constants are variables with unique names that never change, though the data associated with them may be changed, you need to rebuild your module (Scripts Only) afted editing configuration scripts, and if those scripts are part of a hak file (Which may be the case), then you will need to export those changes to the hak file FIRST, because the changes won't stick to the actual file in your module, as the hak file will override the module file, and any changes made the module file, will in fact be overwritten by the hak file's version of the configuration settings.

These configuration scripts are often included in the module & the hak to let you know they exist, but are needed in the hak file where most of the scripts exist, because a builder may want to keep his main module free of the numerable scripts that make certain systems run (e.g. Like Mil Tailors), and so you need to be aware of these facts as a builder.

These constants (const) let the script compiler know that the variable associated with this data will never change, and the data that is tied to it will never change while the module is running as well, but why use constants at all?  Well, there is a good reason, because these constants can be used throughout script systems, and they can actually control scripts & how they function.

Whenever I make a system, whether it's a forge, a conversation that PCs will be using a lot, or whatever, I try to make configuration scripts with as many settings as possible, and this is a great scripting habit to pick up.  Mainly because, the best systems will give the novice scripters more ways to control what you have created, and the more controls you give a script or scripted system, the more robust the script / system becomes.  Anyone can write simple scripts, but if you ever want to rise to a better level of scripting, then you need to familiarize yourself with constants, their usages, and how to implement them throughout scripts.

One thing however to be aware of, is you don't want to create too many of these, as a module does have a limit to how many constants it can use (I don't remember the figure off the top of my head, maybe 300 or so?), anyway, another thing to remember is, constants MUST be  unique, so make sure you use unique naming conventions, like GTS_LOW_FREQ, and in this example GTS stands for gen treasure system / low being the treasure type, and freq being the frequency of the treasure spawning. (Just as an example of naming convention)

Because all variables are public in NWN, we need to remember that constants however act differently, in that they can never be changed by assignment / coding, and using them in coding gives us the ability to point to data which we may have long forgotten what it's value may be, or it tells us what data we are working with.

const string LOC_DB_NAME = "X2_DB_LOCATIONS";

Here we know that LOC_DB_NAME represents the true name of the database we are storing locations to, we don't know the actual database name, but we will likely use this constant variable multiple times in scripting while scripting a database script, and it saves us from having to type the "" as well, for constants only require the constant name!  Which is very useful for long float numbers that we don't want to remember, especially if we are using it frequently throughout coding:  

const float PI = 3.14159265; 

float SIN(int nParameter) { return (nParameter*PI)/180; }  

Constants are awesome for use in math functions :D


Thanks for reading, I may edit this later on to add more on the topic...


Migrate Wizard: 
First Release: 
  • up
  • down