Function in a Function

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Post Reply
Message
Author
User avatar
BradyBrother100
Posts: 51
Joined: Tue Oct 13, 2020 4:21 pm
Location: United States MDT

Function in a Function

#1 Post by BradyBrother100 » Sat May 22, 2021 12:10 am

Hi! I am trying to recreate the Voltage Screen right above the battery switch in the Zibo 737. I want it so that when the battery switch is Off(pos == 0) the text disappears and when the battery switch is On(pos == 1), for the text to be displayed under certain circumstances which are determined by the position of the DC or AC Source Selector Knobs. I have the second part of that done(controlling the visibility of the text based of knob positions.) The problem is that AM gives me the "ERROR - logic.lua:109: Argument 'callback(3)' in function 'xpl_dataref_subscribe' is nil, which is not allowed" error even though I have assigned the correct callback to the function. That function is inside of another function. But when that function is outside of another function, I don't get that error. Is function in function not supported? Is there an easier way to go about this?

Thanks!
Brady

Code: Select all

canvas_add(0,0, 320, 170, function ()
    _rect(0, 0, 320, 170)
    _fill("black")
end)
dc_amps_txt = txt_add("88","font:digital-7-mono.ttf; size:28; color:green; halign:right", 35, 25, 50, 50)
dc_volts_txt = txt_add("88","font:digital-7-mono.ttf; size:28; color:green; halign:right", 35, 120, 50, 50)
ac_amps_txt = txt_add("88","font:digital-7-mono.ttf; size:28; color:green; halign:right", 137, 120, 50, 50)
ac_volts_txt = txt_add("888","font:digital-7-mono.ttf; size:28; color:green; halign:right;", 231, 120, 50, 50)
ac_freq_txt = txt_add("888","font:digital-7-mono.ttf; size:28; color:green; halign:right;", 231, 25, 50, 50)




--Knob Stuff

function battery(pos)
    if pos == 0 then
        visible(ac_amps_txt, false)
        visible(ac_volts_txt, false)
        visible(ac_freq_txt, false)
        visible(dc_amps_txt, false)
        visible(dc_volts_txt, false) 
    elseif pos == 1 then
        function dc_knob(pos)
            if pos == 0 then
            visible(dc_amps_txt, false)
            visible(dc_volts_txt, true)
            elseif pos == 1 then
            visible(dc_amps_txt, false)
            visible(dc_volts_txt, true)
            elseif pos == 2 then
            visible(dc_amps_txt, true)
            visible(dc_volts_txt, true)
            elseif pos == 3 then
            visible(dc_amps_txt, true)
            visible(dc_volts_txt, true)
            elseif pos == 4 then
            visible(dc_amps_txt, true)
            visible(dc_volts_txt, true)
            elseif pos == 5 then
            visible(dc_amps_txt, true)
            visible(dc_volts_txt, true)
            elseif pos == 6 then
            visible(dc_amps_txt, false)
            visible(dc_volts_txt, false)
        end
        end
    
        function ac_knob(pos)
            if pos == 0 then
            visible(ac_amps_txt, false)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 1 then
            visible(ac_amps_txt, false)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 2 then
            visible(ac_amps_txt, true)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 3 then
            visible(ac_amps_txt, true)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 4 then
            visible(ac_amps_txt, true)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 5 then
            visible(ac_amps_txt, false)
            visible(ac_volts_txt, true)
            visible(ac_freq_txt, true)
            elseif pos == 6 then
            visible(ac_amps_txt, false)
            visible(ac_volts_txt, false)
            visible(ac_freq_txt, false)
    end
    end   
end
end     
-------------------------------------------




--Text Set Functions
function ac_amp(value)
    txt_set(ac_amps_txt, string.format("%.0f", value))   
end

function dc_amp(value)
    txt_set(dc_amps_txt, string.format("%.0f", value))   
end

function ac_volt(value)
    txt_set(ac_volts_txt, string.format("%.0f", value))   
end

function dc_volt(value)
    txt_set(dc_volts_txt, string.format("%.0f", value))   
end

function ac_freq(value)
    txt_set(ac_freq_txt, string.format("%.0f", value))   
end
----------------------------------------------------------
xpl_dataref_subscribe("laminar/B738/electric/battery_pos", "FLOAT", battery)
xpl_dataref_subscribe("laminar/B738/knob/ac_power", "FLOAT", ac_knob)
xpl_dataref_subscribe("laminar/B738/knob/dc_power", "FLOAT", dc_knob)
xpl_dataref_subscribe("laminar/B738/ac_amp_value", "FLOAT", ac_amp)
xpl_dataref_subscribe("laminar/B738/dc_amp_value", "FLOAT", dc_amp)
xpl_dataref_subscribe("laminar/B738/dc_volt_value", "FLOAT", dc_volt)
xpl_dataref_subscribe("laminar/B738/ac_volt_value", "FLOAT", ac_volt)
xpl_dataref_subscribe("laminar/B738/ac_freq_value", "FLOAT", ac_freq)

User avatar
Sling
Posts: 4504
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Function in a Function

#2 Post by Sling » Sat May 22, 2021 2:48 am

You can’t have the subscribe callback inside another function. When the execution hits the subscribe with this callback it cannot find the callback function so will error.

In this case it’s probably better to store the battery switch state in a variable and use it in the other callbacks. BTW you don’t have to use visibility to turn them on/off, you can just set the text string to blank to achieve the same.

You can nest functions but the initial callbacks must be in the main code execution.

Tony

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests