OBJECT_SELF is an object-valued NWScript constant indicating the object that is running the current script. This is the object to which actions are assigned (unless an AssignCommand() indicates otherwise) and is the default object for many NWScript commands.

Every script that is executed is associated with a valid object that will be that execution's OBJECT_SELF. (If the game is instructed to have an invalid object execute a script, the script will not run.) For event handlers, the object running the script is the object whose event is being handled, while spell scripts are run by the caster. One potentially confusing special case is item activation; a "unique power" or "activate item" script is run by the module (as part of the OnActivateItem event), even though the similarity to casting a spell from an item might lead some to think the script would be run by the item activator. (In fact, the item activator does run a script — NW_S3_ActItem01 — but that script then triggers the module event, passing control over to it.)

Macro[edit | edit source]

While OBJECT_SELF is called a constant and can usually be treated as such, it is technically a macro. This has a few consequences of note.

First, OBJECT_SELF does not have a specific value, unlike OBJECT_INVALID for example. Instead, OBJECT_SELF gets replaced by the value of the current object (is bound to an object) as soon as the game encounters the "constant" while executing a script. The timing of this binding is of particular note when used in conjunction with AssignCommand(), as any parameters to the assigned command are not "encountered" until after control is transferred. That is, if OBJECT_SELF is used in the parameters to the assigned command, it will be bound to the object to which the command was assigned. To have the assigned command refer to the assigner, OBJECT_SELF needs to be assigned to a variable, then that variable can be used as a parameter.

    // The object running this script shouts its own name.

    // The module shouts the name of the object running this script.
    object oSelf = OBJECT_SELF;
    AssignCommand(GetModule(), SpeakString(GetName(oSelf), TALKVOLUME_SHOUT));

    // The module shouts the name of the module.
    AssignCommand(GetModule(), SpeakString(GetName(OBJECT_SELF), TALKVOLUME_SHOUT));

Second, each use of OBJECT_SELF consumes a small amount of processing power as its binding must be determined. Scripts that make heavy use of OBJECT_SELF can be made marginally more efficient by assigning OBJECT_SELF to a variable, then using that variable in place of the constant.

Community content is available under CC-BY-SA unless otherwise noted.