wraith70 Posted January 1, 2020 Posted January 1, 2020 I'm trying to write a script to display the altitude of an aircraft. Here is what I have so far: local Unit = {} local id = Unit.getByName('UH1H') local pos = id:getPosition() local distance = sqrt((pos.p.x - zonepos.x)^2 + (pos.p.z - zonepos.z)^2) pos2d.x = pos.p.x --convert Vec3 to Vec2 pos2d.y = pos.p.z landheight = land.getHeight(pos2d) -- get land height at aircraft location local _altitude = pos.p.y - landheight local _string = 'Altitude: ' .. _altitude trigger.action.outTextForGroup(1, _string, 30, false) When I try it in game and in a LUA decoder, I pretty much get the same result:input:2: attempt to call a nil value (field 'getByName') What can I be doing wrong here? TIA
funkyfranky Posted January 1, 2020 Posted January 1, 2020 Delete the first line. A warrior's mission is to foster the success of others. i9-12900K | RTX 4090 | 128 GB Ram 3200 MHz DDR-4 | Quest 3 RAT - On the Range - Rescue Helo - Recovery Tanker - Warehouse - Airboss
wraith70 Posted January 1, 2020 Author Posted January 1, 2020 When I delete the first line, I get this: input:1: attempt to index a nil value (global 'Unit') Actually made it worse.
Hardcard Posted January 2, 2020 Posted January 2, 2020 (edited) @wraith70 It definitely made it better, but there are other problems. Is there an existing UNIT called "UH1H" when the script runs? (It must be the PILOT name) If there isn't, you'll get that error. Also, as a general rule, refrain from giving variables the exact same names of DCS classes and common parameters, otherwise you'll have problems all the time. (Avoid names like Unit, Group, pos, id, etc.) As for getting the unit's altitude, do this: local Huey = Unit.getByName('UH1H') [color="Blue"]-- A unit called [i][b]UH1H[/b][/i] (PILOT name) must exist when this runs, otherwise it'll error[/color] local Huey_Altitude_MSL = Huey:getPoint().y [color="blue"]-- Altitude MSL, in meters[/color] [color="Blue"]-- Now, if you also want to know the huey's altitude from ground, do this:[/color] local Huey_Vec3 = Huey:getPoint() local Huey_Land_Height = land.getHeight({x = Huey_Vec3.x , y = Huey_Vec3.z}) local Huey_Altitude_Ground = Huey_Altitude_MSL - Huey_Land_Height [color="blue"]-- Altitude from ground in meters[/color] Edited January 2, 2020 by Hardcard [sIGPIC][/sIGPIC]
wraith70 Posted January 2, 2020 Author Posted January 2, 2020 @Hardcard Thanks for your input. I did not have the pilot name, so I fixed that, copied verbatim what you posted above, and pasted in a DO SCRIPT and I still get the message: attempt to index a nil value (global 'Unit') on the first line.
Hardcard Posted January 2, 2020 Posted January 2, 2020 @wraith70 Works for me, you're doing something wrong. Post your mission file, we'll see what the problem is [sIGPIC][/sIGPIC]
wraith70 Posted January 2, 2020 Author Posted January 2, 2020 @Hardcard I made a typo. It works great. Thanks for your help.
wraith70 Posted January 4, 2020 Author Posted January 4, 2020 (edited) Ok, So I want to take this one step further. Calling the code from a function in a separate LUA file. Here is the function in MissionFunctions.lua: function AGL(pilot_name,unit_of_measure) --[[ Return Aircraft AGL altitude in Meters or Feet Arguments pilot_name = Name of Pilot/Unit in ME unit_of_measure = Meters or Feet ]]-- local Huey = Unit.getByName(pilot_name) local Huey_Altitude_MSL = Huey:getPoint().y -- Altitude MSL, in meters -- Huey's altitude from ground local Huey_Vec3 = Huey:getPoint() local Huey_Land_Height = land.getHeight({x = Huey_Vec3.x , y = Huey_Vec3.z}) -- Altitude from ground in meters local Huey_Altitude_Ground = Huey_Altitude_MSL - Huey_Land_Height --AGL in Meters local Huey_AGL_Meters = mist.utils.round(Huey_Altitude_Ground , 0) --Round to 0 decimal places --Convert AGL from Meters to Feet local Huey_AGL_Feet = mist.utils.round(Huey_Altitude_Ground * 3.28084, 0) --Round to 0 decimal places if unit_of_measure == meters then return Huey_AGL_Meters end if unit_of_measure == feet then return Huey_AGL_Feet end end Here is how I'm calling from DO SCRIPT in ME: local _string = AGL('Pilot# 001', 'feet') trigger.action.outTextForGroup(1, _string, 30, false) I get a error about attempt to load a NIL value again. This must be a scope thing. Attached is a mission file. TIAMissionFunctions.luaAltitude Test with Function.miz Edited January 4, 2020 by wraith70
Hardcard Posted January 4, 2020 Posted January 4, 2020 (edited) @wraith70 Units of measure are passed as strings, not variables, the if conditions in your function should reflect this. Also, you don't need MIST (or MOOSE) to round up values, standard Lua math.floor() is all you need. Here's a corrected version of your script (haven't tested it, but I don't see why it shouldn't work): function AGL(pilot_name,unit_of_measure) [color="Blue"]--[[ Return Aircraft AGL altitude in Meters or Feet Arguments pilot_name = Name of Pilot/Unit in ME unit_of_measure = Meters or Feet ]]--[/color] local Huey = Unit.getByName(pilot_name) local Huey_Altitude_MSL = Huey:getPoint().y -- Altitude MSL, in meters -- Huey's altitude from ground local Huey_Vec3 = Huey:getPoint() local Huey_Land_Height = land.getHeight({x = Huey_Vec3.x , y = Huey_Vec3.z}) -- Altitude from ground in meters local Huey_Altitude_Ground = Huey_Altitude_MSL - Huey_Land_Height --AGL in Meters local Huey_AGL_Meters = math.floor(Huey_Altitude_Ground , 0) [color="blue"]--Round to 0 decimal places[/color] --Convert AGL from Meters to Feet local Huey_AGL_Feet = math.floor(Huey_Altitude_Ground * 3.28084 , 0) [color="blue"]--Round to 0 decimal places[/color] if unit_of_measure == 'meters' then [color="Blue"]-- Notice the quotation marks[/color] return Huey_AGL_Meters end if unit_of_measure == 'feet' then [color="Blue"]-- Notice the quotation marks[/color] return Huey_AGL_Feet end end Edited January 4, 2020 by Hardcard [sIGPIC][/sIGPIC]
wraith70 Posted January 4, 2020 Author Posted January 4, 2020 Thanks for your response. I've incorporated the changes you suggested. I still get the attached error message. The code works fine until i call it in a function. Attached is also the updated mission file. ThanksAltitude Test with Function.miz
Hardcard Posted January 4, 2020 Posted January 4, 2020 (edited) @wraith70 There are two problems in your mission Incorrect pilot name in the function call, you wrote "Pilot# 001" instead of "Pilot #001" MissionFunctions.lua needs more than 1 second to be fully loaded. If you start calling functions just one second after loading it, you'll get errors. As a rule of thumb, load your function libraries at mission start (no conditions) and give them a few seconds to load, before you start calling stuff from other scripts. Edited January 4, 2020 by Hardcard [sIGPIC][/sIGPIC]
wraith70 Posted January 4, 2020 Author Posted January 4, 2020 Pilot #001 makes it work..go figure. I've loaded the function library at mission start. It's all working now. Thanks again Hardcard
Recommended Posts