Note: LucasForums Archive Project
The content here was reconstructed by scraping the Wayback Machine in an effort to restore some of what was lost when LF went down. The LucasForums Archive Project claims no ownership over the content or assets that were archived on archive.org.

This project is meant for research purposes only.

How to make a new "Global Variable"?

Page: 1 of 1
 SithRevan
08-06-2006, 2:53 AM
#1
I was wondering if somebody could help me with making a global boolean, or if you could point me in the direction of a good tutorial on how to make one, Thanks.
 RedHawke
08-06-2006, 4:00 AM
#2
Don't need a tut for this, simply add your new variables line into globalcat.2da. ;)
 SithRevan
08-06-2006, 12:49 PM
#3
Is it that easy? Woudln't there be a script or something needed to make it work?
 Princess Artemis
08-06-2006, 1:40 PM
#4
The scripts to make a new global work in a dialogue is a_global_set for K2, in K1 there doesn't appear to be a universal script to set a global (unless I missed it). For K1, you'll need to write a script that uses SetGlobalNumber("newglobal", number to set) or SetGlobalBoolean("newglobal", TRUE or FALSE), which you can then attach to whatever it needs to be stuck to--if it's a dialogue, add your new script to it.

K2 uses the same functions within a script, if you're not setting the global from inside a dialogue.

To use a_global_set, in DLGEditor, below the section with the text of your dialogue, there are sections for two conditionals and two scripts. Put in a_global_set for one of the scripts, in P1 put the number you want it set to, and at the end of that under Strin Parameter, type in your new global name.
 Darkkender
08-06-2006, 2:13 PM
#5
Is it that easy? Woudln't there be a script or something needed to make it work?

Yes it is that easy. The primary purpose of scripts with Globals is to interact and change these globals as you progress through the game. While Princess Artemis mentions the method above to run it via scripts 100% of the time. This works for small mods that you only need to create a few globals. If you decide to do some sort of larger scale mod it is much better to interact with the 2da files the reason is the more functions and commands you dump into a script the greater your chance of delays in the game. Also having a master globals list is best for multi-modder mods for various reasons. This would be for just the same reason that Game Developer's use these same methods.
 stoffe
08-06-2006, 4:54 PM
#6
While Princess Artemis mentions the method above to run it via scripts 100% of the time. This works for small mods that you only need to create a few globals. If you decide to do some sort of larger scale mod it is much better to interact with the 2da files the reason is the more functions and commands you dump into a script the greater your chance of delays in the game. Also having a master globals list is best for multi-modder mods for various reasons. This would be for just the same reason that Game Developer's use these same methods.

Huh? I didn't understand much of that, could you explain what you mean in more detail? :) I'm curious, how would you go about checking if a global is set, or set it, if you don't use any scripts, via 2DAs only? What is a "master globals list"? Seems like there is something I have missed...
 Darkkender
08-07-2006, 2:38 AM
#7
What I was refering to from Princess Artemis's post was her discusing creating and managing the Global 100% from scripts. This works when you have only a couple of globals to work with in a mod. However when you work on a large scale mod either by yourself or with a team it is better to have a master list of globals. For the Kotor games this is globalcat.2da. With globalcat.2da you define the global variables then you use the scripts to interact with your globals.

When I was mentioning the scripts causing delays within the game I was refering to the fact that the scripts are a dynamic part of the game which means they are constantly changing the game and the modules that they are running within. Everytime you ask a script to create some sort of object be it a global or a creature or an item it requires memory. The more objects being created dynamically the more of the memory impact your game begins to process. This is why you will notice a certain amount of lag within the game when certain scripts fire. While a global is rather low impact it's better to package away as many elements like globals in a static location.
 SithRevan
08-07-2006, 2:46 AM
#8
Well my question is what exactly is a global in the first place, and how do they work?
 Pavlos
08-07-2006, 5:45 AM
#9
A global can be a numeric, or a boolean value. Booleans can be used to check if I have done something before. Such as: Have I been in this area before? Numerics are more handy when dealing with plots. Rather than being off and on, like a boolean, they can be any integer value between -128 and 127. An example of their use would be to check whether or not I've picked up an item, or sold the item to the merchant, both can use different values and you can check against them to tell the game where your plot is up to.

