Aller au contenu

Photo

CreateObject question


  • Veuillez vous connecter pour répondre
11 réponses à ce sujet

#1
Buddywarrior

Buddywarrior
  • Members
  • 256 messages

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++;
        }
    



#2
MagicalMaster

MagicalMaster
  • Members
  • 2 003 messages

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?



#3
Kato -

Kato -
  • Members
  • 392 messages

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



#4
meaglyn

meaglyn
  • Members
  • 811 messages

Aside from this not being about CreateObject ;)  why not use GetNearestObjectByTag, assuming the waypoints are the only things in the area with those tags.



#5
Kato -

Kato -
  • Members
  • 392 messages

// 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 



#6
Proleric

Proleric
  • Members
  • 2 356 messages
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.

#7
Kato -

Kato -
  • Members
  • 392 messages

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



#8
meaglyn

meaglyn
  • Members
  • 811 messages

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...



#9
Buddywarrior

Buddywarrior
  • Members
  • 256 messages
@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!
 
 


#10
Proleric

Proleric
  • Members
  • 2 356 messages

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.



#11
meaglyn

meaglyn
  • Members
  • 811 messages

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.



#12
Proleric

Proleric
  • Members
  • 2 356 messages
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.