Page 1 of 1

Having issues with CreateObject

Posted: Tue Feb 17, 2004 11:10 pm
by Titanium Dragon
I am using CreateObject, and it is causing me problems. It compiles fine in the toolset, but seems to crash NWN when I use it in code.

NOTE:
bazaarmerchant+doubledigitnumber is the tag of a merchant
q_bazaarmerchant is the resref of the creature that acts as a merchant for the merchant
Merchantof sets which merchant object the creature will be linked to.

CODE (on enter script for an area):

void main()
{
int iNumMerchants=7;
int a;
location lSpawn;
string sSpawn;
a=Random(2);
while(a<iNumMerchants)
{
if(a>9)
{
sSpawn="bazaarmerchant"+IntToString(a);
} else
{
sSpawn="bazaarmerchant0"+IntToString(a);
}
lSpawn=GetLocation(GetObjectByTag(sSpawn));
sSpawn="q_"+sSpawn;
CreateObject(OBJECT_TYPE_CREATURE,"q_bazaarmerchant",lSpawn,FALSE,sSpawn);
SetLocalInt(GetObjectByTag(sSpawn),"Merchantof",a);
a=a+1+Random(2);
}
}

CONDENSED CODE TO CHECK CreateObject (another OnEnter script):

void main()
{
int a=1;
location lSpawn;
string sSpawn;
sSpawn="bazaarmerchant0"+IntToString(a);
lSpawn=GetLocation(GetObjectByTag(sSpawn));
CreateObject(OBJECT_TYPE_CREATURE,"q_bazaarmerchant",lSpawn);
}

Now, am I doing something wrong, or is CreateObject just messed up?

Posted: Tue Feb 17, 2004 11:48 pm
by Jordicus
from Lexicon
Creating a creature during the OnEnter event without first checking to see what's actually entering the area causes NWN to enter an infinite loop of creating creatures (as each creature created causes the OnEnter event to fire its attached script again). This can be avoided by testing if GetIsPC(GetEnteringObject()) is true (if its true, then create the creature(s); if its false then a creature that was created fired the event).
so the CreateObject itself triggers the OnEnter as you have it written... hope that helps..

Posted: Tue Feb 17, 2004 11:51 pm
by Titanium Dragon
Thanks :D That makes perfect sense. Sorry to bother you with something so trivial. :?

Posted: Tue Feb 17, 2004 11:54 pm
by Jordicus
not trivial at all.. just one of those things where 1 + 1 is not adding up for some reason.. :P