Why the Script Fails After Loading
Visual Components auto-compiles scripts while loading the application content. A script can be parsed and compiled before every referenced layout or component object exists. References resolved during that interval can throw exceptions, leaving the script unusable until the component finishes loading or the script is manually recompiled.
This behavior is associated with scripts that import modules beyond vcScript. The reported case used vcHelpers.Robot2, particularly for variable declarations that depend on objects created during component loading.
Select the Correct Event
| Method | Behavior | Decision |
|---|---|---|
OnFinalize |
Runs initialization after the component has loaded and finalized. | Use it for initialization that requires finalized component or layout objects, including the reported vcHelpers.Robot2 case. |
OnRun |
Requires starting the simulation and executes on every run. | Do not use it for load-only initialization when repeated execution is unwanted. |
| Manual script execution or recompilation | Can make the script work after loading has completed. | Use only as a diagnostic confirmation; it does not automate initialization. |
Configure and Verify the Workaround
- Move the initialization that depends on loaded objects into the script's
OnFinalizeevent handler. - Keep the same initialization out of
OnRunif it must not repeat whenever the simulation starts. - Reload the component or simulation and verify that the initialization completes without opening, recompiling, or manually executing the script.
- If the script still fails, inspect imports beyond
vcScriptand identify declarations that resolve component or layout objects before finalization. Confirm the timing diagnosis by checking whether manual recompilation after loading restores operation.
OnFinalize is a timing workaround: it delays object-dependent initialization until component finalization. The evidence does not identify affected product versions or guarantee that every load-time script failure has this cause, so treat successful execution after manual recompilation as supporting evidence rather than proof.
FAQ
How do I execute a Visual Components script after loading?
Place object-dependent initialization in the OnFinalize event handler. This allows the component to finish loading before the script resolves its references.
Why does my Visual Components script work only after recompiling it?
Auto-compilation can occur before referenced layout or component objects exist. If recompiling after loading restores operation, move the affected initialization or variable declarations to OnFinalize.
Should I use OnRun or OnFinalize for vcHelpers.Robot2?
Use OnFinalize for vcHelpers.Robot2 initialization that depends on finalized objects. Use OnRun only when execution should occur each time the simulation is explicitly started.