Jump to content
C4 Forums | Control4

Use boolean variables to execute common code


cteasley

Recommended Posts

During, I believe, the Tech I training, the notion of using variables to trigger the execution of code that could be initiated from multiple locations within the project was discussed.

In some senses, it does complicate the programming but the long term benefits are well worth it. Specifically, it reduces the number of lines of code in the “main” script, and more importantly, it makes it easier to maintain the project and thus, the project is more stable over time. I believe it is what the object-oriented folks call encapsulation.

For example, let’s say your project is configured so that there is a custom button and a keypad button to turn off the theater. Instead of placing the same code in the custom button and in the keypad button script:

1. Define a Boolean variable (e.g., TheaterOff)

2. Place the common code in the When the variable Variables->TheaterOff changes script

3. Perform Toggle Variables->TheaterOff in the “main” script to initiate the common code

Now when it is time to modify the turning off of the Theater, you only have to do this in one location, and don’t have to try to remember all the places you or one of your associates put the turn off Theater logic.

You can go so far as to adopt a convention that states all projects will have a RoomOn, RoomOff, RoomLightsOn, RoomLightsOff, RoomAVOn, and RoomAVOff variable for every room in the project. The RoomLightsOn and RoomLightsOff variables might be considered a little redundant of a light scene but it does provide a level of consistancy.

Thoughts or other recommendations?

Link to comment
Share on other sites


I understand the concept but to me its actually just easier to program on the button itself because otherwise I have to look around a bit more in composer HE to see what a button does (ie look at the button to see what variable is toggled, and look at the variable to see what happens when it changes). I usually just program on the button itself so that I can quickly look at the button on HE and see what I made it do.

For example for basically every room with a TV I have a multitap button to turn on and off lights & AV.

If the lights are off one tap turns on the room lights

If the lights are on

1 tap - turns off main lights

2 tap - turns off all lights

3 taps - turns off all lights (sometimes for the whole floor) and turns the room AV off.

If I did that with variables then Id have to have variables for each room and that would add 8 more variables to the project. If I want to duplicate it somewhere I copy and paste the code to the new button. That does mean if I want to change something I have to change it in multiple locations, but you really just change it once then copy and paste it at the other locations. Also, for the vast majority of rooms I cant see a need to have more than one button do the same function. For rooms with 2 entrances/exits it makes sense, or if youre trying to make a custom remote button and a wall button do the same thing.

Everyone has their preference, and programming wise it may be easier with variables but I find it much easier to keep track of things if I dont have to follow the trail of breadcrumbs.

Link to comment
Share on other sites

I use variables like you are suggesting. I agree that doing them at the variable level makes things much cleaner. I guess the key is to make sure that whatever you do you do it the same way, all the time.

Link to comment
Share on other sites

Yeah, I definitely understand the temptation to put all the code in the “main” script especially if you are the only one managing the code and therefore, very familiar with the code/project.

The use of “trigger” variables really comes into play when there are multiple places (e.g., keypad button, scheduler, etc) that execute the exact same code, and you are managing multiple projects. If you don’t watch it, you will overlook something during a project modification and thus, end up with a system that behaves inconsistently.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.