Conditional Triggers

This post was created in response to a question on the e:cue e:script forum.

The trigger engine in the programmer is a great thing, but one of the shortcomings is that other than setting a date range, there is no way to dynamically enable or disable a trigger in response to changing conditions. I’m going to show you a way to use a little e:script magic to do just that.

Rather than having a trigger perform an action directly, we’ll have the trigger call an e:script macro, passing a trigger ID number as a parameter. The script will check whether the trigger is enabled currently, and run the action if it is enabled or ignore it if not. In order to keep track of multiple triggers, we’ll use a global array that allows us to track an arbitrary number of and also include a script that you can call to enable the triggers, and one to disable them.

So, on with the code:

// init.cpp
// Set up the variables

// Only create the globals if they aren't defined
// already
if (undefined(_init)){
     int _init;
     int _triggercount =5;
     int _enabled[_triggercount];

     // Default all triggers to enabled state
     for (int i=0; i<_triggercount; i++)
     {
         _enabled[i]=1;
     }
 }

This is the first script you’ll need (name it init), which will create the global variables that holds the status of each trigger as well as one more to that determines how many triggers you want to use. You’ll want to change the value of _triggercount to the number of triggers you plan on tracking. This script should be run using the “On Initialization” trigger so that the variables are declared as soon as the programmer is ready to go. When doing your initial programming, you’ll want to run this once or the rest of the scripts won’t work properly.

// enabletrigger.cpp
// getarg(0) is the trigger to enable
// if -1 is passed to getarg(0), enable all triggers

int nTrigger = getarg(0);

if (nTrigger==-1)
{
	for (int i=0; i<_triggercount; i++){
		_enabled(i)==1;
	}
	printf("All triggers enabled\n");

}
else{
	_enabled[nTrigger]=1;
	printf("Trigger %d enabled\n", nTrigger);
}

The second script (name it enabletrigger) will allow you to enable any trigger number by passing it as the getarg(0) parameter. By passing a -1, enable all triggers that you are using. If you are using a call macro action you’ll want to use the following format for your macro name: “enabletrigger, x” where x is the value of the trigger you want to use. If you’re enabling it from e:script, you’ll use the following command:

Call("enabletrigger", x, 0, 0);

again, with x being the number of the trigger you want to enable.

// disabletrigger.cpp
// getarg(0) is the trigger to enable
// if -1 is passed to getarg(0), disable all triggers

int nTrigger = getarg(0);

if (nTrigger==-1)
{
	for (int i=0; i<_triggercount; i++){
		_enabled(i)==0;
	}
	printf("All triggers disabled\n");

}
else{
	_enabled[nTrigger]=0;
	printf("Trigger %d disabled\n", nTrigger);
}

The disabletrigger macro is essentially the same as the enabletrigger macro, but it disables one or all of the triggers by passing -1 or the trigger numbers to the getarg(0) parameter.

// runtrigger.cpp
// Run any trigger requested if enabled
// getarg(0) is the trigger to run

int nTrigger = getarg(0);

// only run the requested command if the trigger
// is enabled
if (_enabled[nTrigger]==1){
	printf("Running trigger %d\n", nTrigger);

	switch (nTrigger)
	{
	case 0:
		GotoCue(QL(1),Q(2),0);
		break;
	case 1:
		GotoCue(QL(2),Q(1),0);
		break;
	case 2:
		ReleaseAll();
		break;
               // Add more cases for each trigger action you want to perform
	}
}

else{
	printf("Trigger %d is not enabled\n", nTrigger);
}

Finally, we have the runtrigger script, which actually receives the triggers and runs the action code if they are enabled. It gets called the same way as the previous two scripts using “runtrigger, x” from an Trigger action or the equivalent “call” command in e:script. It will test first if the trigger number is enabled. If it is, we go into a “switch” statement, which is essentially the same as if you did the following:

if (nTrigger == 1){
    ....
}
if (nTrigger == 2){
    ....
}
if (nTrigger == 3)
    ....
}
....

You’ll want to add a new case/break clause for each action you want to track and add the code that you want to run when that trigger is available in between case and break. Make sure to update the trigger count variable in the init script to match the number of triggers you are going to use. Also, note that the count is 0 based, so if you have 5 triggers, you will have triggers #0-4. You don’t have to use 0 if it is confusing to you, but you then need to add 1 to the number of triggers you’re using in the init script.

If you update the number of triggers in the init script after you’ve run that script the first time, you’ll need to hit the programmer reset button to reset all of the variables, as they cannot be redefined a second time once that script has already been run.

If it’s not already clear, once you have everything set up here, the number in the “case” statement is what defines your trigger number, so to disable the action code for case 3 in the runtrigger script, you’d use a Call Macro action with a name of “disabletrigger, 3” (or it’s equivalent call statement in e:script) in the action that you’d want to disable the trigger. To run trigger 3, you’d add the trigger to programmer’s trigger list and for the action use “runtrigger, 3”.

Please post in the comments if something in here doesn’t make sense. Good luck!

Add a Comment

Your email address will not be published. Required fields are marked *