Jump to content

Altitude Script


wraith70

Recommended Posts

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

Link to comment
Share on other sites

@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 by Hardcard
Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

 

TIA

MissionFunctions.lua

Altitude Test with Function.miz


Edited by wraith70
Link to comment
Share on other sites

@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 by Hardcard
Link to comment
Share on other sites

@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 by Hardcard
Link to comment
Share on other sites

  • Recently Browsing   0 members

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