How to assign differnt action to a controller based on screen
Hi, I would like to understand if there is a way to assign a set of different actions to a controller based on the screen it is on. In case it is not possible to do this automatically, could you suggest me a method that does not involve creating new controllers or dedicated layers?
Thank you
0
Comments
Hi Cristian,
You cannot change the nature of actions after they are created (eg. swapping a Lua callback action for a datachange action). However, you can change parameters of an action by binding them to a variable. I can think of a couple ways to accomplish what you're trying to do.
1a) Create a screen level variable and bind the action's parameter(s) to it.
Just because an action is triggered by a an event on a given control, it does not mean you are restricted to using variables on that control. Right-click on the screen in the Application Model view -> add -> Variable. Name it something appropriate to all screens eg. 'main_button_action' and give it a parameter appropriate to this screen. Let's say on this screen we want our button to be a 'next' button. Give it a value of 'next' (or anything that will identify the action to you). Now, find the control in question in the Application Model view and select it. In the Actions view (on the bottom middle by default) select your action. Now in the properties pane, you should see the properties for your action. Click the 'Add New Argument' button. Name your argument something like do_action and for the parameter click the variable binding button, then navigate to the screen level variable you created. This will pass nil for screens for which this variable does not exist. The way you read this variable is that it gets inserted into the mapargs in your Lua callback. In this example, we named it do_action, so it will be mapargs.do_action.
1b) Alternatively, you can bind the name of the Lua callback itself to a variable. So instead of passing the desired action as a parameter, you can make your screen level variable contain the function name, eg. "cb_intro_screen_button_pressed()", "cb_play_screen_button_pressed()".
2) Create variables on your control and set them appropriately on screen show (pre).
Sometimes you have some overall setup to do for a screen. Usually you would create a Lua callback bound to a screenshow.pre action for this, which you may have done for other reasons. If you already have something like this, you could do something like 1a or 1b, but put the variable on the control instead of the screen.. so you don't have to create it for every screen.
A general tip.. If the action parameters are the same for most screens (and you have a lot of screens in your app that have this button) but only 1 or 2 screens have the button doing the 'alternate' action. Then you may consider setting the variable on screenshow of the different screens and setting it back to default on screenhide of those screens. That way screens using the 'normal' behaviour won't need any changes for it to work.
3) Last but not least. Just check the screen name in the callback.
If your action is a Lua callback, then the screen name is passed along to you in mapargs. It is mapargs.context_screen. Using that screen name as a key in a callback table or just a couple if-else-ifs might be all you need to accomplish your goal.
Feel free to add additional information about your use case if these are not helpful.
Good luck and happy coding!
Corey
Please sign in to leave a comment.