ExecuteScriptChunk(string, object, int)

From NWN Lexicon
Jump to: navigation, search
Nwnee logo.jpg Note: This article documents Neverwinter Nights: Enhanced Edition new content or changes/updates/fixes to 1.69 functions. These are all listed under the category and patches pages.
Execute a script chunk.
string ExecuteScriptChunk(
    string sScriptChunk,
    object oObject=OBJECT_SELF,
    int bWrapIntoMain=TRUE
);

Parameters

sScriptChunk
The string is the script chunk to compile and run on the fly.
oObject
The object assigned to execute the script chunk. (Default: OBJECT_SELF)
bWrapIntoMain
Setting this parameter to TRUE allows the script chunk to skip declaring void main() {}. (Default: TRUE)


Description

Execute a script chunk.

The script chunk runs immediately, same as ExecuteScript().

The script is jitted in place and currently not cached: Each invocation will recompile the script chunk.

Note that the script chunk will run as if a separate script. This is not eval().

By default, the script chunk is wrapped into void main() {}. Pass in bWrapIntoMain = FALSE to override.

Returns "" on success, or the compilation error.

Remarks

Make sure that any quotes are escaped properly, as in the example code below.

For dynamic use you can, quite dangerously, use this to execute chat captured in game by capturing listening events on a creature or using a map note contents. This is not recommended for a live module but may be useful for quickly debugging (although less useful now the developer console has a NWscript debug panel).

One other more dynamic use may be to pass parameters using new GetScriptParam() and SetScriptParam() functions, the variable set in a conversation node being some code to run for instance.

Version

1.74.8188

Example

// Example 1: execute a one liner call to boot the first pc found from the server.
//
ExecuteScriptChunk("BootPC(GetFirstPC());");
 
// Example 2: execute a function from an include file.
//
ExecuteScriptChunk("#include \"inc_foo\"\n void main() { do_foo(); }", oTaro, false);
 
// Thank you to Sherincall in the Neverwinter Vault Discord chat for sharing these examples.

See Also

functions: ExecuteScript


author: Shadguy