Nearest Way Point

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Nearest Way Point

#11 Post by Keith Baxter »

Hi,

Try this.

Code: Select all

 robR_36witC="font:arimo_bold.ttf; size:36px; color: white; halign:left; valign:center;"

    lat1=0
	lon1=0



background = canvas_add(0, 0, 600, 600, function()
   _rect(0,0,400,512)
   _fill("blue")

end)

---What this function is doing is creating a TABLE named <waypoints>
local function data_callback(waypoints)
  if waypoints ~= nil then
	canvas_draw( background,function()
		_rect(0,0,400,512)
        _fill("blue")

		for i=1, #waypoints do
			print(" name:" .. waypoints[i]["ICAO"] .. " lon:" .. waypoints[i]["LONGITUDE"] .. " lat:" .. waypoints[i]["LATITUDE"])
			_txt(waypoints[i]["ICAO"],robR_36witC,200,100+ (i*35))
			
		end
     end)
  end
end
	
	   
function xpl_dataref_subscribe_trottle(...)
    -- Local variables
    local args = {...}
    local user_callback = args[#args - 1]
    local delta_ms = table.remove(args, #args)
    local data = nil
    
    -- Replace the user callback with our own
    args[#args] = function (...)
        data = {...}
    end
    
    -- Do the actual subscribe
    xpl_dataref_subscribe(table.unpack(args))
    
    -- Start a timer that will fire the callback on requested interval
    timer_start(0, delta_ms, function()
        if data ~= nil then
            user_callback(table.unpack(data))
            data = nil
        end
    end)
end

function new_xpl_data(latitude, longitude)
    lat1=latitude
	lon1=longitude
    print("Latitude: " .. lat1)
    print("Longitude: " .. lon1)
    nav_get_nearest("WAYPOINT",lat1, lon1, 1, data_callback)	   
	data_callback()  
end

xpl_dataref_subscribe_trottle("sim/flightmodel/position/latitude", "DOUBLE",
                      "sim/flightmodel/position/longitude", "DOUBLE", new_xpl_data, 1000)


Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: Nearest Way Point

#12 Post by Marcnamara2021 »

Thank You a lot. Regards. I will try.

User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: Nearest Way Point

#13 Post by Marcnamara2021 »

Thank You a lot.

But I'm going to do an immersive learning to get the proper skills on the programming needed to build elegant, short and efficient scripts.

Regards

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Nearest Way Point

#14 Post by jph »

Marcnamara2021 wrote: Sat Dec 11, 2021 12:00 pm Thank You a lot.

But I'm going to do an immersive learning to get the proper skills on the programming needed to build elegant, short and efficient scripts.

Regards
Hi Marc,
if I can suggest - simply make it work ! - the 'elegant, short and efficient scripts' is mere icing on the cake and makes little difference (IF ANY!) to the usage. Concentrate on simply making it do what you want it to do.
The rest is simply not needed at this stage - or any really IF it works for you.
THE key is to make it so YOU understand your code.

The important things are -
COMMENT THE CODE - in a way that works for you and you can go back to a year later and it still makes sense.
check it works ok
Make multiple backups as you progress.
Make changes as needed then -

COMMENT THE CODE - in a way that works for you and you can go back to a year later and it still makes sense.
check it works ok
Make multiple backups as you progress
Make changes as needed then -

COMMENT THE CODE - in a way that works for you and you can go back to a year later and it still makes sense.
check it works ok
Make multiple backups as you progress.
Make changes as needed then -

Be happy.

The rest is only when you are happy. Elegant code is fine. The BEST code is what works and what you can understand - now, and in a year - or two years.

Best of luck
Joe. ;)
Joe. CISSP, MSc.

User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: Nearest Way Point

#15 Post by Marcnamara2021 »

:D :D :D Thanks!!! I will remember.

Post Reply