Complex color change with scripts

<< Click to Display Table of Contents >>

Navigation:  Examples > Change the color of an object >

Complex color change with scripts

Previous pageReturn to chapter overviewNext page

Suppose you have many objects (motors) of the same type, each of which has 2 parameters (state and speed). You need to change the color of an object depending on its state and speed.

 

We already know that if the color changes depending on the values of several tags,  we need to use scripts; and if we use objects of the same type, in order to simplify the binding of duplicated objects to tags, we need to use indirect names based on user-defined properties.

 

Let's look at an example.

 

1. Create 2 tags (State and Speed) for each Motor object. For convenience, we’ll do this as a group, and then copy the group by the number of objects in the project:

 

 

complexwithscriptstags

 

2. Let’s create a graphical Motor object for our example:

clip0026

 

Let's open the Object properties window, create user-defined property - "Number" with the value "1", because We will bind this object to the State1 Speed1 tags:

 

clip0027

complexudp

 

3. Now we need to create a script for an object in the ST language with an execution type - OnDataChange:

 

stscriptforcomplexscript

 

 

Depending on tag's values for every Motor object use fill color:

 

State

Speed

Color

0

Any

RED

1

0...500

GREEN

1

500...1000

YELLOW

1

>1000

BLUE

 

Let's write our script:

 

scripttextexampleforcomplexexample

After you have recorded the script, be sure to launch it by clicking the button on the toolbar: runsimulation

 

4. Let's bind our script to the object: open the properties of the Motor object, then - Scripts/Collection and bind our script:

 

motorpropertieswindow

stcollection

 

Now we have a Motor with the Fill Color property configured in the script.

 

5. Now let’s duplicate the object as many times as needed for the project. Because we used a custom property, we don't need to customize the script for each Motor. We only need to duplicate the Motor and change the value of the user-defined property in the Property sheet::

 

complexduplicate

 

 

complexpropertysheet

 

6. Let's Run simulation to check the settings:

 

complexrunsimscript

 

You can download this project here.