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.
()import(scriptName: s)
script node within a script_rep in World Editor.Shared scripts are defined in World Editor as part of the AI instance's primitive tree:
script_rep node (script repository folder)script child nodesscript 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.
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:
my_function() or through eventsThis 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.
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
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.
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.
"unknown library <name>".import is called. Calling import is fast — it just executes pre-compiled bytecode.script_rep/script primitive nodesSource: 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)