Lua API detect cleared Inputbox

  • LeonAir
  • LeonAir's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 57
  • Thank you received: 58

TPL_KUNENA_MESSAGE_CREATED Lua API detect cleared Inputbox

14 May 2021 17: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

Please Log in or Create an account to join the conversation.

  • LeonAir
  • LeonAir's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 57
  • Thank you received: 58

TPL_KUNENA_MESSAGE_REPLIED_NEW Lua API detect cleared Inputbox

10 Jun 2021 16: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
The following user(s) said Thank You: abief

Please Log in or Create an account to join the conversation.

  • abief
  • abief's Avatar
  • Offline
  • Premium Member
  • Premium Member
  • I'm here, there and elsewhere!
  • Posts: 95
  • Thank you received: 19

TPL_KUNENA_MESSAGE_REPLIED_NEW Lua API detect cleared Inputbox

15 Jun 2021 08:25 - 15 Jun 2021 08:27
#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 Jun 2021 08:27 by abief.

Please Log in or Create an account to join the conversation.

  • LeonAir
  • LeonAir's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 57
  • Thank you received: 58

TPL_KUNENA_MESSAGE_REPLIED_NEW Lua API detect cleared Inputbox

15 Jun 2021 11: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
The following user(s) said Thank You: abief

Please Log in or Create an account to join the conversation.

  • abief
  • abief's Avatar
  • Offline
  • Premium Member
  • Premium Member
  • I'm here, there and elsewhere!
  • Posts: 95
  • Thank you received: 19

TPL_KUNENA_MESSAGE_REPLIED_NEW Lua API detect cleared Inputbox

15 Jun 2021 16: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)

Please Log in or Create an account to join the conversation.

Time to create page: 0.322 seconds
Powered by Kunena Forum