View Single Post
Old 01-27-2005, 12:08 PM   #2
Well past expiration date
tk102's Avatar
Join Date: Jan 2004
Posts: 5,768
Current Game: FTL
10 year veteran!  Forum Veteran  Helpful!  Notable contributor 
When spawning an object of any kind, it is important to take re-entry into consideration. Most of the time, you only want to spawn something once, so you have to come up with a way of determining ahead of time whether you've already spawned something or not.

1. Check for the presence of the spawned object.

Seems pretty straightforward right? If the object exists, don't spawn another one. This method works well for placeables and spawned items that go into a spawned placeable.

An example of the code would look like this:
void main() {
  object oContainer=GetObjectByTag("spawned_container_tag");
  if (oContainer==OBJECT_INVALID) {
    vector vContainer=Vector (0.0, 0.0, 0.0);
    location lContainer=Location(vContainer, 0.0);
    oContainer= CreateObject(OBJECT_TYPE_PLACEABLE, "container_template", lContainer);
    CreateItemOnObject("item_template", oContainer); 
2. Set a Local Boolean

This method is good for items that are being spawned into a pre-existing container. The idea is that we're going to put an invisible flag upon the container when we spawn the items for the first time. Then if our script fires again, it will first check for the existence of that flag and, if present, abort. If you firing your spawn script from a dialog, you could also use the presence of the boolean flag in a Conditional Script to prevent the dialog branch from being available.

Note: In general, it is best to write only to Local variables of objects, not creatures since they use them for walking waypoints and other AI. Unless you've researched the AI scripts and know which ones are safe to use, stick to object or use Global variables.
void main() 
//Conditional Spawn NPC Script By  TK102, Darth333, and RedHawke
  int nBoolSlot=6; //any number 0-7 can be used here

  object oLocker=GetObjectByTag("footlker099"); // get a reference to the pre-existing container

  int nCheck=GetLocalBoolean(oLocker ,nBoolSlot);

  if (nCheck) { return; } //exit if TRUE
  CreateItemOnObject("g_w_lghtsbr05", oLocker);
  CreateItemOnObject("g_a_mstrrobe01", oLocker);
  CreateItemOnObject("g_w_sbrcrstl04", oLocker, 2);
// now close the door so we can't re-enter
3. Set a Global Boolean

This method is suitable for creatures. Instead of writing a value locally to an object, we will write it to the memory of the game itself. To do this we will need to use a boolean variable that is defined in globalcat.2da. If you think you can find an obsolete Global Boolean that is already defined (like one from the Endar Spire) you can use that. Otherwise you'll have to edit globalcat.2da, add your new boolean, and put it in your override (and distribute it with your mod).

void main() 
  int nCreatureSpawned = GetGlobalBoolean("my_global_bool");
  if (nCreatureSpawned) { return; } //exit if TRUE
  CreateObject( OBJECT_TYPE_CREATURE, "creature_template", GetLocation(GetFirstPC()) );
// now close the door so we can't re-enter

Last edited by tk102; 04-16-2005 at 11:36 PM.
tk102 is offline   you may: quote & reply,