Switch Context

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
colinparker1967
Posts: 8
Joined: Thu Nov 17, 2022 4:00 pm

Switch Context

#1 Post by colinparker1967 »

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

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Switch Context

#2 Post by Tetrachromat »

Hi Colin

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 )
..
Use this kind of code:

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() )
..
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

colinparker1967
Posts: 8
Joined: Thu Nov 17, 2022 4:00 pm

Re: Switch Context

#3 Post by colinparker1967 »

Hi Paul

How clever. I will give it a go, thank you for the quick response.

Colin

colinparker1967
Posts: 8
Joined: Thu Nov 17, 2022 4:00 pm

Re: Switch Context

#4 Post by colinparker1967 »

Tried this and it worked perfectly. Read the programmers manual back-to-back over the weekend, what a clever programming language it is.

Post Reply