You are here

The Case of the missing Switch (Tutorial)

Genisys / Guile
Old Vault Category: 

Leaving the fundamentals (Variables), I'm going to explore a more verse & critical topic, making good use of the switch / case function, because this is something you are often want to see in a lot of scripts, as many things require conditionals, and the ole if / else if / else structure is not always needed for conditionals, indeed I've learned to use switch / case statments instead in most intances, mainly because if a script is going to fire a lot, it's code needs to be highly efficient, and this leads us towards better module performance  & ultimately lower scripts.

If you take a good look at my PC Assistant or the Mil Tailors Revision that I created & shared to the nwn community, specifically the Conversation / Scripting Routines I  used in these systems, you'll find that I made good use of switch / case statements to handle entire conversations, also I used Conversational Tokens and a function that interpreted what the PC was clicking on based upon variables set upon the PC when they click on lines, to let me know what section of the conversation the PC is in, this way a switch(nSec) can help me write code in chunks for the conversation, with functions for both Starting Conditionals (Text Appears When) & the Action Taken Event for conversations...

Of course those scripts are far above the novice scripter, but those wanting to learn how to code really awesome conversations that normally would use / require a lot of scripts, or want to rework existing conversations, then I encourage you to learn how I created these conversation, especially the Wand of Alterations I recently created (as it's much easier to follow).  Using simple switch / case statements in functions is highly recommended, and below I'm going to show you a few functions that properly use them to increase efficiency of a function & script.

string GetItemBaseName(object oItem){

   string sReturn = "Invalid";

   int nBase = GetIemBaseType(oItem);


   case BASE_ITEM_AMULET: return "Amulet";

   case BASE_ITEM_RING: return "Ring";

   // etc....


 return sReturn; 


The above function will let us get the Item's base type name, e.g. it returns the string "Ring" for any Ring, this can of course be used in multiple scripts in different ways, like in a script that changes the name of an item to a new name.  Ultimately reducing coding redundancy & increasing code efficiency should be the objective of every coder, because writing code repeatedly over years gets very tiring and is very time consuming, as I cannot tell you how many times I've written the same old loop to get all items by a tagname or something along those lines!  This is one of the reasons why the community was calling for Custom Functions when NWN had first begun (Like the one lightly outlined above), so that we could eliminate a lot of redundant coding with simple new functions.

Switches are so very needed in a lot of instances, especially when working with items, if you dig through the includes in my forge systems, you'll find a lot of those being used, because I just didn't want to go through the old if/else if/else routine, and many functions were called frequently, so I didn't want to have to code it for every line in the conversation, no way!  Ultimately, using switch / case functions in creative ways will lead you to create more profound programs, and if you look at Higher Ground's Sim Tools or Legendary Leveler scripts, you'll find yourself overwhelmed by the amount of data they packed into those scripts using switch / case functions...  (Also make good use of Script Templates as well!)

Every coder has their own style / ways of doing things, I'm just trying to point you towards the better way that I've found while learning to code in NWScript, and another example of rock solid use of switch / case statements is....  In a forge conversation I did a long time ago I was setting variables on the PC to determine which line the PC had clicked on, these variables were used in a custom function to interpret what the PC had selected, before I performed the final action (Forging an Item).  But there is also use of variables set on placeables, which can be interpreted in an entire host of different ways.  For example, let's say we created a lever and wanted to create a way to change what the lever did, so we set a variable in the toolset on the lever LEVER_ACTION int 1  this of course can be 1 - 5, and in the actual function that performs actions when the lever is pulled we will interpret the variable, as the builder can change the variable on different levers painted inside the module at different places... (See code example below)

void PerformLeverAction(object oLever){

  int nWhich = GetLocalInt(oLever, "LEVER_ACTION");


   case 1: {

    // Perform this action... 

   } break;

   case 2: {

   // Perform this action instead...

  } break;

  case 3: { } break;

  case 4: { } break; // etc.. etc..

 }// end switch

} // end function 


Now, we have a fully functional template lever, that can do 1-X different things, depending upon how you setup the function, and you can write notes in the comments of this Template Lever (as it will be used many times possibly for different functions) to let you know what each # actually does, e.g. 1 = Open Nearest Door  2 = Set Variable on Object By THIS TagName, etc..  I cannot express enough the need for quality use of the switch/case function, because the function is so very nice at keeping code rather tidy & readable, efficient, and can help eliminate redundancy!  Hence, the call for Template Stuff in the scripting forums...  (The community needs high quality custom scripted systems / templates, definitely!)  So if you are learning to code and find some creative way to use switch / case statements to create functions that are very useful or templates that are awesome, please be kind enough to share what you create, because community co-op is so very critical at this late in the life of this game.  (Because many of the greatest scripters are gone now)

Using switch / case in conversations have helped me reduce the original 250+ Scripts that bloated your module in Mil Tailors, reducing it down to a mere 50 scripts, and that my friends is very high quality scripting, for it's far easier to dig through 50 scripts that are well coded, then digging through 250 scripts with a bunch of small functions in them.  Though, for the sake of complexity, scripting for conversations is anything but novice, and is usually a task more suited for veteran coders.  Nevertheless, making good use of switch / case statements can help you become a much better coder indeed!

Migrate Wizard: 
First Release: 
  • up
  • down