Script function question - GetStringLeft

I have a script where I want to check for an NPC that has either the tag “c_h_jousting_1”, “c_h_jousting_2” or “c_h_jousting_3”. Only one of these will be in the area at one time. Can you write it like this?

object oNPC = GetObjectByTag(GetStringLeft("c_h_jousting_",14));

I hope this can work, but have never tried it. At least it compiled in the toolset…

Alright, doesn’t seem to work unfortunately ingame.

I’m not an NWN2 guy, so my EE solutions probably wouldn’t work, but if there’s only one of these objects in an area at a time, maybe change their tag on spawn to all have the same tag, then add a variable to the object to differentiate the type. Then you can use GetNearestObjectByTag?

Alternately, you could loop all creature objects in the area and check for that tag prefix?

Here’s the (granted, nwn1) code to support the second option…

string sTarget = "c_h_jousting_";
object oAnchor = GetFirstObjectInArea(GetArea(oPC), ~OBJECT_TYPE_CREATURE);
object oSubject = GetNearestObject(OBJECT_TYPE_CREATURE, oAnchor, ++n);
int n; while (GetIsObjectValid(oSubject))
{
    if (GetStringLeft(GetTag(oSubject), GetStringLength(sTarget)) == sTarget)
    {
        // Do stuff here ...
        break;
    } 

    oSubject = GetNearestObject(OBJECT_TYPE_CREATURE, oAnchor, ++n);
}
1 Like
void main()
{
	object oObj;
	int n;
	
	for (n=1; n<4; n++)
	{
		oObj = GetNearestObjectByTag("c_h_jousting_" + IntToString(n));
		
		if (GetIsObjectValid(oObj))
		{
			//SendMessageToPC(GetFirstPC(FALSE), GetTag(oObj) + " is valid");
			// do stuff
			break;
		}
	}
}

Mmm, not sure travus solution would work for this since I have code that checks the oNPC in different places.

This seems like the best solution for my case. I’ll see if I can create a function where I do that.

If checking multiple areas, change GetNearestObjectByTag to GetObjectByTag.

Thanks for all of you trying to help! However, I realize that I haven’t made my needs in this situation clear at all. I wanted to have oNPC been able to have multiple tags. Three different creatures that can be in the area at any time. Then I call oNPC to do a bunch of stuff in my scripts. I’m not that good at explaining these things it seems. I think I have found a good way for to do it for my situation though. I spawn in the NPC, depending on different circumstances they are different creatures, and then I change the tag of the creature (like @tinygiant suggested) so I can use the object oNPC = GetObjectByTag("c_human_jousting"); in all the different places where it is already called in the 5 different scripts. Again, a bit complicated to explain. The way I did it now was to create a function looking like this. I think this should work without issues:

void CreateNewOpponent(object oPC)
{

	object oOpponentWP = GetObjectByTag("WP_TARGET_START");
	location lLocation = GetLocation(oOpponentWP);
	
	if(GetLocalInt(oPC,"secondopponent"))
	{	
		CreateObject(OBJECT_TYPE_CREATURE,"c_human_jousting2",lLocation,FALSE,"c_human_jousting");	
	}
	
	
	else if(GetLocalInt(oPC,"thirdopponent"))
	{	
		CreateObject(OBJECT_TYPE_CREATURE,"c_human_jousting3",lLocation,FALSE,"c_human_jousting");	
	}
	
	else
	CreateObject(OBJECT_TYPE_CREATURE,"c_human_jousting",lLocation);
	

}