import - ryzom/ryzomcore GitHub Wiki


title: Import description: Execute a shared script from the script repository published: true date: 2026-03-14T00:00:00.000Z tags: editor: markdown dateCreated: 2023-03-16T22:25:11.750Z

import

The import native AI script function executes a named script from the script repository in the current group's context. This is the mechanism for reusing common script code across multiple NPC groups without duplicating it in each event handler.

Syntax

()import(scriptName: s)

Arguments

  • scriptName (string): The name of the script to execute, as defined in a script node within a script_rep in World Editor.

Where scripts come from

Shared scripts are defined in World Editor as part of the AI instance's primitive tree:

  1. At the top level of an AI instance, create a script_rep node (script repository folder)
  2. Inside it, create one or more script child nodes
  3. Each script node has a name and a code block (the AI script source)

When the AI service loads the primitives, each script node is compiled to bytecode and registered in a global library (AIVM::CLibrary) keyed by its name. The import function looks up the pre-compiled bytecode by name and runs it.

What "import" actually does

import works like an inline include — the library script's bytecode is interpreted immediately in the current group's context, as if the code were pasted at the call site. This means:

  • Top-level statements execute immediately (set variables, call native functions, etc.)
  • Function definitions in the script are registered as callbacks on the current group, available for later use via my_function() or through events

This is similar to how a Python import both defines functions and runs top-level code. A library script can do purely one or the other, or both.

Common patterns

Pattern 1: Utility code that runs immediately

// Library "setup_guard" — runs top-level statements
()setAggro(50, 10);
()setCanAggro(1);
()setAttackable(1);
// In a start_of_state handler:
()import("setup_guard");  // executes the setup immediately

Pattern 2: Function library

// Library "boss_utils" — defines functions for later use
announce_phase()
{
    ()phraseBegin();
    ()phrasePushValue("integer", phase);
    ()phraseEndNpcMsg(0, "shout", "BOSS_PHASE");
}
// In a start_of_state handler:
()import("boss_utils");   // registers announce_phase() on this group
phase = 1;
announce_phase();          // now callable

Scope

The imported code runs in the current group's context — it can read and write the calling group's variables, and caller/parent resolve as if the code were written inline in the event handler. Functions defined by the import are registered on the calling group's state instance, not globally.

Scripts in the library are available to all groups within the same AI instance. A script defined in one continent's primitives is not available to groups in a different continent's AI instance.

Example

Define a shared script in World Editor:

script_rep (name: "common_scripts")
  └── script (name: "announce_combat")
        code:
          ($eid)getCurrentPlayerEid();
          ()phraseBegin();
          ()phrasePushString("player", $eid);
          ()phraseEndNpcMsg(0, "shout", "COMBAT_ANNOUNCE");

Then use it from any event handler:

// In a group_under_attack event handler:
()import("announce_combat");

This executes the announce_combat script as if its code were written directly in the event handler.

Notes

  • If the script name is not found in the library, a warning is logged: "unknown library <name>".
  • The script is compiled once at primitive load time, not each time import is called. Calling import is fast — it just executes pre-compiled bytecode.
  • Unlike user-defined functions (which are scoped to a group's state machine), library scripts are globally available within the AI instance.
  • Changes to the script code in World Editor require reloading the primitives in the AI service to take effect.

See also

  • AI Script - Describes user-defined functions and the script_rep/script primitive nodes
  • loadFile - Load and execute script from a file at runtime (not for production use)

Source: ryzom/server/src/ai_service/nf_state_instance.cpp (import_s_), ryzom/server/src/ai_service/ais_actions_global.cpp (DEFINE_ACTION ContextGlobal SCRIPT), ryzom/server/src/ai_service/script_vm.cpp (CLibrary)

⚠️ **GitHub.com Fallback** ⚠️