Page 1 of 1
Using UnitX() In Conditions
Posted: Fri Oct 20, 2017 5:25 pm
by TitaniumTrout
Hello!
I'm working with Conditions in the Event Editor and can get it to pass a true or false depending on a variable. Working example here :
Code: Select all
local mission = ScenEdit_GetMission('OPFOR', 'Test')
if mission.isactive == true then
return true
else
return false
end
Now when I try to get some unit info on the triggering plane I just get a false return. Does UnitX() not work within the Condition field?
Code: Select all
local unit = ScenEdit_UnitX()
if unit.altitude > '1000' then
return true
else
return false
end
Also, are multiple conditions AND or OR with each other?
RE: Using UnitX() In Conditions
Posted: Sat Oct 21, 2017 8:12 am
by michaelm75au
Yes, you can use UnitX in a condition.
If any condition is FALSE, then condition fails.
Useful thing is that you can include a print in the Lua scripts, and they will print to a file 'LuaHistory' in folder 'logs'for the day.
RE: Using UnitX() In Conditions
Posted: Sat Oct 21, 2017 12:43 pm
by TitaniumTrout
Excellent! I wasn't aware of the lua log, that'll be very helpful for troubleshooting. I was comparing an integer with a string.
Code: Select all
local unit = ScenEdit_UnitX()
print(unit.altitude)
local minAlt = 3000
if unit.altitude < minAlt then
return true
else
return false
end
RE: Using UnitX() In Conditions
Posted: Sun Oct 22, 2017 12:15 am
by michaelm75au
Another useful tidbit:
To see what scripts are executing in events, open the Lua console and run the following.
_lua_event = true
This will list out the script executed to the history file. eg
22/10/2017 2:03:07 PM -- B976 --
Script:Within range
local x,y
local answer = false
-- unit base range on
local u = ScenEdit_GetUnit({name='Group_2612', guid='cd015e09-d208-4e67-9af0-3e8e0412209c'})
if u == nil then
return answer
end
-- unit that triggered event
local contact = ScenEdit_UnitX()
-- this unit seen as a contact by others
local con = contact.ascontact
-- not generated as a contact yet
if #con ~= 0 then
-- listed as a contact by some side
-- what is the side id for the base unit
local side = ScenEdit_GetSideOptions({side=u.side}).guid
-- find the side's entry in the triggering unit's contact list
for x = 1, #con do
if con[x].side == side then
-- contact on my side
local mycontact = con[x].guid
-- distance between my base unit and the contact
local dist = u:rangetotarget( mycontact)
if dist < 10 then
print('target in range (' .. dist ..'NM)')
ScenEdit_SetSidePosture( ScenEdit_GetSideOptions({side=contact.side}).guid, side ,'H')
answer=true
else
print('target not in range (' .. dist ..'NM)')
end
end
end
end
return answer
...
Function:ScenEdit_GetUnit (4) Error:Need to define a Name or Guid to identify a unit. Preferably a Guid or Side & Name.
22/10/2017 2:03:07 PM -- B976 --
Script:SAR
print('activate rescue')
local rescue = ScenEdit_UnitX()
local sar = ScenEdit_UnitY()
local chopper = nil
print(rescue)
print(sar)
if sar.unit.mission.name == 'SAR Mission' then
print( sar.unit.name .. ' rescuing ' .. rescue.name)
chopper = sar.unit
else
local mission = ScenEdit_GetMission(sar.unit.side, 'SAR Mission')
if mission == nill or mission.isactive == false then
print('no rescue')
else
for x = 1, #mission.unitlist do
local u = ScenEdit_GetUnit( {guid = mission.unitlist[x]})
print(u.airbornetime )
if #u.airbornetime > 0 then
chopper = u
print( chopper.name .. ' rescuing ' .. rescue.name)
break
else
print('no rescue')
end
end
end
end
if chopper ~= nil then
chopper.course = { {latitude = rescue.latitude, longitude = rescue.longitude, PRESETTHROTTLE='Hover', PRESETALTITUDE='MinAltitude'}}
end
activate rescue
unit {
type = 'Ship',
subtype = '9001',
name = 'Life Raft [5m]',
side = 'Markers',
guid = '7b6fc824-ad6f-4d77-9b55-d47729b2078b',
proficiency = 'Regular',
latitude = '33.8400329345168',
longitude = '136.823460247542',
altitude = '0',
heading = '0',
speed = '0',
throttle = 'FullStop',
autodetectable = 'False',
mounts = '0',
magazines = '0',
unitstate = 'Unassigned',
fuelstate = 'None',
weaponstate = 'None',
}
{ unit = unit {
type = 'Aircraft',
subtype = '6001',
name = 'Seeker #1',
side = 'SideA',
guid = '7a3b7dce-abe1-4b60-b0d5-b0f27510d4de',
proficiency = 'Regular',
latitude = '33.8481608094017',
longitude = '137.358870760905',
altitude = '457.2',
heading = '265.9691',
speed = '140',
throttle = 'Cruise',
autodetectable = 'False',
base = 'USS Rentz',
mission = 'SAR Mission',
mounts = '0',
magazines = '0',
unitstate = 'OnPlottedCourse',
fuelstate = 'None',
weaponstate = 'None',
}, sensor = { [1] = { name = 'Generic FLIR', type = 'Infrared' } } }
Seeker #1 rescuing Life Raft [5m]
22/10/2017 2:03:11 PM -- B976 --
Script:Within range
local x,y
local answer = false
-- unit base range on
local u = ScenEdit_GetUnit({name='Group_2612', guid='cd015e09-d208-4e67-9af0-3e8e0412209c'})
if u == nil then
return answer
end
-- unit that triggered event
local contact = ScenEdit_UnitX()
-- this unit seen as a contact by others
local con = contact.ascontact
-- not generated as a contact yet
if #con ~= 0 then
-- listed as a contact by some side
-- what is the side id for the base unit
local side = ScenEdit_GetSideOptions({side=u.side}).guid
-- find the side's entry in the triggering unit's contact list
for x = 1, #con do
if con[x].side == side then
-- contact on my side
local mycontact = con[x].guid
-- distance between my base unit and the contact
local dist = u:rangetotarget( mycontact)
if dist < 10 then
print('target in range (' .. dist ..'NM)')
ScenEdit_SetSidePosture( ScenEdit_GetSideOptions({side=contact.side}).guid, side ,'H')
answer=true
else
print('target not in range (' .. dist ..'NM)')
end
end
end
end
return answer
RE: Using UnitX() In Conditions
Posted: Sun Oct 22, 2017 12:20 am
by michaelm75au
Note the error reported
Function:ScenEdit_GetUnit (4) Error:Need to define a Name or Guid to identify a unit. Preferably a Guid or Side & Name.
It relates to the proceeding script 'Within range' on line (4). The reference used in the call does not exist.
Errors and print statements will show up after the dump of the script.
Again very useful for finding those weird cases before releasing the scenario into the "wild".[:D]