BlockWerk Documentation
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="7 8 3 12 7 16"/><polyline points="17 8 21 12 17 16"/><line x1="14" y1="5" x2="10" y2="19"/></svg>

Script

Advanced

Execute custom JavaScript or Python logic inside the simulation loop. Define your own input and output ports — they become live variables in your script.

🚦 When to use: for nonlinear control laws, observers, fuzzy logic, swing-up controllers, custom math. Stick to standard blocks (Sum, Integrator, Gain, etc.) for plant models and linear dynamics — they are faster and easier to debug visually.

Simulate in BlockWerk

Interactive sandbox directly in your browser — zero installation, WASM-powered.

Simulate in BlockWerk →

Mathematical Model

y(t)=fscript(u(t),t)y(t) = f_{\text{script}}(u(t), t)

Parameters

Parameters Label Type Default Description
language Language select javascript Scripting language. Switching language replaces the default starter code if the editor is untouched.
script Script code // Available variables: t, dt, propar, and your custom input names // Assign values to your custom output names out = input * 2; Script executed every simulation step. Use the dedicated Script editor (double-click the block) for syntax highlighting and live console output.
customInputs Input Ports text [{"id":"input","label":"Input","dataType":"number"}] JSON array of input ports. Each entry: { id, label, dataType }. Edit via the Interface tab in the script editor.
customOutputs Output Ports text [{"id":"out","label":"Output","dataType":"number"}] JSON array of output ports. Each entry: { id, label, dataType }. Edit via the Interface tab in the script editor.
scriptUrl External Script URL text Optional URL. Click 'Pull' in the editor to fetch and replace the local script — never auto-fetched.

Usage Examples

  • Zie de ingebouwde voorbeelden in de handleiding voor een demonstratie van dit blok.

Remarks & Best Practices

  • JavaScript vs Python: JS runs natively (fastest); Python runs via Pyodide (slower, ~2–4 s cold start) — use JS for high-frequency loops
  • Hot-reload: While the simulation is running, saving the script hot-reloads it in the worker — no restart needed
  • Port collisions: Port names must not clash with runtime variables (t, dt, propar) or language keywords
  • Hardware access: Use propar.write() for real-time Bronkhorst MFC control; writes are queued to the main thread
  • Error handling: Runtime errors are rate-limited in the console; the block face turns red and displays the latest error message

Related Components