<< Click to Display Table of Contents >> Manual > Server Creation Guide > Action Edition > Action Reference > Script Ver2 (SC2) > Function Description |
About object hierarchies
Script Ver2 actions execute scripts written in script files, and the content written in one script file becomes the "root object."
Objects have a hierarchical structure, with the root object at the first level. The root object is an object that has properties and methods that affect the whole. In addition to having arithmetic objects (Math objects) as child objects, the root object can also use objects generated by the "new" operator as child objects of the root. If you write your own user-defined function in the root object, that function can only be called from within the same script file. For information about objects that can be generated by the new operator, see the description in "Methods that can be used with this action".
Events that call this action
Scripts for Script Ver2 actions are executed by calling them from FA-Server events, just like other actions.
By calling this action from each event such as Periodic event/Scheduled event/Tag event, you can set the cycle and timing for executing the script.
The processing written in the script is executed each time an action is called from an event.
For example, if you want to run a script that will repeat processing at regular intervals while FA-Server is running online, or if you want to run a so-called resident process, you can call it from Periodic event.
On the other hand, if you want to perform processing only at a specific time rather than repeatedly, you can call it from Scheduled event or Tag event.
When registering an event, select the event that best suits your purpose from among the periodic, fixed time, and tag events, and register this action. For example, if you want to run a script periodically, select "Periodic event". Alternatively, if you want to run a batch-like process at a fixed time, select "Scheduled event", and if you want to run a process triggered by a tag value, select Tag event. |
Script execution and event occurrence timing
When a Script Ver.2 action is executed from a FA-Server event, events such as OnTimer/OnHeartBeat/OnTagValueChanged/OnExpressionValueChanged will occur within the script.
An important concept here is the timing of script event occurrence. Each script event is evaluated when the action is executed, and the event that meets the occurrence condition will occur. For example, OnTagValueChanged/OnExpressionValueChanged etc. will not occur immediately when the condition such as the tag value or condition expression is met, but will occur when the action is executed (i.e. when it is called from Periodic event etc.).
Similarly, the OnHeartBeat event is always called every time an action is executed by the event. For example, if you set an action to be called with Periodic event every second, the OnHeartBeat event will be called every second. In addition, the occurrence interval of the OnTimer event timer processing (executing processing at regular intervals) also depends on the call cycle from the FA-Server event. For example, if you set this action to be called with Periodic event every 10 seconds, even if you try to generate a timer with a 5-second cycle by writing SetTimer(0, 5000, -1) in the script, the OnTimer event will occur every 10 seconds. Therefore, when implementing timer processing for the OnTimer event, you need to call this action from Periodic event with a cycle faster than the timer.
If you want to implement a process in a script that is executed periodically, you can use the OnHeartBeat event or the OnTimer event with the SetTimer method. We recommend using the OnTimer event with the SetTimer method to implement a periodic process unless there is a special reason. The OnHeartBeat event is always called every time an action is executed by an event, so the execution period depends on the timing of calling this action from the FA-Server event. On the other hand, using SetTimer has the advantage that multiple timers with different periods can be set at the same time. The idea is to use the OnTimer event to implement a periodic process, and use the OnHeartBeat event if you want to execute a script at a time specified by FA-Server's Scheduled event, etc., so choose according to your purpose. |
The OnTimer event is executed if the condition (the period set in OnTimer has elapsed) is met when the Script Ver.2 action is called by an event.
for example, - Event period: 10 seconds ・OnTimer period: 11 seconds If you specify the above, OnTimer will not be processed 11 seconds after the first execution, but will be processed when the action is called again by the event as shown below.
1: First execution (0 seconds) → OnTimer starts counting. 2: First execution of the event (10 seconds have passed) → The timer period is 11 seconds, so the condition is not met and no processing is performed yet. 3: The OnTimer cycle has elapsed (11 seconds have elapsed) --> The Script Ver2 action has not been called, so it has not yet been executed. 4: Second execution due to event (20 seconds have passed) → Since the set period of OnTimer has been exceeded, the condition is met and the process is executed.
Therefore, if you want to process OnTimer for each event, the OnTimer period must be set to a shorter period than the event period. |