Page 1 of 1
					
				 Using the Unit.inArea method
				Posted: Sun Jan 27, 2019 4:04 am
				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?
			 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sun Jan 27, 2019 7:56 pm
				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.
 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sun Jan 27, 2019 8:10 pm
				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!
			 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sun Jan 27, 2019 8:41 pm
				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
 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sun Jan 27, 2019 9:59 pm
				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!
			 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sat Nov 13, 2021 8:44 am
				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
			 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sat Nov 13, 2021 9:24 am
				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."
 
 
			 
			
					
				 RE: Using the Unit.inArea method
				Posted: Sun Nov 14, 2021 6:24 am
				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