You are here

Variables Preferred (Tutorial)

Genisys / Guile
Old Vault Category: 

Variables are something you create out of thin air, just like in math class when you saw  n = 4  or a = 2 , instances of these variables are integers, where a whole number is represented by a letter, likewise variables are names that represent data, and within NWScript we use variables a lot to declare something.  For example, if I declare object oPC = GetLastUsedBy();  Then I'm describing the variable (oPC), which is an "object" (The type of data that the variable name points at), and GetLastUsedBy(); actually tells us who object oPC is...

Variables are such an amazing thing, they lead us towards amazing & dynamic systems, that run mostly from a little code and some input data, but more importantly any variable can be different types of data. but only one type of data at a time.  I'm talking about variables for a reason today, because a lot of what you will encounter in Neverwinter Nights, may use variables to control it, either on an object itself (Like a monster, a placeable, an item, or even a waypoint), and these variables can come in different forms!

For instance, the name of a waypoint is a variable, though it has an assignment place given to it directly in the waypoint creation / editting / tool window, every piece of data that is part of an object is in fact a variable, it's just that we haven't put it into a variable in a script yet.  For example:  string sWaypointName = GetName(oWay);  of course we haven't defined oWay in this example, but you know what information we are extracting from oWay (It's name) and we are assigning the data (The waypoint's name) to the string variable's name (sWaypointName)...

Variables come in 4 major data types....   string, object, int (Integer), and float.....  (Though this is NOT all of the variables available, but for the ease of this tutorial, I'm gonna keep things short and simple.  int n = 4;  is how we properly announce an integer in NWScript, but because n isn't very discriptive, we usually attached more to it, like   int nGP = 4;  where nGP, as you might have guessed is representing a gold piece amount of 4....  We can declare a variable without assigning them data, if we plan to use it redundantly, but it's always wise to go aheead and assign a variable some data to start, though we can change it later, example;  int nGP = 0;   if(GetGold(oPC) >2000) { nGP = 200; }  Here we declare nGP gave it an integer of 0, then later change it after checking the PC's gold (that they have on them)....

Placeables can also store variables, these are considered persistent (Unless deleted), for everything created inside the module stays unless the object is changed.  Variables that are assigned to a PC themselves may actually dissappear, unless the variable is set on an item the PC has in their possession.  So, as a good practice, if we through scripting set a variable on something, instead of in the Toolset / module, then it's only temporary (until the module restarts or the game ends), but if the variable is set on an item and the player's character is saved, then it does indeed become persistent, IF the player is playing on a server, offline however, the variables only ever stick until the PC saves their character, even those set on items...

For this reason, it's important to know the difference between a temporary & a persistent variable, those set on placeables, doors, waypoints, or spawned in creatures (at the time of creation), will stay with them unless destroyed, then of course all variables are gone with the destruction of the object that is NOT a PC character.  A PC may die, but their variables will still stick to them, but if they log out, that may not be the case! (If on a server)  Now you know the difference between persistent & temporary variables, hopefully, learn to use persistent variables on placeables, NPCs, and these can be used in a whole host of very useful ways!

When I create template placeables, like portals, I use a variable on the portal to determine where the portal leads to, instead of writing a script every time I want to create a new portal, I simply write one script that reads the variable (Example here)  DESTINATION_WP_TAGNAME   string   "ThisTagName"  on the portal to determine which waypoint I'll be jumping the user to.  You should also consider this methodology as the "preferred" way to script things, because it saves you time, and prevents your module being loaded up with redundant scripts (See the modules with 5,000++ Scripts, not good & a nightmare to dig through believe me!)

We should as a standard form of practice, while learning to code, or coding, remember that if we can use persistent variables, and template objects (Scripted one time for a specific purpose using variables set on an object persistently), then we can save ourselves A LOT of work later on.  Below you will find a simple script I used to setup my Template Portal, it's rather complex to novices scripters, but you should be able to see I am using multiple variables set on the placeable portal to control how each portal I create functions.... e.g. GetLocal.......();

I can't paste code in these forums, even with htlm code tags, for some reason, so here is a link to the code that I was trying to share...

(This script was taken from a template script & modified by me for persistent variable use, the preferred methodology.)

Migrate Wizard: 
First Release: 
  • up
  • down
Thunderstorm Witch

Looks like the link has expired... a pity.

Thumbs up for that article!

Never underestimate the power of variables!

  • up
  • down