maartentamboer / X-Touch-Mini-FS2020

Control FS2020 with a Behringer X-Touch Mini
https://dev-tty.nl/X-Touch-Mini-FS2020/
MIT License
59 stars 19 forks source link

local/global variable set/get feature #14

Closed jeremy-collette closed 3 years ago

jeremy-collette commented 3 years ago

Hello, is setting simvar values supported in event_press conditions?

I'm trying to create a COM swap button as follows:

    {
      "index": 6,      
      "event_press": {
        "type": "condition",
        "event": [ "{% if data.get_simvar_value('SWAP_COM') == 1.0 %}",
                  "{{ data.trigger_event('COM_STBY_RADIO_SWAP', 1.0) }}",
                  "{{ data.set_simvar_value('SWAP_COM', 0.0) }}",                  
                "{% else %}",
                  "{{ data.trigger_event('COM2_RADIO_SWAP', 1.0) }}",
                  "{{ data.set_simvar_value('SWAP_COM', 1.0) }}",                  
                "{% endif %}" ],
        "description": "Swap COM"
      }   

But set_simvar_value does not seem to work. Any ideas?

maartentamboer commented 3 years ago

set and get _simvar_value value will only work on existing simvars which can be found here: https://github.com/odwdinc/Python-SimConnect/blob/master/SimConnect/RequestList.py

I'm guessing you want to use SWAP_COM as a temp variable?

jeremy-collette commented 3 years ago

Ah I see... Yes that's correct. Is there any way to support defining new simvars? It looks like you are already be defining vars in _deff_test?

If not, is there any way to store state in the key bindings?

maartentamboer commented 3 years ago

You could try to just create a new variable in the data object. The data object is of the type ConditionalRunner https://github.com/maartentamboer/X-Touch-Mini-FS2020/blob/main/conditionalrunner.py#L9 Since Python is fairly flexible regarding the handling of objects you could probably add a variable to that conditionalrunner instance. So perhaps something like data.my_var_name = 1.0 could work

Otherwise I will implement a new feature which will enable a global variable storage

jeremy-collette commented 3 years ago

I understand. I will try setting a value on the data object.

Yes, something like "set_localvar_value" would be a nice feature :-)

jeremy-collette commented 3 years ago

I tried setting a variable directly on the data member but it did not work (Python error). Looks like a local/global variable set/get feature might be required for more complex behaviors.

maartentamboer commented 3 years ago

Sounds like a good feature, I'll hopefully have some time next weekend to implement this

jeremy-collette commented 3 years ago

@maartentamboer If you're looking for contributors, I'd be happy to take a stab at this. Seems like a simple method on the ConditionalRunner should handle it. Otherwise good luck & thanks!

maartentamboer commented 3 years ago

Contributors are always welcome 👍 feel free to make a PR anytime PR https://github.com/maartentamboer/X-Touch-Mini-FS2020/pull/23 has support for this feature. I will close this issue once a new release is made.

I've updated your proposal from the opening post so it works in this new feature.

    {
      "index": 16,
      "event_press": {
        "type": "condition",
        "event": [ "{% if data.get_global_variable('SWAP_COM') == true %}",
                     "{{ data.print('COM_STBY_RADIO_SWAP') }}",
                     "{{ data.trigger_event('COM_STBY_RADIO_SWAP', 1.0) }}",
                     "{{ data.set_global_variable('SWAP_COM', false) }}",
                   "{% else %}",
                     "{{ data.print('COM2_RADIO_SWAP') }}",
                     "{{ data.trigger_event('COM2_RADIO_SWAP', 1.0) }}",
                     "{{ data.set_global_variable('SWAP_COM', true) }}",
                   "{% endif %}" ],
        "description": "Swap COM"
      }

data.get_global_variable(key): Get a global variable value based on a key
data.set_global_variable(key, value): Set a global variable to a value
data.print(data): Print something in the console

maartentamboer commented 3 years ago

Fixed in the latest release https://github.com/maartentamboer/X-Touch-Mini-FS2020/releases/tag/v1.1.0