Author Topic: Triggering a script on the death of a group  (Read 360 times)

Legacy_uberdowzen

  • Newbie
  • *
  • Posts: 41
  • Karma: +0/-0
Triggering a script on the death of a group
« on: November 16, 2010, 06:11:27 am »


               I'm trying to write a script that will trigger my module to end when a group of creatures (quest giver NPCs) are dead. This is what I've got so far:

SetLocalInt(GetPCSpeaker(), "nStaffKilled", 0);

This line is part of a script which starts the quest. I added it so that the variable nStaffKilled was defined for later on.

Then I've added to the onDeath script of the NPCs:


   if (GetLocalInt(oKiller, "nStaffKilled") == 1)

   {

       object oPC = GetPCSpeaker();

       AssignCommand(oPC, ClearAllActions());

       SetCutsceneMode(oPC, TRUE);

       ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectCutsceneDominated(), oPC, 100.0f);

       SetCameraMode(oPC, CAMERA_MODE_TOP_DOWN);

       DelayCommand (2.0f, AssignCommand(oPC, FadeToBlack(oPC, FADE_SPEED_SLOWEST)));

       DelayCommand (3.0f, AssignCommand(oPC,   EndGame("")));

   }



   SetLocalInt(oKiller, "nStaffKilled", 1);

Can anyone see what's wrong? This is driving me nuts.
               
               

               
            

Legacy_Mudeye

  • Full Member
  • ***
  • Posts: 238
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #1 on: November 16, 2010, 03:49:39 pm »


               A couple of things look wrong.
1) There is no speaker when you are in an OnDeath script.
2) You don't show were the oKiller comes from.  Is it supposed to be the PC? 
3) You created the variable nStaffKilled on the PC but you try to read it from the oKiller object.  That will only work if the oKiller is the PC.  But right after that you try to get the PC using GetPCSpeaker().
               
               

               
            

Legacy_Mudeye

  • Full Member
  • ***
  • Posts: 238
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #2 on: November 16, 2010, 04:05:05 pm »


               I'll assume the problem is that you aren't getting your script to fire and that the block of cutscene code is correct.

This can all be done in the OnDeath script for the NPC quest givers. 

Get the name of the OnDeath script for the NPC quest givers. I'll assume the name is "npc_death".  Also I'll assume that you have 5 NPCs you are watching to die.

Make a new OnDeath script with a different name like "npc_death2".

void Main()
{
   ExecuteScript( "npc_death"); 

   object mod = GetModule();
   int deadCnt = GetLocalInt(mod,"deadCnt");
   SetLocalInt(mod,"deadCnt",deadCnt+1);
   if( deadCnt == 4 )
   {
        //do the end stuff here
        object pc = GetFirstPC();
        AssignCommand( pc, ClearAllActions());

        SetCutsceneMode(pc, TRUE);

        ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectCutsceneDominated(), pc, 100.0f);

        SetCameraMode(pc, CAMERA_MODE_TOP_DOWN);

        DelayCommand (2.0f, AssignCommand(pc, FadeToBlack(pc, FADE_SPEED_SLOWEST)));

        DelayCommand (3.0f, AssignCommand(pc, EndGame("")));
   }
}

I haven't run this because I'm not at home. But the idea is sound and the code is close to what you need.
The variable used is called "deadCnt" and is on the module object.
It defaults to 0 so no need to set it at module startup.
Each time one of the selected NPCs dies they will increment the deadCnt.
When the 5th one dies the deadCnt will already be 4 so that will cause the block of ending code to run.
The name "npc_death" is not going to be the real name so you need to replace that with the real name of the OnDeath script that your NPCs are using.
If the number of NPCs is different than 5 then replace 4 with whatever it is minus 1.  So if there were 3 NPCs you would  put 2 in place of 4.  Because 2 is 3 - 1.  
               
               

               


                     Modifié par Mudeye, 16 novembre 2010 - 04:11 .
                     
                  


            

Legacy_uberdowzen

  • Newbie
  • *
  • Posts: 41
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #3 on: November 16, 2010, 07:12:21 pm »


               

Mudeye wrote...

A couple of things look wrong.
1) There is no speaker when you are in an OnDeath script.


WAIT!!! I didn't see that! I think that might be the problem.

2) You don't show were the oKiller comes from.  Is it supposed to be the PC?


I just didn't copy that part. oKiller is defined earlier as whoever killed the creature in question.

3) You created the variable nStaffKilled on the PC but you try to read it from the oKiller object.  That will only work if the oKiller is the PC.  But right after that you try to get the PC using GetPCSpeaker().


Yeah, I'm not very happy with having to do that but I couldn't think of any other way of defining nStaffKilled with a starting value of 0.

I think #1 might be the problem. I'll changed it then get back to you.
               
               

               
            

Legacy_Mudeye

  • Full Member
  • ***
  • Posts: 238
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #4 on: November 16, 2010, 07:25:45 pm »


               I think GetFirstPC() will work fine if it is a single player module.

You don't actually have to define local  int variables before you read them in NWNScript. If they haven't been defined, you can still read them.  The value will be 0.

You may also have the case where a henchman or summon kills the creature.  In those cases you would need to get the pc from the creature with something like GetMaster().
               
               

               


                     Modifié par Mudeye, 16 novembre 2010 - 07:29 .
                     
                  


            

Legacy_uberdowzen

  • Newbie
  • *
  • Posts: 41
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #5 on: November 16, 2010, 09:27:48 pm »


               Oh right. I thought is was like C++ where you had to define variables before you could use them. My mistake. Thanks for your help!
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #6 on: November 16, 2010, 09:47:59 pm »


               

uberdowzen wrote...

Oh right. I thought is was like C++ where you had to define variables before you could use them. My mistake. Thanks for your help!


You are half right.  If you read the comments that proceed the GetLocal functions it clears it up a bit.

// Get oObject's local integer variable sVarName
// * Return value on error: 0
int GetLocalInt(object oObject, string sVarName)


So yes if the VarName is not defined on the object you get an Error that gives you a return value of 0.
               
               

               
            

Legacy_Mudeye

  • Full Member
  • ***
  • Posts: 238
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #7 on: November 17, 2010, 04:00:09 pm »


               Calling it an ERROR might be a little misleading too.  The NWNScript engine doesn't balk on these ERRORs as C++ would.   The engine is intended to handle cases where objects and variables are missing.  In an environment where the exact state of the system is unpredictable because of the limitless variations cause by user choices the decision to allow ERRORs to pass unnoticed was a good one.   If developers had to always account for every possible condition with an ERROR free implementation I dare say there would be no working modules.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #8 on: November 17, 2010, 06:15:16 pm »


               Yes I agree that Error is not the best word to use here.  I only used the word Error because it is the one Bioware chose to use in thier function header,



The whole point, of the statment above, Is that if you read the Header's for the functions you plan on useing a some of the questions you have will be answered.  



               
               

               
            

Legacy_Mudeye

  • Full Member
  • ***
  • Posts: 238
  • Karma: +0/-0
Triggering a script on the death of a group
« Reply #9 on: November 17, 2010, 06:56:31 pm »


               Agreed.