5.2.7. C Integration: Context Variables

This tutorial demonstrates how to read, write, and enumerate daslang global variables from a C host using the daScriptC.h API.

After a program is compiled and simulated, its global variables live in the context. The C API provides five functions to access them:

Function

Purpose

das_context_find_variable(ctx, name)

Look up a global by name; returns an index

das_context_get_variable(ctx, idx)

Get a raw void* to the variable storage

das_context_get_total_variables(ctx)

Total number of globals

das_context_get_variable_name(ctx, i)

Name of the i-th global

das_context_get_variable_size(ctx, i)

Size (in bytes) of the i-th global

5.2.7.1. The daslang script

The companion script declares four scalar globals:

options gen2

var score : int = 42
var speed : float = 3.14
var player_name : string = "Hero"
var alive : bool = true

[export]
def print_globals() {
    print("  score       = {score}\n")
    print("  speed       = {speed}\n")
    print("  player_name = {player_name}\n")
    print("  alive       = {alive}\n")
}

5.2.7.2. Reading globals

After simulation, look up a global by name and cast the returned pointer to the appropriate C type:

int idx = das_context_find_variable(ctx, "score");
if (idx >= 0) {
    int * p = (int *)das_context_get_variable(ctx, idx);
    printf("score = %d\n", *p);
}

String globals are stored as char* internally:

int idx = das_context_find_variable(ctx, "player_name");
if (idx >= 0) {
    char ** p = (char **)das_context_get_variable(ctx, idx);
    printf("player_name = %s\n", *p);
}

bool is stored as a single byte (not int):

char * p = (char *)das_context_get_variable(ctx, idx_alive);
printf("alive = %s\n", *p ? "true" : "false");

5.2.7.3. Writing globals

The pointer returned by das_context_get_variable is read-write. Modifications are immediately visible to daslang functions:

int * p = (int *)das_context_get_variable(ctx, idx_score);
*p = 9999;    // next call to a daslang function sees score == 9999

5.2.7.4. Enumerating all globals

int total = das_context_get_total_variables(ctx);
for (int i = 0; i < total; i++) {
    const char * name = das_context_get_variable_name(ctx, i);
    int sz = das_context_get_variable_size(ctx, i);
    printf("  [%d] %s (size=%d)\n", i, name, sz);
}

5.2.7.5. Build & run

Build:

cmake --build build --config Release --target integration_c_07

Run:

bin/Release/integration_c_07

Expected output:

=== Reading globals from C ===
score = 42
speed = 3.14
player_name = Hero
alive = true

=== Writing globals from C ===
C set score = 9999
C set speed = 99.5

=== Calling daslang to verify ===
  score       = 9999
  speed       = 99.5
  player_name = Hero
  alive       = true

=== All global variables ===
  [0] score (size=4)
  [1] speed (size=4)
  [2] player_name (size=8)
  [3] alive (size=1)

See also

Full source: 07_context_variables.c, 07_context_variables.das

Previous tutorial: tutorial_integration_c_sandbox

Next tutorial: tutorial_integration_c_serialization

C++ equivalent: tutorial_integration_cpp_context_variables

daScriptC.h API header: include/daScript/daScriptC.h