I am in the process of building a switch-board which contains a number of switches which vary dependent upon the configuration of a 2-dimensional array.
Having succeeded in placing a variable number of switches on the screen it only now occurs to me that I seem to have no way of knowing when the common callback routine fires which switch has been pressed. I have looked and can't find any obvious solutions.
Any ideas?
Colin
Switch Context
-
- Posts: 236
- Joined: Sun Feb 14, 2021 6:55 pm
Re: Switch Context
Hi Colin
In LUA you can use a function that returns a callback function.
Instead of the following code:
Use this kind of code:
Now each switch gets its own callback function, although the logic is the same for all switches.
With some advanced LUA features called 'closures' you can even have different local variables in the callback's.
Paul
In LUA you can use a function that returns a callback function.
Instead of the following code:
Code: Select all
function callback(position, direction) -- common callback function
..
end
switch_id = switch_add( "off.png", "on.png", 100, 100, 100, 100, callback )
..
Code: Select all
function new_callback() -- produces separate callback function on each call
return function(position, direction)
..
end
end
switch_id = switch_add( "off.png", "on.png", 100, 100, 100, 100, new_callback() )
..
With some advanced LUA features called 'closures' you can even have different local variables in the callback's.
Paul
-
- Posts: 8
- Joined: Thu Nov 17, 2022 4:00 pm
Re: Switch Context
Hi Paul
How clever. I will give it a go, thank you for the quick response.
Colin
How clever. I will give it a go, thank you for the quick response.
Colin
-
- Posts: 8
- Joined: Thu Nov 17, 2022 4:00 pm
Re: Switch Context
Tried this and it worked perfectly. Read the programmers manual back-to-back over the weekend, what a clever programming language it is.