Define the Timer Requirement
First determine whether the function must run every X seconds of simulation time. The evidence addresses simulation-time scheduling; it does not establish wall-clock timing behavior. Keep that distinction explicit when verifying the result, especially when changing simulation speed.
Configure the Periodic Event
| Method | Implementation | Use |
|---|---|---|
| Python script | Run a loop with delay() in the OnRun method, then call the function. |
Directly invoke script logic at each simulation-time interval. |
| Process Modeling | Create a process containing a loop, a delay, and an action such as sending a signal. | Trigger model behavior without adding a separate threading design. |
- Place the required action inside an
OnRunloop or a Process Modeling loop. - Insert
delay()for the required simulation-time interval. - Run the simulation at the intended speed and confirm that the action occurs once per interval.
- Avoid using Python
printor other output methods as the primary timing test because output can be relatively slow and distort the performance observation.
Separate Timer Cost from Rebuild Cost
A loop using delay() operated successfully at x100 simulation speed in the reported case. The actual slowdown came from component parameters that continually rebuilt the simulation, not from the periodic delay.
If performance falls after adding the timer, inspect component parameters for repeated rebuild activity before replacing the timer with threading. The evidence also identifies the Optimize Rebuild addon as a way to address careless rebuild configuration, but it does not define the addon's settings or guarantee a specific performance improvement.
Verify the Result
Confirm both timing and performance under the target simulation speed. Observe the function or signal directly, verify one activation per configured simulation-time interval, and then check whether component edits or parameter evaluations trigger continual rebuilds. Treat console-output latency separately from timer execution latency.
FAQ
How do I call a function every X seconds of simulation time?
Place the function in an OnRun loop and use delay() for the required interval. A Process Modeling loop with a delay and send-signal action is an alternative.
Does delay() slow down a simulation at x100 speed?
In the reported case, delay() worked at x100 speed. Continual simulation rebuilds caused by component parameters were the actual performance problem.
Why should I avoid Python print when testing a simulation timer?
Python print and other output methods can be relatively slow, so their overhead can make the timer appear more expensive than it is. Verify the event through its function result or signal instead.