Jump to content

Scripting Help; Aircraft Destruction Leads to Script Failure


IIJG77_Kieran310

Recommended Posts

Specifically with regards to 

Unit.getByName("Huey3")

Huey3 is the unit (and group) name, of the helicopter that is crashed by a player. The error messages continue until the player respawns. The script works as intended, until the destruction of a unit referenced in it.

The script:

local flagValue = trigger.misc.getUserFlag("52")
local LockFlag = trigger.misc.getUserFlag("53") -- to stop infinite loops and 9999+ F10 options
local TransGroup = Group.getByName("Lancer-6")
local TransUnit = Unit.getByName("Lancer-6")
local TransUnitPos = TransUnit:getPosition()
local InitUnit = Unit.getByName("Huey3")
local InitUnitPos = InitUnit:getPosition()
local InitUnitAGL = InitUnitPos.p.y - land.getHeight({x=InitUnitPos.p.x, y = InitUnitPos.p.z})
local InitGroup = Group.getByName("Huey3")
local InitID = Group.getID(InitGroup)
local InitTransDis = mist.vec.sub(InitUnitPos.p, TransUnitPos.p)
if ((InitTransDis.x <= 50 and InitTransDis.x > 0) or (InitTransDis.x < 0 and InitTransDis.x >= -50)) and ((InitTransDis.z <= 50 and InitTransDis.z > 0) or (InitTransDis.z < 0 and InitTransDis.z >= -50)) -- If distance between transporter (helo) and transportable (ground unit) is within a 100m flat radius
and LockFlag == 0 and InitUnitAGL <= 15 and InitUnit:getVelocity().x <= 5.55555 and InitUnit:getVelocity().y <=10 and InitUnit:getVelocity().y >= -10 then
trigger.action.addOtherCommandForGroup(InitID,"Pick up LANCER-6 (HMMWV-TOW)","52","1")
trigger.action.setUserFlag("53",1) -- lock flag for antispam
elseif not (((InitTransDis.x <= 50 and InitTransDis.x > 0) or (InitTransDis.x < 0 and InitTransDis.x >= -50)) or ((InitTransDis.z <= 50 and InitTransDis.z > 0) or (InitTransDis.z < 0 and InitTransDis.z >= -50))) then
trigger.action.removeOtherCommandForGroup(InitID,"Pick up LANCER-6 (HMMWV-TOW)")
-- trigger.action.outText("FALSE",20) -- debug line
trigger.action.setUserFlag("53",0) end -- lock disable
if flagValue == 1 then -- This is the pickup process of Lancer-6 by Huey3
TransGroup:destroy()
trigger.action.setUserFlag("52",2)
trigger.action.removeOtherCommandForGroup(InitID,"Pick up LANCER-6 (HMMWV-TOW)")
trigger.action.setUnitInternalCargo("Huey3",900)
trigger.action.outTextForGroup(InitID,"LANCER-6 (HMMWV-TOW) LOADED",10) end
if flagValue == 2 and LockFlag == 0 and InitUnitAGL <= 15 and InitUnit:getVelocity().x <= 5.55555 and InitUnit:getVelocity().y <=10 and InitUnit:getVelocity().y >= -10 then -- This gives the dropoff option if parameters are met, or disables it if not
trigger.action.addOtherCommandForGroup(InitID,"Drop off LANCER-6 (HMMWV-TOW)","52","3")
trigger.action.setUserFlag("53",1) -- lock flag for antispam
elseif flagValue == 2 and (InitUnitAGL >= 15 or InitUnit:getVelocity().x >= 5.55555 or (InitUnit:getVelocity().y >=10 and InitUnit:getVelocity().y <= -10)) then -- This removes the dropoff option if parameters are not met
trigger.action.removeOtherCommandForGroup(InitID,"Drop off LANCER-6 (HMMWV-TOW)")
trigger.action.setUserFlag("53",0) end -- lock disable
if flagValue == 3 then -- Drop off execution
mist.respawnGroup("Lancer-6", true) 
local InitUnitLoc = InitUnit:getPosition().p
local InitUnitDir = InitUnit:getPosition().z
local InitSPWN = mist.vec.add(InitUnitLoc, mist.vec.scalar_mult(InitUnitDir, 7))
mist.teleportToPoint({gpName="Lancer-6", point=InitSPWN, action="teleport"})
trigger.action.removeOtherCommandForGroup(InitID,"Drop off LANCER-6 (HMMWV-TOW)")
trigger.action.outTextForGroup(InitID,"LANCER-6 (HMMWV-TOW) UNLOADED",10)
trigger.action.setUnitInternalCargo("Huey3",0)
trigger.action.setUserFlag("52",0) end -- resets loop


In the editor, this script in run in a (continous trigger) > (2 seconds) and (Group Exists "Huey3") > (The script)

After some testing, trying other ways of stopping script execution on death, I have discovered that even though the (Group Exists) condition is no longer met, the script continues executing, and I can't figure out why.

 

This leads to pop-up error messages, repeatedly. As seen in picture below.

 

TUzlgxp.jpg

My DCS log file is attatched.

dcs.log


Edited by IIJG77_Kieran310
Link to comment
Share on other sites

Not sure, but it seems to me that "Huey3" is a group name. A unit name would be more like "Huey3-1" (you can check in the ME what actually is the group name and the unit name). If you write 

local InitUnit = Unit.getByName("Huey3")

and Huey3 is not a unit name, then InitUnit will be nil, hence the error message.

Link to comment
Share on other sites

No, it is also a unit name. I have named it as such specifically. The code runs fine - until Huey3 disappears (I missed this from the original post).

The error is because the function Unit.getByName is being run on a non-existent value.

Hence, ideally I would like the script to stop when Huey3 dies, for example.

 

BUUn9F5.png

 

I am quite new to Lua coding. Is there perhaps some way to stop error messages from popping up when an item is 'nil'? To get the game to just ignore it?

Thanks for the reply.


Edited by IIJG77_Kieran310
Link to comment
Share on other sites

Quote

before accessing group/unit you should check if exist/alive with :

 

if Group.getByName(groupName) and Group.getByName(groupName):isExist() == true and #Group.getByName(groupName):getUnits() > 0 then

 

or

 

if Unit.getByName(unitName) and Unit.getByName(unitName):isExist() == true then

 

Thanks, I will give it a try!

Edit: Works perfectly, appreciate the help


Edited by IIJG77_Kieran310
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...