Aller au contenu

Photo

Count objects with same tag in an area


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

#1
kamal_

kamal_
  • Members
  • 5 260 messages
In this case the objects are waypoints. I do not want to give them some kind of local variable if possible

    if (GetIsObjectValid(oWP))
        {
            do
            {
            iNumWaypointInArea++;
            //next line is not correct
            object oWP= GetNextObjectInArea(GetArea(OBJECT_SELF));
            }
            while //unsure what to put in the while
            (GetObjectByTag(sWpType, nNth));

        }

?

#2
Lugaid of the Red Stripes

Lugaid of the Red Stripes
  • Members
  • 955 messages
Use GetNearestObjectByTag, in a loop, and just keep incrementing the iNth until you run out of objects.

int iNth = 2;
string sTag = "WP_Tag";
object oWP = GetNearestObjectByTag(sTag, 1);
while (GetIsObjectValid(oWP)) {
oWP = GetNearestObjectByTag(sTag, iNth);
iNth++;
}

return (iNth - 2);

#3
Lugaid of the Red Stripes

Lugaid of the Red Stripes
  • Members
  • 955 messages
Oh, and remember that you can do some nifty operations on strings, like only getting the first N characters, or using IntToString to stick a number on the end of a string. So it's easy to look for WP_Tag_01 to WP_Tag_NN. You could also create a more complicated naming scheme for your (I assume) ambient waypoints. For example, you could have the prefix "awp_WALK" for points NPCs are supposed to walk to and through, and "awp_POST" for points they're supposed to merely hang out at (like the difference between walking through a market and pausing to check something out). Then you could have a set of suffixes that are more specific, like "_shop", "_loiter", "_guard", "_work", "_talk", etc.
So your builder could throw down a bunch of waypoints that say "awp_SEAT", and the NPCs will use them indecriminately, or he could go back and make some "awp_SEAT_talk" or "awp_SEAT_drink" to more finely customize what happens at any given waypoint.

#4
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
Personally I do this a bit different, here's a snip of code I have for counting waypoints in an area:

// If we have not yet counted the waypoints, do so now and save as a local Int on area so we don't have to count again later
if (nSpawnPoints == 0)
 {
 nSpawnNum = 0;
 oSpawnPoint = GetFirstObjectInArea(oArea);
 while(GetIsObjectValid(oSpawnPoint))
  {
  if ((GetObjectType(oSpawnPoint) == OBJECT_TYPE_WAYPOINT)
   && (GetTag(oSpawnPoint) == "waypoint_tag"))
   {
   nSpawnNum ++;
   oSpawnPoint = GetNextObjectInArea(oArea);
   }
  else
   {
   oSpawnPoint = GetNextObjectInArea(oArea);
   }
  }
 SetLocalInt(oArea, "KM_Spawn_Points", nSpawnNum);
 }

#5
kamal_

kamal_
  • Members
  • 5 260 messages

_Knightmare_ wrote...
...

Not repeating the calculation is good and will get added in. Easing the AI load is always good. Both methods look like they work btw, the animations I'm forcing for testing now play. Now to track other bugs in the code (mumbles about what could be making them hostile).

We're getting there.

#6
Kaldor Silverwand

Kaldor Silverwand
  • Members
  • 1 598 messages
A generic (untested) function to return the number of objects of a certain type in an area would be:

int countObjectsinArea(object oArea, int nObjectType)
{
int nObjectCount = 0;
object oObject = GetFirstObjectInArea(oArea);
while(GetIsObjectValid(oObject))
{
nObjectCount += (GetObjectType(oObject) == nObjectType);
oObject = GetNextObjectInArea(oArea);
}
return nObjectCount;
}

Regards

#7
Shallina

Shallina
  • Members
  • 1 012 messages
The best way to do it if it works is the the way of Lugaid.

Personnaly I nevers got those GetObjectByTag with number working as I want. So I am using something similar to Knightmare.

Or I include the number in the tag. and increase the tag.

tag = tag + IntToString(n);

Modifié par Shallina, 10 juillet 2011 - 12:27 .


#8
kamal_

kamal_
  • Members
  • 5 260 messages
The code from _Knightmare_ is working. It is what I'll be using. Also hopefully eliminated the strange hostile commoner bug.

#9
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
Glad to hear it worked for you. Did if fix your hostile commoners bug? What was happening in the first place (beyond the obvious that you had hostile commoners lol)?

That code snip is from a creature spawning script I did. It selects waypoints at random and spawns things in there. Seems you are doing something similar?

Modifié par _Knightmare_, 10 juillet 2011 - 06:47 .


#10
kamal_

kamal_
  • Members
  • 5 260 messages

_Knightmare_ wrote...

Glad to hear it worked for you. Did if fix your hostile commoners bug? What was happening in the first place (beyond the obvious that you had hostile commoners lol)?

That code snip is from a creature spawning script I did. It selects waypoints at random and spawns things in there. Seems you are doing something similar?

Yes, fixed that. I posted the cause and fix in the Commoner AI thread. There is now test code and a readme you can download.