Skip to content

context

Jan Boon edited this page Mar 14, 2026 · 5 revisions

title: Context description: Get a group context reference for use as a function parameter published: true date: 2026-03-14T00:00:00.000Z tags: editor: markdown dateCreated: 2023-03-16T22:21:42.809Z

context

The context native AI script function returns a script context reference (@ variable) for a group. This is needed when you must pass a group reference as a parameter to a native function that accepts a c (context) type argument.

In AI script, you can already call functions on named groups directly using the dot syntax (e.g. other_group.my_function()). However, native functions that accept a group as a parameter — like queryEgs, giveReward, newNpcChildGroup, talkTo, etc. — require a @ context variable. The context() function bridges this gap by converting a group reference into a storable, passable @ variable.

Syntax

(context: c)context()

Return Value

  • context (context): A reference to the group's state instance (CStateInstance), stored as a @ prefixed variable.

Named Groups vs Context Variables

Feature Named group (e.g. my_group) Context variable (e.g. @grp)
Call script functions my_group.my_function(); @grp.my_function();
Pass as native function parameter Not possible ()queryEgs("Hp", $eid, @grp, 4, "msg");
Store in a variable Not possible (@grp)my_group.context();
Reference current group Use caller or parent keywords (@self)context();

The reason bare group names can't be passed as parameters is a parser limitation: in the AI script grammar, a bare name like my_group in a parameter position is parsed as a float variable read (TOKEN_NAMEPUSH_VAR_VAL), not as a group reference. The group lookup (PUSH_GROUP) is only triggered by the dot syntax (my_group.), which is a separate grammar rule for method calls and property access. Only @ prefixed names (TOKEN_CTXNAMEPUSH_CTX_VAR_VAL) produce a context type on the stack that native functions can accept.

Note that writing @my_group directly does not work as a shorthand either — @ variables are a separate namespace from group names. The @ prefix reads from the group's local context variable table (_CtxLogicVar), which is only populated by explicit assignment. Group names are resolved through findContext(), which is only called from the dot syntax grammar rule. Hence context() is the only bridge between the two.

Getting the Current Group's Context

(@myContext)context();

Getting Another Group's Context

Call context() on a named group using the dot syntax:

(@otherGroup)npc_group_5.context();

This works for any group in the same AI instance, even if it's in a different NPC manager. The group is looked up by name across all managers.

Common Uses

Context references are needed for:

  • Callback targets: Functions like queryEgs, talkTo, receiveMissionItems, giveMissionItems, and giveReward trigger user events on the specified group.
  • Dynamic group creation: newNpcChildGroup returns a context for the newly created group.
  • Cross-group communication: You can call functions on another group's context, e.g. (@grp)other_group.context(); @grp.my_function();

Examples

// Pass current group as callback target for EGS query
(@myGroup)context();
()queryEgs("Hp", $playerEid, @myGroup, 4, "hp_query");
// Get context for a group in another manager
(@bossGroup)boss_guards.context();
()giveReward("Reward!", "Rare!", "Full!", "No pts!", @bossGroup);
// Spawn a child group and store its context
(@child)newNpcChildGroup("template_scout", "patrol_machine", $zone);
// Can now call functions on the child: @child.my_function();

See also

  • AI Script - The @ prefix for context variables and the caller/parent keywords
  • queryEgs - Example of a function requiring a context parameter
  • newNpcChildGroup - Returns a context for the new group

Source: ryzom/server/src/ai_service/nf_static.cpp (context__c), ryzom/server/src/ai_service/state_instance.cpp (findContext)

Clone this wiki locally