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));
}
?
Count objects with same tag in an area
Débuté par
kamal_
, juil. 08 2011 11:32
#1
Posté 08 juillet 2011 - 11:32
#2
Posté 08 juillet 2011 - 11:52
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);
int iNth = 2;
string sTag = "WP_Tag";
object oWP = GetNearestObjectByTag(sTag, 1);
while (GetIsObjectValid(oWP)) {
oWP = GetNearestObjectByTag(sTag, iNth);
iNth++;
}
return (iNth - 2);
#3
Posté 09 juillet 2011 - 12:07
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.
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
Posté 09 juillet 2011 - 12:48
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);
}
// 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
Posté 09 juillet 2011 - 01:10
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)._Knightmare_ wrote...
...
We're getting there.
#6
Posté 09 juillet 2011 - 11:33
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
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
Posté 10 juillet 2011 - 12:25
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);
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
Posté 10 juillet 2011 - 01:57
The code from _Knightmare_ is working. It is what I'll be using. Also hopefully eliminated the strange hostile commoner bug.
#9
Posté 10 juillet 2011 - 06:45
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?
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
Posté 10 juillet 2011 - 07:44
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._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?





Retour en haut







