Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post bug reports and ask for game support here.

Moderator: MOD_Command

Post Reply
rneum
Posts: 17
Joined: Fri Nov 12, 2021 4:56 pm

Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by rneum »

I apologize for the multiple posts. I'm receiving an error of "You are not allowed to post links, emails or phone numbers for 7 days from the date of your tenth post." for posts with content such as the Command build and version numbers. I've determined the forum dislikes long numbers (lat and lon to 13 decimal places)

How do I use programmatically generated reference points with the Unit:inArea() function?

BLUF:
Command Version One dot Zero four, Build One-Thousand One-Hundred and forty-seven dot thirty-four
Lua_AddReferencepointError.scen
Test_InArea.lua.txt - Script to run in Lua Script Console

The Unit:inArea({'RP-6380','RP-6381','RP-6382','RP-6383'}) works as expected with manually created reference points, but does not produce a proper result with ScenEdit_AddReferencePoint() generated reference points. I'd thought the issue was a name format and added the ability to use mimicry (RP-...). that doesn't fix it either.

I attached the .scen file with the manual RPs and lua to run in the Lua script console. You can also create a new scenario, define the area manually area as N49, 37', 40"/W2,32',20" x N49,24',30/W2,18',40" and run the following test function.

Code to reproduce is attached in the Test_InArea.lua.txt.

This isn't a blocker, but it prevents a unit test for a function. It will be when I use actions to dynamically deploy randomized reference points.
==================================================================
==================================================================


function TestInArea()
-- Define area as N49, 37', 40"/W2,32',20" x N49,24',30/W2,18',40"
--Enter the RP Names below
local mrp1 = 'RP-6380'
local mrp2 = 'RP-6381'
local mrp3 = 'RP-6382'
local mrp4 = 'RP-6383'
local nameRp1 = 'PortsInAreaA'
local nameRp2 = 'PortsInAreaB'
local nameRp3 = 'PortsInAreaC'
local nameRp4 = 'PortsInAreaD'
mimicmanualnames = false
if mimicmanualnames then
nameRp1 = 'RP-9100'
nameRp2 = 'RP-9101'
nameRp3 = 'RP-9102'
nameRp4 = 'RP-9103'
end
--setup
testside = VP_GetSides ()[1].name
local port = { name = 'Saint Sampson', shore = { latitude = 49.48, longitude = -2.45 }, harbor = { latitude = 49.51, longitude = -2.42 }, country = 'Guernsey' }

local retval,shipT = pcall(ScenEdit_AddUnit,{type ='Ship', unitname =port.name .. " Buoy", dbid=347, side =VP_GetSides ()[1].name, Lat=port.harbor.latitude,Lon=port.harbor.longitude}) ;
local testship
if not retval or shipT ~= nil then
testship = ScenEdit_GetUnit({name=shipT.name , guid=shipT.guid});
end
size = 1
local rp1=ScenEdit_AddReferencePoint( {side=testside, name=nameRp1, lat=testship.latitude + (size/10), lon=testship.longitude + (size/10), highlighted=true})
local rp2=ScenEdit_AddReferencePoint( {side=testside, name=nameRp2, lat=testship.latitude + (size/10), lon=testship.longitude - (size/10), highlighted=true})
local rp3=ScenEdit_AddReferencePoint( {side=testside, name=nameRp3, lat=testship.latitude - (size/10), lon=testship.longitude + (size/10), highlighted=true})
local rp4=ScenEdit_AddReferencePoint( {side=testside, name=nameRp4, lat=testship.latitude - (size/10), lon=testship.longitude - (size/10), highlighted=true})
-- test
if testship ~= nil then
t = testship:inArea({'RP-6380','RP-6381','RP-6382','RP-6383'})
t = testship:inArea({mrp1,mrp2,mrp3,mrp4})
print("Manual RPs tested testship:inArea({'" .. mrp1 .. "','" .. mrp2 .. "','" .. mrp3 .. "','" .. mrp4 .. "'}) match:")
print(t)
t = testship:inArea({nameRp1,nameRp1,nameRp1,nameRp1})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({'" .. nameRp1 .. "','" .. nameRp2 .. "','" .. nameRp3 .. "','" .. nameRp4 .. "'}) match:")
print(t)
t = testship:inArea({rp1.name,rp2.name,rp3.name,rp4.name})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({rp1.name,rp2.name,rp3.name,rp4.name}) match:")
print(t)
t = testship:inArea({rp1.guid,rp2.guid,rp3.guid,rp4.guid})
print("Lua based RPs ceated using ScenEdit_AddReferencePoint tested testship:inArea({rp1.guid,rp2.guid,rp3.guid,rp4.guid}) match:")
print(t)

end
-- cleanup
strnum = ScenEdit_InputBox ('pause before cleanup')
if testship ~= nil and testship:delete() then end
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp1.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp2.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp3.guid})
ScenEdit_DeleteReferencePoint ({side=testside, guid=rp4.guid})
end
TestInArea()
Attachments
Lua_AddRef..intError.zip
(70.74 KiB) Downloaded 8 times
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by KnightHawk75 »

See my attached testbed sample for a functional example.
You can either look at the special actions or to test under 'event' conditions enable the regularly timed event.
Both execute the following for the auto-generation test (that also include a test of manually placed tanker in the scene).
It creates the rp's and then uses them in a unit:inArea() test, that should be true based on it's generate location.