I'm not sure what Darkkender means by a "Master Global" but for keeping track of the main plot on M4-78 I've taken to using "K_DRO_MASTER" with a list of what all the values mean so other people on the team can use it easily.
 stoffe
08-07-2006, 6:08 AM
#10
What I was refering to from Princess Artemis's post was her discusing creating and managing the Global 100% from scripts. This works when you have only a couple of globals to work with in a mod.


Uh, you can't create global variables from scripts at all in KotOR (unlike NWN). They must be added to globalcat.2da in order to be created, or the game won't keep track of them. If you use SetGlobalNumber() (for example) with a variable name not present in globalcat.2da nothing will be stored.


Everytime you ask a script to create some sort of object be it a global or a creature or an item it requires memory. The more objects being created dynamically the more of the memory impact your game begins to process.

Well, in the case of the NWN and KotOR games the delays when spawning new creatures mostly seems to come from disk access rather than RAM access, since the game does not seem to cache UT* templates in memory and thus has to read it from the harddrive every time an object needs to be created in the game world from such a template. The first time you load a particular type of creature takes longer since the game will have to load the model, animations and textures from the harddrive as well (which then appear to be cached in memory for subsequent uses).

In my experience running scripts themselves won't visibly impact the performance of the game unless you either run large amounts of them and have some insanely complex script (or a flawed script with an infinite loop, before the game times out and aborts execution), or, as you said, make the scripts do things that requires harddrive access which might slow things down briefly as the game loads those resources. But it's not the scripts themselves that cause the delays. :)

Well my question is what exactly is a global in the first place, and how do they work?

There are basically three types of variables in the KotOR games:
Script variables: These are declared in scripts and exists when a script is running, and ceases to exist after the script has finished executing. Since most script executions only take a few milliseconds they are fairly shortlived. :)


Local variables (Number, Boolean) are set on objects, and will keep their value as long as that object exists in the active game world. An object can be anything from the whole area itself to a creature or a waypoint. Since objects exist on a per-module basis this means Locals usually only are accessible inside the module they were set in since you are only able to access its host object there (with the exception of local variables set on the main character, party members and puppets, since those objects exist game-wide.) Local numbers are stored in a pre-defined number of numerical slots for each object, which range from 0-31 for Numbers and 0-159 for Booleans.


Global variables (Number, Boolean, String, Location) are variables that exist game-wide and not associated with a particular object. As such a global set in one area can be read from any other area with its value intact. The global variables the game recognizes are defined in the globalcat.2da file, which determines the data type and name of the variable. Globals are referred to by a text string name (unlike the Locals who are referred to by index number with a fixed hardcoded number of them available).
 SithRevan
08-07-2006, 1:37 PM
#11
Local variables (Number, Boolean) are set on objects, and will keep their value as long as that object exists in the active game world. An object can be anything from the whole area itself to a creature or a waypoint. Since objects exist on a per-module basis this means Locals usually only are accessible inside the module they were set in since you are only able to access its host object there (with the exception of local variables set on the main character, party members and puppets, since those objects exist game-wide.) Local numbers are stored in a pre-defined number of numerical slots for each object, which range from 0-31 for Numbers and 0-159 for Booleans.

Thanks for the info, and also my next question is how would you set a global boolean or whatever on an object like a placeable?
 Darth333
08-07-2006, 2:27 PM
#12
While locals are stored on objects, global variables are stored within the game itself as explained by stoffe here:

Global variables (Number, Boolean, String, Location) are variables not associated with a particular object that exist game-wide. As such a global set in one area can be read from any other area with its value intact. The global variables the game recognizes are defined in the globalcat.2da file, which determines the data type and name of the variable. Globals are referred to by a text string name (unlike the Locals who are referred to by index number with a fixed hardcoded number of them available).


If it's the syntax you are looking for, it has already been given by Princess Artemis (post #4), above:

SetGlobalBoolean("mynewglobal", TRUE); //or FALSE


as indicated by stoffe at post #10, you also have to edit globacat.2da to be able to keep track of them (unless you re-use some existing global but if you do so, make sure that it is no longer used by the game or it could screw up your game as globals are used to keep track of plot advancement, amongst others).

Btw, what are you trying to do with this?


and a bit off-topic:

