Using the Unit.inArea method

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
Primarchx
Posts: 1954
Joined: Sun Jan 20, 2013 9:29 pm

Using the Unit.inArea method

Post by Primarchx »

So I want to determine if a unit is in a given area at the Lua level. For this I thought I would use the Unit.inArea method and pass it a table of RPs defining a given area.

list_of_RPs = ScenEdit_GetReferencePoints({side='Pink', area={'RP1', 'RP2', 'RP3', 'RP4'})
unit_to_check = ScenEdit_GetUnit({side='Blue', name='Maverick'})
checkval = unit_to_check.inArea(list_of_RPs)
if checkval == true then
etc

But all I get is "instance method 'inArea' requires a non null target object".
I've tried passing just a list of RP guids; RP guids, lats & longs, etc. Not sure what this method is wanting as an argument. Any ideas?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Using the Unit.inArea method

Post by KnightHawk75 »

list_of_RPs is returning nil there from GetReferencePoints(), why I don't know but nothing I try when using side='x',area={'nameofrefpoint','nameofrefpoint',etc} returns a value\table as expected, I may need play around more with it.

The follow sample from my console does seem to work though for what you want.

Code: Select all

unit_to_check = ScenEdit_GetUnit({side='OpFor', name='FireFox'}) 
 local checkval = false;
 checkval = unit_to_check:inArea({'RP-5732','RP-5733','RP-5735','RP-5734'});
 if checkval == true then
     print('in area! do something!');
 end
 
Make sure the RP list is in a validated order\shape though.
User avatar
Primarchx
Posts: 1954
Joined: Sun Jan 20, 2013 9:29 pm

RE: Using the Unit.inArea method

Post by Primarchx »

Thanks! May make it a bit more clumsy but I can keep a table of formed areas that need to be checked that will provide the list of RPs. Will probably work.

Just getting back into programming and my background is in c++. It's a weird experience trying to reconcile operations, syntax, structures, etc in another language!
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Using the Unit.inArea method

Post by KnightHawk75 »

never-mind about me always getting nil mentioned above, had a typo in a var name, haha knew I must be doing something wrong. lol

Anyway beyond just the shortcut posted above, using your original desired code you can get it to work like this as well, though I'm not sure why you might need to if you already have the names\guids and can just call :inArea() directly with them like above.
Maybe you don't want to type same names over and over in larger code? but then could just use static table ie myRPlist={'rp1','rp2','rp3','rp4'}; or built it dynamically like below:

Code: Select all

 local list_of_RPs = {};
 list_of_RPs = ScenEdit_GetReferencePoints({side='OpFor', area={'RP-5732','RP-5733','RP-5735','RP-5734'}})
 if list_of_RPs ~= nil then
   local tNames = {};
   for key,value in pairs(list_of_RPs) do
     tNames[key]= value.name; 
   end
   unit_to_check = ScenEdit_GetUnit({side='OpFor', name='TestRQ180'})
   local checkval = false;
   checkval = unit_to_check:inArea(tNames);
   if checkval == true then
     print('in area');
   end
 else
   print('Reference point list was empty');
 end


User avatar
Primarchx
Posts: 1954
Joined: Sun Jan 20, 2013 9:29 pm

RE: Using the Unit.inArea method

Post by Primarchx »

I'm working on a method of keeping SAMs on Weapons Tight unless a hostile a/c comes within a certain distance of them. Every 30 seconds eligible SAMs have a small chance of firing and even then they're Weapons Free for just a minute or so. So I'm testing Blue a/c to see if they're in a permissive zone for Red SAMs to see if those SAMs might light them up. This works when I trigger checks based on Event Editor 'Unit in Area' but I don't want these events going off all the time. Instead I have pulsed 30 second and 1 minute events that do this sort of thing using Lua.

Thanks for the help!
rneum
Posts: 17
Joined: Fri Nov 12, 2021 4:56 pm

RE: Using the Unit.inArea method

Post by rneum »

I've found the Unit:inArea() works with manually defined reference points such as:

t = unit_to_check ({'RP-6380','RP-6381','RP-6382','RP-6383'})

It doesn't work with programmatically generated reference points such as the following:


unit_to_check = ScenEdit_GetUnit({side='OpFor', name='TestRQ180'})
offset=1
local rp1=ScenEdit_AddReferencePoint( {side='OpFor', name='A', lat=unit_to_check.latitude + (offset/10), lon=unit_to_check.longitude + (offset/10), highlighted=true})
local rp2=ScenEdit_AddReferencePoint( {side='OpFor', name='B', lat=unit_to_check.latitude + (offset/10), lon=unit_to_check.longitude - (offset/10), highlighted=true})
local rp3=ScenEdit_AddReferencePoint( {side='OpFor', name='C', lat=unit_to_check.latitude - (offset/10), lon=unit_to_check.longitude + (offset/10), highlighted=true})
local rp4=ScenEdit_AddReferencePoint( {side='OpFor', name='D', lat=unit_to_check.latitude - (offset/10), lon=unit_to_check.longitude - (offset/10), highlighted=true})
local list_of_RPs = {};
list_of_RPs = ScenEdit_GetReferencePoints({side='OpFor', area={rp1.name,rp2.name,rp3.name,rp4.name}})
if list_of_RPs ~= nil then
local tNames = {};
for key,value in pairs(list_of_RPs) do
tNames[key]= value.name;
end
local checkval = false;
checkval = unit_to_check:inArea(tNames);
if checkval == true then
print('in area');
end
else
print('Reference point list was empty');
end
Attachments
Lua_AddRef..intError.zip
(70.74 KiB) Downloaded 4 times
rneum
Posts: 17
Joined: Fri Nov 12, 2021 4:56 pm

RE: Using the Unit.inArea method

Post by rneum »

Hey everyone, sorry about the tens of posts. I posted the Command Version and build numbers and it rejected the content. There seems to be content rules (regex??) that trigger "You are not allowed to post links, emails or phone numbers for 7 days from the date of your tenth post."

KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Using the Unit.inArea method

Post by KnightHawk75 »

renum,
I addressed your issues in the tech support thread, both with a functional sample of my own, and I found where the problems were in your script that were tripping you up (very easy fixes).
https://www.matrixgames.com/forums/tm.asp?m=5100173
Post Reply

Return to “Lua Legion”