switch_add
Simulation Instrument Studio 1
Definition
switch_id = switch_add(img_pos_0, img_pos_1, ..., x, y, width, height, click_callback)
Remarks
Add a multi-position switch. Callback receives the new position.
Example: the simulator drives the switch
A click sends a command. The picture does not move until the sim reports the new key position. Every panel then shows the same key.
-- The simulator owns the key. A click asks for a new position.
-- The picture moves only when the sim reports the key.
-- 0 Off, 1 Mag 1, 2 Mag 2, 3 Both, 4 Start.
function on_click(position, direction)
local next_pos = position + direction
if next_pos < 0 or next_pos > 4 then
return
end
if next_pos == 0 then xpl_command("sim/magnetos/magnetos_off_1") end
if next_pos == 1 then xpl_command("sim/magnetos/magnetos_left_1") end
if next_pos == 2 then xpl_command("sim/magnetos/magnetos_right_1") end
if next_pos == 3 then xpl_command("sim/magnetos/magnetos_both_1") end
if next_pos == 4 then xpl_command("sim/starters/engage_starter_1") end
end
function on_key(key)
switch_set_position(ignition, key)
end
ignition = switch_add("ign_off.png", "ign_mag1.png", "ign_mag2.png", "ign_both.png", "ign_start.png", 40, 180, 48, 48, on_click)
xpl_dataref_subscribe("sim/cockpit2/engine/actuators/ignition_key[0]", "INT", on_key)Example: the switch drives the simulator
A click moves the key, then writes that position. Use this when the instrument is the control, not a mirror.
-- The switch owns the key. A click moves it, then tells the sim.
-- 0 Off, 1 Mag 1, 2 Mag 2, 3 Both, 4 Start.
function on_click(position, direction)
local next_pos = position + direction
if next_pos < 0 then next_pos = 0 end
if next_pos > 4 then next_pos = 4 end
switch_set_position(ignition, next_pos)
xpl_dataref_write("sim/cockpit2/engine/actuators/ignition_key[0]", "INT", next_pos)
end
ignition = switch_add("ign_off.png", "ign_mag1.png", "ign_mag2.png", "ign_both.png", "ign_start.png", 40, 180, 48, 48, on_click)


