Why won't this script work?

I’m at a total loss here. This script does things but ingame nothing is happening. Why? Can anyone help? When the PC is entering this area, a lot of enemies that are at first set as “Commoner” are turned into hostiles. I’ve done a few “SendMessageToPC” to see if things run, and I get a lot of messages, so it seems that the script runs, but none of the NPCs turn into enemies. Not a single one. Why? Please help. I can see nothing wrong, but apparently it’s not working at all.

#include "nw_i0_generic"
#include "ginc_object"

void Death(string sTagString, int iInstance = 0)
{
	PrettyDebug("Applying Death Effect To: " + sTagString + " Instance = " + IntToString(iInstance));		
    
        object oObject = GetObjectByTag(sTagString, iInstance);

        AssignCommand(oObject, SetIsDestroyable( FALSE,FALSE,TRUE ));
	    SetImmortal( oObject, FALSE );
	    SetPlotFlag( oObject, FALSE );
        effect eFX = EffectDeath();
		PrettyDebug("Name of oObject = " + GetName(oObject));		
        ApplyEffectToObject( DURATION_TYPE_INSTANT,eFX,oObject );
    
}

void PlayCustomAnimationVoid(object oObject, string sAnimation, int iLoop = 0, float fSpeed = 1.f)
{
	PlayCustomAnimation(oObject, sAnimation, iLoop, fSpeed);
}


void main()
{
	object oPC = GetEnteringObject();	
	if(!GetIsPC(oPC)) return;
	
	if(GetLocalInt(OBJECT_SELF,"Done")) return;
	
	SetLocalInt(OBJECT_SELF,"Done",1);
	
	object oHeadmage = GetNearestObjectByTag("teacher2");
	
	int nENEMYSkeleton = 1;
	int nENEMYOrc = 1;
	int nENEMYWarrior = 1;
	int nENEMYGoblin = 1;
	
	while (nENEMYGoblin <= 4)
	{
		object oGoblin = GetNearestObjectByTag("l_goblin" + IntToString(nENEMYGoblin));
		
		SendMessageToPC(oPC,"Found a goblin"); 
		ChangeToStandardFaction(oGoblin, STANDARD_FACTION_HOSTILE);
		SetIsTemporaryEnemy(oPC, oGoblin);
		AssignCommand(oGoblin, ActionAttack(oPC));
		AssignCommand(oGoblin, DetermineCombatRound(oPC));
		nENEMYGoblin = nENEMYGoblin + 1;
	}
	
	object oSkeletonBlackguard = GetNearestObjectByTag("c_skeleton7");
	ChangeToStandardFaction(oSkeletonBlackguard, STANDARD_FACTION_HOSTILE);
	SetIsTemporaryEnemy(oPC, oSkeletonBlackguard);
	AssignCommand(oSkeletonBlackguard, ActionAttack(oPC));
	AssignCommand(oSkeletonBlackguard, DetermineCombatRound(oPC));
	
	while (nENEMYWarrior <= 4)
	{
		object oWarrior = GetNearestObjectByTag("villian" + IntToString(nENEMYWarrior));
		
		SendMessageToPC(oPC,"Found a warrior"); 
		ChangeToStandardFaction(oWarrior, STANDARD_FACTION_HOSTILE);
		SetIsTemporaryEnemy(oPC, oWarrior);
		AssignCommand(oWarrior, ActionAttack(oPC));
		AssignCommand(oWarrior, DetermineCombatRound(oPC));
		nENEMYWarrior = nENEMYWarrior + 1;
	}
	
	while (nENEMYOrc <= 8)
	{
		object oOrc = GetNearestObjectByTag("l_orc" + IntToString(nENEMYOrc));
		
		SendMessageToPC(oPC,"Found an orc"); 
		ChangeToStandardFaction(oOrc, STANDARD_FACTION_HOSTILE);
		SetIsTemporaryEnemy(oPC, oOrc);
		AssignCommand(oOrc, ActionAttack(oPC));
		AssignCommand(oOrc, DetermineCombatRound(oPC));
		nENEMYOrc = nENEMYOrc + 1;
	}
	
	while (nENEMYSkeleton <= 4)
	{
		object oSkeleton = GetNearestObjectByTag("l_skeletonwarrior" + IntToString(nENEMYSkeleton));
		
		SendMessageToPC(oPC,"Found a skeleton"); 
		ChangeToStandardFaction(oSkeleton, STANDARD_FACTION_HOSTILE);
		SendMessageToPC(oPC,"Skeleton now hostile");
		SetIsTemporaryEnemy(oPC, oSkeleton);
		AssignCommand(oSkeleton, ActionAttack(oPC));
		AssignCommand(oSkeleton, DetermineCombatRound(oPC));
		nENEMYSkeleton = nENEMYSkeleton + 1;
	}
	
	Death("agatha2");
	Death("befden2");
	
	PlayCustomAnimationVoid(oHeadmage,"*proneB",1);
}
1 Like

Just a quick glance, but …

You need to get the next goblin, etc in the group.

1 Like

Eh, ok…but why do I get the message “Found a goblin” four times then. It seems to be working (this is a script example made by Clangeddin that I use here). And why does the message “Skeleton now hostile” show up, but none of the skeletons turn hostile?

Edit: Ok, maybe I’ll redo the whole script then, and try a variation by travus instead. See if that works better.

1 Like

The function says this:

// Get the nth Object nearest to oTarget that has sTag as its tag.
// * Return value on error: OBJECT_INVALID
object GetNearestObjectByTag(string sTag, object oTarget=OBJECT_SELF, int nNth=1);

So, we need to iterate through int iNth until it is no longer a valid object.

Therefore, you need …

while (nENEMYGoblin <= 4)
	{
		object oGoblin = GetNearestObjectByTag("l_goblin", OBJECT_SELF, nENEMYGoblin);
		
		SendMessageToPC(oPC,"Found a goblin"); 
		ChangeToStandardFaction(oGoblin, STANDARD_FACTION_HOSTILE);
		SetIsTemporaryEnemy(oPC, oGoblin);
		AssignCommand(oGoblin, ActionAttack(oPC));
		AssignCommand(oGoblin, DetermineCombatRound(oPC));
		nENEMYGoblin = nENEMYGoblin + 1;

        // MISSING LINES ON ALL SECTIONS        
        oGoblin = GetNearestObjectByTag("l_goblin", OBJECT_SELF, nENEMYGoblin);
	}
1 Like

Thanks. I found another way of solving the whole thing though. I just changed the GetNearestObjectByTag to GetObjectByTag then everything worked as it should.

1 Like

I know you already have it worked out, but the following should work for you too:

#include "ginc_actions"

void Death(string sTagString)
{
        object oObject = GetObjectByTag(sTagString);

        AssignCommand(oObject, SetIsDestroyable(FALSE,FALSE,TRUE));
	    SetImmortal(oObject, FALSE);
	    SetPlotFlag(oObject, FALSE);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDeath(), oObject);
}

void main()
{
	object oPC = GetEnteringObject();
	
	if (!GetIsPC(oPC) || GetLocalInt(OBJECT_SELF, "Done")) return;
	
	int n = 1;	
	string s;
	
	SetLocalInt(OBJECT_SELF, "Done", TRUE);
	PlayCustomAnimation(GetNearestObjectByTag("teacher2", oPC), "proneB", 1);
	Death("agatha2");
	Death("befden2");
	StandardAttack(GetNearestObjectByTag("c_skeleton7", oPC), oPC);
			
	while (n <= 4)
	{
		s = IntToString(n);
		StandardAttack(GetNearestObjectByTag("l_goblin" + s, oPC), oPC);
		StandardAttack(GetNearestObjectByTag("villian" + s, oPC), oPC);
		StandardAttack(GetNearestObjectByTag("l_skeletonwarrior" + s, oPC), oPC);
		StandardAttack(GetNearestObjectByTag("l_orc" + s, oPC), oPC);	// first four orcs
		
		n += 1;
	}
	
	while (n <= 8)	
	{
		StandardAttack(GetNearestObjectByTag("l_orc" + IntToString(n), oPC), oPC);	// next four orcs
		n += 1;
	}
}
2 Likes

Hehe. You always find a way to make the scripts more compact and effecient. Love it. I’ll copy this one, but I think I’ll keep my version in the game since I did some changes to it and I don’t want to confuse myself. :grinning:

EDIT: Actually, I wonder if this script works better, though with the StandardAttack function. When entering the area the enemies are supposed to go hostile and some of them are supposed to just attack random guards and commoners, and some of them are to attack the PC. This was one of the things I’ve changed in my script. I let some of them just change faction…

1 Like