get units in area?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
Whicker
Posts: 665
Joined: Tue Jun 19, 2018 9:54 pm

get units in area?

Post by Whicker »

Is there an easy way to get what is inside of an area defined by some ref points?

I see that I can check if a unit is in an area, but I want to check the area and see what units are in it. Only thing I can think of is lopping thru all the units on a side and checking each one to see if it is in an area.

I'd like to see if there are more than x mobile facilities, or more than x tanks really.

this works per unit:
local u= SE_GetUnit({name='tank', guid='b87e998b-cca8-4e6d-beb0-8333cc3aeae6'})
print(u:inArea{"1","2","3"})--prints yes or no

but I want to do something more like:
local p = ScenEdit_GetReferencePoints({side="Crimson Commonwealth", area={"1","2","3"}})
print(p.unitsInArea)--no such thing
Whicker
Posts: 665
Joined: Tue Jun 19, 2018 9:54 pm

RE: get units in area?

Post by Whicker »

I guess I can see how this isn't possible - the game would have to do the same thing - loop thru all units to see what is in any given area of rps.

The units that I wanted to check were all in groups, so rather than loop thru all the units on the side I made a table of group names and then loop thru those to see if they are in the area. The goal is to pop up a message if none of the units are in the area so this is what I think works:
local t={'Group 3349','Group 3350','Group 3351','3rd Armored'}
for i = 1,#t do
local u = SE_GetUnit({name=t,side='Crimson Commonwealth'})
if u ~= nil then
unitsToCheck = u.group.unitlist
for i = 1, #unitsToCheck do
local uu = SE_GetUnit({guid=unitsToCheck})
if uu:inArea{"RP-4091","RP-4092","RP-4093","RP-4094"} then
--print(uu.name..' is it in the area? '..tostring(uu:inArea{"RP-4091","RP-4092","RP-4093","RP-4094"}))
return --there is a unit still in the area so exit
end
end
end--loop of unitlist
end--table of groups
ScenEdit_SetEvent('Sangster Recaptured', {IsActive=true})--if we get this far there are no units in the area
ScenEdit_SpecialMessage('playerside','Commander, <br><br>Our forces have cleared the area around Sangster of most major enemy units, we should have control of the Airport in the next hour.<br><br>(you must have ground forces occupying the base for 60 minutes)')
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: get units in area?

Post by KnightHawk75 »

Maybe unrelated I think but if you ever need to a generic isInArea sort of check without having to have RP's or even navzones or wanting to create a unit etc.. I had the following that your post reminded me I meant to share a while back in case someone else ever needed it. It was part of some ridiculous enhancements I did with my own unit spawner (also never shared).


The use case (among others) was where I needed it\preferred it over creating an unit just to call :inArea. I found better performance and less leakage as I recall. Particularly in a dynamic waypoint generator to target, among other things during pathfinding check it needed to check was if a possible next location was inside a no-nav zone that applied to a particular\potentially newly generated units. Another use was simply allowing me to do an 'inarea()' type call on any point within a given polygon I wanted without having to actually have navzones\rp's defined in the editor, sort of private easier to manage zones of my own if you will for a specific case or two.

text file attached to avoid forum garbling it.
Attachments
InPolygonS..atedTest.txt
(3.68 KiB) Downloaded 29 times
User avatar
michaelm75au
Posts: 12464
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: get units in area?

Post by michaelm75au »

I haven't tried it, but couldn't a NOT UnitsInArea trigger do it for you, where the triggering type is any enemy unit???
Michael
Post Reply

Return to “Lua Legion”