Code: Select all

 local function TestAutoRPS() 
   local autolats = {26.077119, 26.092939, 23.935986, 23.917725}
   local autolons = {-95.459756, -91.126452, -91.078016, -95.414230}
   local autonamebase = "Auto-";
   local autosidename = "Green";
   local autorps = {}
   local u = SE_GetUnit({side="Green",name="Tanker VLCC #1"})
   local retvalu2,u2 = pcall(ScenEdit_AddUnit,{side=autosidename,type="Ship",dbid=259,name="AutoGen Tanker VLCC",latitude=25.0849,longitude=-93.0443,heading=0,speed=0})
 

Code: Select all

   print('adding auto ref points.');
   for i = 1,4 do
     autorps[i] = ScenEdit_AddReferencePoint({side=autosidename, name=autonamebase .. tostring(i), latitude=autolats[i], longitude=autolons[i]})
     if autorps[i] ~= nil then print('refpoint '.. tostring(autorps.name) .. ' added.') end
   end
   print("-- Sanity checking. --")
   print("verifying autorps returns");
   for k,v in ipairs(autorps) do
     print(v.name .. ":  lat: " ..v.latitude .. " lon: " .. v.longitude )
   end
   print("verifying autorps with side lookup and getrefpoint calls")
   local s = VP_GetSide({side="Green"})
   for k,v in ipairs(s.rps) do
     if string.find(v.name,'Auto-',1,true) ~= nil then --filter for only Auto-xx rps
       print("getting refference point for " .. v.name)
       local rp = ScenEdit_GetReferencePoint({side="Green",guid=v.guid})
       print("rp obtained - " ..rp.name .. ":  lat: " ..rp.latitude .. " lon: " .. rp.longitude )
       rp = nil;
     end
   end
   print("-- End sanity checking. --");
 

Code: Select all

   if #autorps == 4 then --Do we have list and right number of autogen rps?
     local retval = u:inArea({'Auto-1','Auto-2','Auto-3','Auto-4'})
     print('Manual tanker is in the Auto-X area? ' .. tostring(retval));
     ScenEdit_MsgBox("Manual tanker is in the Auto-X rp Area? : " .. tostring(retval),1)
   end
   if (retvalu2 ==true) and u2 ~=nil then --was unit creation successful?
     local retval = u2:inArea({'Auto-1','Auto-2','Auto-3','Auto-4'})
     print('Auto generated tanker is in the Auto-X area? ' .. tostring(retval));
     ScenEdit_MsgBox("Auto generated tanker is in the Auto-X Area? : " .. tostring(retval),1)
   end
 

Code: Select all

   --cleanup
   if u2 ~=nil then --if unit exists remove it.
     print("removing Auto generated unit.");
     u2:delete();
   end
   print("removing Auto-X ref points.")
   for i=1,#autorps do
     if autorps[i] ~= nil then --skip if nil.
       local strsuccess = "Removed RP " .. tostring(autorps[i].name) .. "  by guid:".. tostring(autorps[i].guid);
       local retval1 = ScenEdit_DeleteReferencePoint({side=autosidename,guid=autorps[i].guid});
       if retval1 then print(strsuccess); else print('failed to remove'); end
     end
   end
 end
 TestAutoRPS();
 
Can also just plop the above into the console after loading the scene.
I'll have to dig into yours to see where things go wrong, probably something simple but not obvious at first glance.
Attachments
TestBed_Au.._114735.zip
(11.08 KiB) Downloaded 5 times
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by KnightHawk75 »

rneum,

Two problems in your original code:
first (primarily):

Code: Select all

local rp1=ScenEdit_AddReferencePoint( {side=testside, name=nameRp1, lat=testship.latitude + (size/10), lon=testship.longitude + (size/10), highlighted=true})
 local rp2=ScenEdit_AddReferencePoint( {side=testside, name=nameRp2, lat=testship.latitude + (size/10), lon=testship.longitude - (size/10), highlighted=true}) 
These two need to be reversed, the location calculated for #2 needed to be first, and the first one needs to be second, if you want a 4pt poly box shape. (this would fail normal geometric validation without it... on the map before cleanup you can see A is to the right of B). As it was unit actually was not really inside given the order of submission.

Second:

Code: Select all

    t = testship:inArea({nameRp1,[b]nameRp1,nameRp1,nameRp1[/b]}) <--you forgot to change the last 3 varnames.

Attached is your .txt file updated (working) with my changes (some vars weren't 'local' and I tweaked a few others, but the crux was what was already mentioned.)
Attachments
Test_InArea_lua_kh.txt
(3.72 KiB) Downloaded 18 times
rneum
Posts: 17
Joined: Fri Nov 12, 2021 4:56 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by rneum »

Thank you, the order of reference points. SMH
jannas34
Posts: 74
Joined: Thu Jan 09, 2020 4:09 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by jannas34 »

lololol this is not getting fixed
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by KnightHawk75 »

ORIGINAL: jannas34

lololol this is not getting fixed
Jannas34 - Did you read the thread? There was nothing to fix, it was just user error.
thewood1
Posts: 10137
Joined: Sun Nov 27, 2005 6:24 pm
Location: Boston

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by thewood1 »

What do you think the answer to that is? Does no one realize this guy posted in over 20 threads with similar messages. Why are wasting the effort in responding?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit:inArea() returns No when using ScenEdit_AddReferencePoint() generated reference points

Post by KnightHawk75 »

ORIGINAL: thewood1

What do you think the answer to that is? Does no one realize this guy posted in over 20 threads with similar messages. Why are wasting the effort in responding?
Answer to what is?
Yes I realized, _after_ this and a few other posts
Post Reply

Return to “Tech Support”