DCode

Covers Engine 2.0.0

Modules and Native Functions

Share script helpers and call functions supplied by engine plugins.

Import helper files to reuse DCode or Lua functions. Call a plugin’s native DCode function when work needs the device access or C++ implementation supplied by that plugin.

This guide uses the Lua backend. Here, a native function means C++ code supplied by a plugin and called through Lua. Native C++ Execution instead compiles the DCode file itself; that backend does not support imports or callDcodeFunction.

Share a helper

Create scripts/helpers/filter.dcode in the active project:

local previous = 0

gain = 0.2

function low_pass(value):
    previous = previous + ((value - previous) * gain)
    return previous

function reset(value):
    previous = value

Bare functions and assignments are exported on the imported module table. previous stays private because it is local; gain, low_pass, and reset are public.

Create scripts/filter_demo.dcode beside the helpers directory:

import "./helpers/filter"

task_periodic docs_demo_filter:
    start:
        filter.reset(|docs_demo_raw|)

    task elapsed_seconds:
        |docs_demo_filtered| = filter.low_pass(|docs_demo_raw|)

Relative imports use the importing script’s folder; dotted imports map to folders inside scripts. Both .dcode and .lua helpers are supported. The filter’s private value is shared by callers of that imported helper; use per-task state when several tasks need independent filter histories.

Call a native function

An engine plugin can register functions with SDK_API::registerDCodeFunction. The engine qualifies the function with the plugin ID. With the public example plugin installed and loaded, an executable body can call:

local result = example_device_plugin.test_value({ base = 40, offset = 2 })
|docs_demo_result| = result

The example function returns 42 by adding base and offset. Native functions take one JSON-like payload and return one JSON-like result. A single-output function can return a scalar; multiple outputs can use named object fields. Follow that plugin’s contract and use the editor’s registered-function completion to find available names.

The call runs in the calling DCode execution path. A blocking native function also blocks that path, so avoid slow I/O inside channel calculations and event reactions. Use an engine module or loop to acquire data and publish channel values when the work needs independent scheduling.