MTM Posted December 12, 2023 Posted December 12, 2023 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 Zyll Posted December 12, 2023 Solution Posted December 12, 2023 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...endZyll @ TAW
MTM Posted December 13, 2023 Author Posted December 13, 2023 Thanks Zyll, that worked! It didn't like != for some reason so I tried ~= and it worked with that. Thanks for your help!
Zyll Posted December 13, 2023 Posted December 13, 2023 Whoops! You are right, got my nots mixed up, but you are on the ball. Zyll @ TAW 1
cfrag Posted December 13, 2023 Posted December 13, 2023 (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 December 13, 2023 by cfrag 1
Recommended Posts