Jump to content

New to scripting... need some help plz!


Go to solution Solved by Zyll,

Recommended Posts

Posted

Greetings all. I've just started trying to learn how to use scripting and not having much success. I want to create a script tied to an F10 radio item that will show me my wingman's fuel percentage. I got that to work using the following script...

local a1 = Unit.getByName('Hornet 1-1')
local b1 = Unit.getFuel(a1)
local c1 = math.floor(b1*10000)/100
trigger.action.outText("Hornet 1-1 Fuel Remaining: "..c1.."%",10)  

 

Only problem is I get an error message if my wingman is already dead. So, I'm guessing I need an if-then condition to tell the script to only run if my wingman is still alive. I know it can be done using the DCS built-in trigger system, but I'd really like to learn how to do it in LUA. 

Anyone want to share their wisdom on this? I have no prior experience with coding, so this is uncharted territory for me. Any help would be appreciated. Thanks in advance!

  • Solution
Posted

a1 will evaluate to nil if it doesn't exist, so you can simply wrap the lines that follow in an if clause:

if a1 != nil then
...
end



Zyll @ TAW

Posted

Thanks Zyll, that worked! It didn't like != for some reason so I tried ~= and it worked with that. Thanks for your help!

Posted (edited)
9 hours ago, Zyll said:

if a1 ~= nil then
...
end

In Lua, you can simplify that to 

if a1 then 
 ...
end

It's a matter of preference - to me that is more readable, to others it may seem strange, so use whatever works best for you

Edited by cfrag
  • Like 1
  • Recently Browsing   0 members

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