Skip to content
bizzehdee edited this page Jun 23, 2026 · 2 revisions

Host Objects

engine.SetGlobal lets you expose a C# object to scripts without writing a custom provider. Public members opt in with attributes.

Attributes

Attribute Target Description
[ScriptVisible] Property or Method Makes the member readable from script
[ScriptWritable] Property Also allows script-side assignment

Both attributes are in the DScript namespace.

Example

using DScript;

public class AppConfig
{
    [ScriptVisible]
    public string Environment { get; set; } = "production";

    [ScriptVisible]
    [ScriptWritable]
    public int MaxConnections { get; set; } = 100;

    [ScriptVisible]
    public string GetVersion() => "2.1.0";

    [ScriptVisible]
    public bool IsFeatureEnabled(string name)
        => name == "dark-mode";

    // Not visible to scripts — no attribute
    public string InternalSecret { get; set; } = "hidden";
}
var config = new AppConfig();
engine.SetGlobal("config", config);
// In script
console.log(config.Environment);          // "production"
console.log(config.GetVersion());         // "2.1.0"
console.log(config.IsFeatureEnabled("dark-mode")); // true

config.MaxConnections = 50;               // allowed — [ScriptWritable]
// config.Environment = "staging";        // would throw — not [ScriptWritable]
// config.InternalSecret;                 // undefined — not [ScriptVisible]

Supported return types

Methods and properties may return:

C# type Script value
int, long, byte, short Integer
double, float Float
string String
bool Boolean (1/0)
ScriptVar Passed through directly
void Undefined

Method parameters

Script calls pass arguments positionally. The C# method receives them via normal parameter binding. If fewer arguments are passed than declared parameters, missing values are default(T).

[ScriptVisible]
public int Add(int a, int b) => a + b;
config.Add(3, 4);  // 7

Notes

  • SetGlobal uses reflection at call time. For performance-critical paths, consider writing a typed native function with AddNative instead.
  • [ScriptWritable] without [ScriptVisible] has no effect — the property is not exposed at all.
  • Static methods are not currently supported via SetGlobal; use AddNative for those.

Clone this wiki locally