Author Topic: CreateObject question  (Read 388 times)

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
CreateObject question
« on: February 28, 2015, 07:20:13 pm »


               

I'm spawning creatures in an area at waypoints. MOB_1 - MOB_10. I would like to reuse this script for multiple areas with the same waypoint names in each area. I'm afraid that it may randomly pick an area with the waypoint name however. Is there a way to only use the waypoints in the area the script was triggered? Below is a snippet of my code.


 


object oTarget;
object oSpawn;
location lTarget;
int i = 1;
 

while( i < 11){
            SendMessageToPC(oPC, "Testing the value of i: " + IntToString(i));
            oTarget = GetWaypointByTag("MOB_"+ IntToString(i));
            lTarget = GetLocation(oTarget);
            oSpawn = CreateObject(OBJECT_TYPE_CREATURE, mob, lTarget);
            i++;
        }
    



               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
CreateObject question
« Reply #1 on: February 28, 2015, 08:33:56 pm »


               

Why not just loop through the area (or a radius that is centered on the area and covers the whole zone (and only considers waypoints)) rather than using GetWaypointByTag?



               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
CreateObject question
« Reply #2 on: February 28, 2015, 08:46:24 pm »


               

Do you absolutely need your waypoints to be numbered? I'm also using a custom spawner with waypoints, each waypoint is tagged "WP_" + tag of the area, so no numbers are needed and the waypoints are retrieved with GetObjectByTag(), an efficient function. As long as no two areas have the same tag it works perfectly, no lag, even with many areas spawning at the same time.


 


 


Kato



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
CreateObject question
« Reply #3 on: February 28, 2015, 08:51:40 pm »


               

Aside from this not being about CreateObject '<img'>  why not use GetNearestObjectByTag, assuming the waypoints are the only things in the area with those tags.



               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
CreateObject question
« Reply #4 on: February 28, 2015, 09:30:31 pm »


               

// Replace OBJECT_SELF if not appropriate in the context. Note that


// the area's tag is used, so the waypoints tag would have to look like this, using


// the example of post #1: "MOB_" + tag of area + 1 and upper


 


string sTag = "MOB_" + GetTag(GetArea(OBJECT_SELF)); // retrieve area any which way you want


int nNth = 1;


object oWP = GetObjectByTag(sTag+IntToString(nNth)); 


 


while(oWP != OBJECT_INVALID)


{


    CreateObject(OBJECT_TYPE_CREATURE, mob, GetLocation(oWP)); 


    oWP = GetObjectByTag(sTag+IntToString(++nNth));     


}


 


 


This way you can have your waypoints numbered and quickly retrieve only those within the specified area. The code can be called from anywhere and does not perform distance checks in the loop.


 


EDIT: Fixed an oversight, thanks to Proleric.


 


 


Kato 



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
CreateObject question
« Reply #5 on: February 28, 2015, 10:19:45 pm »


               Unless I missed something, shouldn't that simply be

    oWP = GetObjectByTag(sTag+IntToString(++nNth));  

since that tag is unique?


Good approach, though - GetNearest will only work if the caller has a location, and is not in an area OnEnter event.


I'm interested to know why the OP requires the duplication of waypoint names.


If there's a compelling reason for that, such as the duplication of areas, a Moneo script could be used to reset the tags to unique values including the area name, as suggested above.
               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
CreateObject question
« Reply #6 on: February 28, 2015, 10:31:58 pm »


               

True, Proleric, I was in a hurry, sorry about that. I'll edit...



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
CreateObject question
« Reply #7 on: February 28, 2015, 10:47:31 pm »


               


Good approach, though - GetNearest will only work if the caller has a location, and is not in an area OnEnter event.




 


If this is a trigger its trivial, and on enter it's not hard to find an object in the area to use. Are you saying that getnearest does not work in on area enter events period? I have not encountered that, just that you need to find an actual object in the area...


               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
CreateObject question
« Reply #8 on: February 28, 2015, 10:50:29 pm »


               
@Proleric , No real reason other than laziness when it comes down to it. Quickly being able to copy and pasting my trigger and waypoints into different areas for ease of use.

 

I never knew that Waypoints were considered objects the same way that 'objects' are, that opens even more possibilities. 


 

Thanks for the feedback about using GetObjectByTag(),  I'm glad I can use that instead of GetWaypointByTag().

 

thank you all for the feedback!

 

 


               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
CreateObject question
« Reply #9 on: March 01, 2015, 09:48:43 am »


               


If this is a trigger its trivial, and on enter it's not hard to find an object in the area to use. Are you saying that getnearest does not work in on area enter events period? I have not encountered that, just that you need to find an actual object in the area...




 


I find that a player's location is unreliable when they enter an area. The engine sets it to (0,0,0) for a significant time, during which the OnEnter script usually runs, before the correct location is established. So, GetNearest... and other location-dependent functions are equally unreliable. There is a similar problem OnExit.


 


It's not possible to use the area or module as a reference point, because they don't have a location property.


 


It's possible to work around this, of course, but I find it more robust to use one of the other methods.


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
CreateObject question
« Reply #10 on: March 01, 2015, 05:35:45 pm »


               


I find that a player's location is unreliable when they enter an area.


 


 So, GetNearest... and other location-dependent functions are equally unreliable.




I totally agree with the first part of the above. But the second statement does not follow from the first.  GetNearest and other location dependent functions should be reliable as long as you don't use the PC's location. I was just curious if there was more to what you were saying that I was missing. Thanks.


 


But yes, if you can use unique tags then gobt is the way to go.


               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
CreateObject question
« Reply #11 on: March 01, 2015, 09:50:11 pm »


               Yes, as I said, there are workarounds, but I prefer to work with more robust functions if possible, as it's easy to forget the detailed nuances.