<< Click to Display Table of Contents >> Navigation: Advanced Features > Scripting > Advanced Scripting > Dynamic Sripting |
In this context, Dynamic Scripting refers to features that allow dynamically accessing objects, i.e. without necessarily specifying them in code explicitly. The concrete instances will be determined during execution and may well depend on run-time values.
Note: While Loops (in combination with the DynamicValue member, see above) may also be considered a way of “dynamic scripting”, they are conceptually not a part of the features described here and will be covered in their own chapter (see below).
In this context, there are two features for dynamic scripting support: Indexers and Inline Placeholders. To understand the different possibilities and limitations, keep the following “execution pipeline” in mind:
-Load all script lines
-Create local Script Parameter Variables (if required for custom Scripts).
-Evaluate Indexers
-Iterate over all evaluated script lines and for each line:
•Evaluate Inline Placeholders
•Process line
Indexers were primarily implemented to allow dynamic access to several objects:
Sequence1.ClipContainer[1-3].Opacity = 0.2
The “Indexer” is the [1-3] in the example above. When evaluating the script code, during “pre-processing”, for all upcoming execution steps this one line will be replaced with the following lines:
Sequence1.ClipContainer1.Opacity = 0.2
Sequence1.ClipContainer2.Opacity = 0.2
Sequence1.ClipContainer3.Opacity = 0.2
Note: This essentially also leads to code repetition, like in Loops, but the technical approach is very different, see Loops below.
Since Indexers are evaluated before the actual script is processed, the Indexer expressions can contain Global Variables and Script Parameter Variables – but cannot contain Local Variables, because these will not have been evaluated, yet.
This example demonstrates the use of Script Parameter Values as Indexers:
Assumptions: Project contains Sequence1 with several ClipContainers
Script1, without parameters:
Set min = 1
Set max = 3
HideContainers min,max
Script, named “HideContainers”, with parameters min, max:
Sequence1.ClipContainer[min-max].Opacity = 0
Sequence1.ClipContainer[min-max].Transform.Position 0,0,0
Since Indexers do not rely on run-time information for their evaluation, the Script Editor is able to offer rudimentary code completion support for members after the Indexer. This may lead to unexpected (and, frankly, wrong) suggestions, because for editing the Script Editor supplies the members of the first item that could be a possible match for the Indexer expression, i.e. in the example above the first Clip Container of the Sequence.
Inline Placeholders were implemented to allow easy, dynamic access to objects, methods or properties as demonstrated in this example:
Assumptions: Project contains Sequence1 with ClipContainer1
Set p1 = x
Set p2 = Position
Set p3 = Position.X
Log Sequence1.ClipContainer1.Transform.Position.{p1}
Log Sequence1.ClipContainer1.Transform.{p2}.X
Log Sequence1.ClipContainer1.Transform.{p3}
Since Inline Placeholders are evaluated “just in time” before executing each line, they can use almost any kind of input, not just local or global variables, as the contrived and not recommended approach in this example demonstrates:
Assumptions: Project contains Sequence1 with ClipContainer1 and Variable1
Variable1 = Clip
Set i = 1
Set x = Container
Set y = 1
Log Sequence{Eval String(i, '.', Variable1, x, y)}.Opacity
In this example, the highlighted expression within the inline placeholder uses the `Eval` method which is evaluated as "1.ClipContainer1" and inserted into "log Sequence{}.Opacity"
resulting in "log Sequence1.ClipContainer1.Opacity", which is a valid statement.