Lua API detect cleared Inputbox

  • LeonAir
  • LeonAir's Avatar Offline Autor
  • Senior Mitglied
  • Senior Mitglied
  • Beiträge: 57
  • Dank erhalten: 58

Lua API detect cleared Inputbox wurde erstellt von LeonAir

Posted 14 Mai 2021 19:13 #1
Hello everyone,

currently there is no way to detect whether a switch was deleted in an inputbox.
Code:
local idx = form.addInputbox(nil, true) print(type(form.getValue(idx)), system.getInputsVal(form.getValue(idx))) form.setValue(idx, nil) print(type(form.getValue(idx)), system.getInputsVal(form.getValue(idx))) >> userdata 0.0 >> userdata 0.0

There is a workaround when proportional is disabled:
Code:
local switch -- represents the assigned switchitem local function initForm() form.registerInputbox(nil, false, function(value) switch = system.getInputsVal(value) ~= 0.0 and value or nil end) end

However this does not work when proportional is enabled, since an 'empty' SwitchItem cannot be distinguished from a proportional control that is in middle position.
The best fix for this would probably be that the value of an inputbox is nil when no control is assigned.
Alternatively there could be a second boolean parameter for the callback function.

Best regards
Leon
von LeonAir

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • LeonAir
  • LeonAir's Avatar Offline Autor
  • Senior Mitglied
  • Senior Mitglied
  • Beiträge: 57
  • Dank erhalten: 58

LeonAir antwortete auf Lua API detect cleared Inputbox

Posted 10 Juni 2021 18:06 #2
For everyone who still has the same problem:
Apparently there is a function system.getSwitchInfo that I managed to overlook like ten times.
It works just fine:

Code:
local switch -- assigned switchitem local function initForm() form.registerInputbox(switch, true, function(value) local info = system.getSwitchInfo(value) switch = (info and info.assigned) and value or nil system.pSave("key", switch) end) end

When the callback function is expected to be called from elsewhere with value = nil, line 5 should be extended like this:

Code:
local info = value and system.getSwitchInfo(value) or nil
von LeonAir
Folgende Benutzer bedankten sich: abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • abief
  • abief's Avatar Offline
  • Premium Mitglied
  • Premium Mitglied
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

abief antwortete auf Lua API detect cleared Inputbox

Posted 15 Juni 2021 10:25 #3
To detect if it has been deleted, you can check the "label" attribute for "??" value, I think.
Code:
label == "??"
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
Last Edit:15 Juni 2021 10:27 von abief
Letzte Änderung: 15 Juni 2021 10:27 von abief.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • LeonAir
  • LeonAir's Avatar Offline Autor
  • Senior Mitglied
  • Senior Mitglied
  • Beiträge: 57
  • Dank erhalten: 58

LeonAir antwortete auf Lua API detect cleared Inputbox

Posted 15 Juni 2021 13:08 #4
label == "??" works as well, but string comparison is less efficient than just checking the boolean value, so I would recommend using
Code:
info.assigned
von LeonAir
Folgende Benutzer bedankten sich: abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • abief
  • abief's Avatar Offline
  • Premium Mitglied
  • Premium Mitglied
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

abief antwortete auf Lua API detect cleared Inputbox

Posted 15 Juni 2021 18:09 #5
true, but it all depends on where you are using the code. If is in the configuration form, is something that you normally set and forget, so it's used once, maybe twice during the whole lifetime of the model, so is effectively "dead" code and there is no need to optimize it as much as possible. Would be different if it was within the loop, where you need to be as efficient as possible.

I take notice of your proposed use of
Code:
info.assigned
anyway. One never knows when things get handy.

regards,
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
von abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Ladezeit der Seite: 1.008 Sekunden