Post by southerlybuster on Aug 1, 2020 20:08:42 GMT -5
How do we go about to create static variables within our User Functions? Simply put to create variables within a function that get remembered across multiple calls of that function.
Here is a simple use case:
User function's first argument is X
Call user function from Ableton with first argument set to "Get"
It looks to see what the selected track and clip is, remembers what that track and clip is, say in variables rem_track_name and rem_clip_name.
Call user function again from Ableton with first argument set to "Show"
(By now the selected track and clip has changed)
Recall the previously remembered track and clip names to do some thing with them, so accessing previously saved rem_track_name and rem_clip_name.
In a standard python program (not in ClyphX) I might achieve this by defining global variables rem_track_name and rem_clip_name out side the definition of the function, but from my trials in ClyphX Pro, it does not accept such definitions.
I see ClyphX Pro has %variables% but these once defined should not be changed and I suspect are for the scripting side not for the Python programming side.
I tried adding objects to the self (like self.apples="eatone" ), no luck either.
Post by southerlybuster on Aug 7, 2020 22:01:04 GMT -5
I have answered my own question, hopefully will help others trying to do the same. To define a static variable in Python with ClyphX Pro that is accessible and able to be modified from more than one user function the following must happen:
1) Define the variable at the top level, like right after the first import statement
# Import UserActionsBase to extend it. from ClyphX_Pro.clyphx_pro.UserActionsBase import UserActionsBase
2) Define the same variable in 1) within the user function as global, otherwise it is a local variable within the function of the same name as the global variable (oops) :
def my_user_function(self, action_def,args): global my_global_var
3) Use the global variable within the function, here just append some message after it and then show the result in the Log.txt file:
my_global_var=my_global_var + ' and oranges' self.canonical_parent.log_message(my_global_var)