In my experience running scripts themselves won't visibly impact the performance of the game unless you either run [...](or a flawed script with an infinite loop, before the game times out and aborts execution)
This shouldn't really impact performance in a very noticeable way in KotOR/TSL either. The script seems to loop a few times and then it breaks automatically.
 stoffe
08-07-2006, 2:36 PM
#13
Thanks for the info, and also my next question is how would you set a global boolean or whatever on an object like a placeable?

You don't. As said above, global variables are not set on objects, they exist on their own game-wide. There is only one global with each name in the whole game. It's local variables that can be set independently on objects.

To set a local, pick an index that's not already occupied. If you have no standard scripts running on the placeable (such as Random Loot generation for container placeables) you could pick any index between 0 and 159. Then you use a script to set it.

If it's in a dialog you can use the script a_local_set that comes with the game for this. Assign it to a Script slot of a dialog node and set the first parameter (P1) to the index of the LocalBoolean slot you wish to set.

If you want to set it in one of your own scripts you use the SetLocalBoolean() function, where you set the first parameter to the object you wish to set it on, the second to the index you wish to set, and the third to the TRUE/FALSE value you want to set.

To check if it's set in your scripts you similarily use the GetLocalBoolean() function where the first parameter is the object to check the local value on and the second is the LocalBoolean index to check.

One important thing to keep in mind with GetLocalBoolean() is that it does not return TRUE or FALSE for some reason. It returns either FALSE or not FALSE. Thus, never do this when using that function since you'll get unpredictable results:

if (GetLocalBoolean(OBJECT_SELF, 12) == TRUE) {
...
}

Instead, check like this for the desired result:

if (GetLocalBoolean(OBJECT_SELF, 12)) {
...
}


Here is an incomplete list of which LocalBooleans and LocalNumbers are generally already in use on different types of objects. This is in general, for specific standard game objects others may be set as well during quests and conversations.

Creatures:
Boolean 1-3 - Creature AI, ambient NPC settings.
Boolean 10 - Dialogs, used to set that the creature has been talked to once.
Boolean 20-28 - Creature AI, used to activate userdefined script events.
Boolean 29 - Creature AI, used to set if NPC should play ambient animations when idle.
Boolean 30-64 - Reserved range for scenario/plot specific uses, some NPCs use them, most do not.
Boolean 65 - Creature AI, used to set of the NPC should walk around randomly when idle.
Boolean 66-86 - Creature AI, various settings set in the NPC spawn scripts.
Boolean 87 - Creature AI, blocks out the AI for this creature if set.
Boolean 88 - Creature AI defined, but likely unused in K2:TSL.
Boolean 89 - Creature AI, sets the creature to use the Boss Combat AI, overriding their normal AI style.
Boolean 90 - Creature AI, sets that the creature has activated their energy shield in combat.
Boolean 91 - Creature AI, toggles userdefined event for the OnDialogEnd event handler.
Boolean 92 - Creature AI, sets that a force using NPC has buffed with Resist/Immunity at start of battle.
Boolean 94 - Creature AI, sets that the creature has entered combat.
Boolean 95 - Creature AI, sets the creature to use the KotOR1 Malak end fight AI.
Boolean 96 - Creature AI, sets creature to use a zone controller to determine their combat zone.
Boolean 97 - Creature AI, setting for the OnDialog event.
Boolean 98-109 - Creature AI, used by the waypoint walking/ambient animation system.
Boolean 110-159 - Generally unused as far as I can tell.

Number 0-2 - Creature AI, used by the waypoint walking/ambient animation system.
Number 3-6 - Creature AI, internal state info for last attack used, chosen attack pattern etc.
Number 7 - Creature AI, Zone Controller this creature is attached to, if any.
Number 8-9 - Creature AI, settings for healer AI, how much to heal and how often.
Number 10-11 - Creature AI, used for turrets to determine rate of fire.
Number 12-31 - Generally unused as far as I can tell.


Placeables:
Boolean 10-19 - Security terminals, used to set the user's choice of action.
Boolean 55 - Containers, container has been bashed and broken item substituted.
Boolean 57 - Containers, used to set that random loot has been spawned.


Triggers:
Boolean 40 - Used to set that the trigger has been tripped for one-shot triggers.
Page: 1 of 1