Flag testing

In creating conversations and quests in the previous chapters we used flags, which, depending on the situation, were assigned the value 0 (false) or 1 (true). When preparing the quest to be used in this manual, the greatest problem I encountered was checking whether a given flag actually had the appropriate value. Unfortunately, the D’jinni Editor doesn’t allow you to check flag status. The good news, though, is that this can be done by using a script. Just as a refresher, we used the following flags:

orders_received,

leader_killed,

results_reported.

Now let’s create a new script. Select New -> Neverwinter Script (.nss) from the 'File menu:

The new neverwinternights script window will appear:

As usual, let’s start with a comment:

Now type in the following line:

Note that at the end of a line ending with the keyword “if”, don’t add a semi-colon. This is extremely important. If a semi-colon is used in this situation, the script will not work properly.

The keyword “if” is a conditional instruction. The task of a conditional instruction is to control a script whose parts will only be executed when particular conditions are met. As a result of executing this conditional “if” instruction, the value 0 (false) is given when the condition is false, and the value 1 (true) is given when the condition is true. The GetDialogFlagValue function draws on the value of the dialogue flag of the given name.

The whole expression can be read as follows: when the value of the dialogue flag called orders_received is equal to 0 then… Of course we still need to finish the script and determine what’s to happen when the value of the flag is equal to 0. Write the next part of the conditional “if” instruction as follows:

The PrintString function displays the given text in the Aurora log. The whole phrase can now be read as follows: when the value of the dialogue flag called orders_received is equal to 0, then display the text Flag orders_received = 0 in the event registry window. Easy, right? Now, we have to write a conditional “if” instruction for when the flag orders_received is equal to 1:

The same script can be used to check the other two flags. Copy the whole block of code and substitute the other flag names for orders_received:

And now for something a little different. Anyone old enough to have learned to code from books will probably remember that early computer programs just displayed text, and the text “Hello World” was used as a common introduction to scripting. So, in order to honor old conventions, let’s add the following at the end of the script:

The coding is a bit longer now but it’s also got more style. Select Save As… from the File menu:

This opens the standard Windows window used for saving files. Go to the Data\Scripts folder (where all scripts are saved), type in test_flag as the name and click Save. Remember that the name of the file cannot exceed 16 characters. Make sure the file is saved in the .nss format:

If a different type of file is listed instead, it probably means that you’re trying to save the content in another open window by accident. Before saving, make sure that the active window is in fact the new neverwinternights script window. If it is inactive, the title bar will be grey:

A different color means that the window is active:

An asterisk before the name in the title bar means that the file has not been saved since the last change was introduced. Remember that you can always save a script to a particular module. To do this, select Save new neverwinternights scripts from the File menu, then choose Module in the Save File window.

After saving the file, check to see that no errors occurred when saving and compiling the script. Open the Resources Explorer window. To do this, select Utility Windows -> Resources Explorer from the View menu:

In the Resources Explorer window, expand the Data and then Scripts trees. The test_flag.nss script should appear:

Now switch to game mode. To do this, select Play from the Game menu:

After switching to game mode, load the module. Once it has launched, go to the Resources Explorer window and right-click the test_flag.nss script. Select Run script from the drop down menu:

In the Aurora log window you should have the following:

As you can see, all the flags have an initial value of 0. Now, take the quest from Jethro and relaunch the script. Then kill the bandit leader and relaunch the script again. You’ll see the changes. Thus, as you can see, scripts can make life much easier.