Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:
  • Seite:
  • 1

THEMA:

Lua API detect cleared Inputbox 14 Mai 2021 19:13 #1

  • LeonAir
  • LeonAirs Avatar Autor
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 57
  • Dank erhalten: 58
Hello everyone,

currently there is no way to detect whether a switch was deleted in an inputbox.
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:
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

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Lua API detect cleared Inputbox 10 Jun 2021 18:06 #2

  • LeonAir
  • LeonAirs Avatar Autor
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 57
  • Dank erhalten: 58
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:

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:

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

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Lua API detect cleared Inputbox 15 Jun 2021 10:25 #3

  • abief
  • abiefs Avatar
  • Offline
  • Expert Boarder
  • Expert Boarder
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19
To detect if it has been deleted, you can check the "label" attribute for "??" value, I think.
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)

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Letzte Änderung: von abief.

Lua API detect cleared Inputbox 15 Jun 2021 13:08 #4

  • LeonAir
  • LeonAirs Avatar Autor
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 57
  • Dank erhalten: 58
label == "??" works as well, but string comparison is less efficient than just checking the boolean value, so I would recommend using
info.assigned
Folgende Benutzer bedankten sich: abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Lua API detect cleared Inputbox 15 Jun 2021 18:09 #5

  • abief
  • abiefs Avatar
  • Offline
  • Expert Boarder
  • Expert Boarder
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19
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
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)

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • Seite:
  • 1
Ladezeit der Seite: 0.219 Sekunden
Powered by Kunena Forum