Author Topic: Onenter random spawn help  (Read 475 times)

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Onenter random spawn help
« on: November 15, 2011, 05:09:39 pm »


               I'm trying to use the script generator to place one of two chests at a waypoint in an area. Im trying to make it random. I've tested the script below but no chest spawns when i enter the area. Can someone please take a look at it and tell me my error?

Also, how can i unspawn these after a certain time has passed on the onexit?



/*
 *  Script generated by LS Script Generator, v.TK.0
 *
 *  For download info, please visit:
 *  http://nwvault.ign.c....Detail&id=1502
 */
// Put this script OnEnter.

void main()
{
    object oTarget;
    object oSpawn;
    // Get the creature who triggered this event.
    object oPC = GetEnteringObject();
    // Only fire for (real) PCs.
    if ( !GetIsPC(oPC)  ||  GetIsDMPossessed(oPC) )
        return;
    // Only fire once per PC.
    if ( GetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF)) )
        return;
    SetLocalInt(oPC, "DO_ONCE__" + GetTag(OBJECT_SELF), TRUE);
    // Decide what to do based on a die roll.
    switch ( d2() )
    {
        case 1:
        {
            // Spawn "chest1".
            oTarget = GetWaypointByTag("WP_CHEST1");
            oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "chest1", GetLocation(oTarget));
         break;
         case 2:
            // Spawn "chest2".
             oTarget = GetWaypointByTag("WP_CHEST2");
             oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "chest2", GetLocation(oTarget));
             break;
                }
            }
        }
               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Onenter random spawn help
« Reply #1 on: November 15, 2011, 05:47:55 pm »


               Your code is working in my testing mod, so you should probably make sure that your chests have the correct ref(i.e chest1 and not chest001 for instance). Also, GetWaypointByTag() returns a waypoint with the specified tag, but from any area, so if you have some other waypoints with same tag, in another area, the chests might as well be created there. If this is the case, you should consider using GetNearestObjectByTag() instead.

Kato
               
               

               


                     Modifié par Kato_Yang, 15 novembre 2011 - 05:53 .
                     
                  


            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Onenter random spawn help
« Reply #2 on: November 15, 2011, 06:19:21 pm »


               Thanks Kato that was it! I had changed the tag but not the res ref. '<img'> The script generator is a blessing for those like me learning to script.

What about my second question? Getting them to despawn after a certain time has passed on exit?
               
               

               
            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Onenter random spawn help
« Reply #3 on: November 15, 2011, 06:25:43 pm »


               actually this may just work.

/*
*  Script generated by LS Script Generator, v.TK.0
*
*  For download info, please visit:
*  http://nwvault.ign.c....Detail&id=1502
*/
// Put this script OnExit.


void main()
{
   // Get the creature who triggered this event.
   object oPC = GetExitingObject();

   // Only fire for (real) PCs.
   if ( !GetIsPC(oPC)  ||  GetIsDMPossessed(oPC) )
       return;

   // Destroy objects (not fully effective until this script ends).
   DelayCommand(300.0, DestroyObject(GetObjectByTag("chest1")));
   DelayCommand(300.0, DestroyObject(GetObjectByTag("chest2")));
}
               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Onenter random spawn help
« Reply #4 on: November 15, 2011, 06:36:44 pm »


               Well I'd rather put the delay in the DestoyObject() function itself, but if it works for you, all the better. Also, depending on your needs, you might have to verify if any other PC remains in the area before destroying the chests.

Kato
               
               

               
            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Onenter random spawn help
« Reply #5 on: November 15, 2011, 07:17:58 pm »


               

Kato_Yang wrote...

Well I'd rather put the delay in the DestoyObject() function itself, but if it works for you, all the better. Also, depending on your needs, you might have to verify if any other PC remains in the area before destroying the chests.

Kato



How do you put it in the function? And yes verifying if any other PC's remain is a must.
               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Onenter random spawn help
« Reply #6 on: November 15, 2011, 08:53:59 pm »


                Okay, so verifying the presence of other PCs in the area. Depending on your setup and/or preferences, I would see two simple possibilities, yet I'm sure there are more.

1- You can replace your script's code with:

void DelChests()
{
   object oPC = GetFirstPC();
   while(GetIsObjectValid(oPC))
   { 
       if(GetArea(oPC) == OBJECT_SELF) return;
      oPC = GetNextPC();
   }
   DestroyObject(GetObjectByTag("chest1"));
   DestroyObject(GetObjectByTag("chest2"));
}

void main()
{
   object oPC = GetExitingObject();
   if(!GetIsPC(oPC) || GetIsDMPossessed(oPC)) return;
   oPC = GetFirstPC();
   while(GetIsObjectValid(oPC))
   {
      if(GetArea(oPC) == OBJECT_SELF) return;
      oPC = GetNextPC();
   }
   DelayCommand(300.0, DelChests());
}

This is tested and works well but it's not very elegant and not the fastest, so:

2- You could put this in the script handling the OnEnter event of your area(s):

void main()
{
   object oPC = GetEnteringObject();
   if(GetIsPC(oPC) && !GetIsDM(oPC))
   SetLocalInt(OBJECT_SELF, "NumPCs", GetLocalInt(OBJECT_SELF, "NumPCs")+1);
}

And then replace your initial script's code with:

void DelChests()
{
   if(GetLocalInt(OBJECT_SELF, "NumPCs") > 0) return;
   DestroyObject(GetObjectByTag("chest1"));
   DestroyObject(GetObjectByTag("chest2"));
}

void main()
{
   object oPC = GetExitingObject();
   if(!GetIsPC(oPC) || GetIsDMPossessed(oPC)) return;
   int nNumPCs = GetLocalInt(OBJECT_SELF, "NumPCs")-1;
   if(nNumPCs < 1)
   {
       DeleteLocalInt(OBJECT_SELF, "NumPCs");
       DelayCommand(300.0, DelChests());
   }
   else SetLocalInt(OBJECT_SELF, "NumPCs", nNumPCs);
}

The difference between both options is that the second one only increments/decrements and reads a variable holding the number of PCs in the area(the variable is deleted once the area is empty of PCs), instead of looping over the PCs. Furthermore, the variable holding the PC number can be useful for other systems such as area spawners and cleaners.



Kato
               
               

               


                     Modifié par Kato_Yang, 15 novembre 2011 - 09:37 .
                     
                  


            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Onenter random spawn help
« Reply #7 on: November 16, 2011, 04:16:39 pm »


               Great stuff! Thank you again for your help Kato.