Reference Variables


Like other variables, reference variables must be declared before they can be used:

ref   refVarName  

You can use a reference variable anywhere you could use an object reference. Uninitialized reference variables act as if they referred to the scripted object or quest itself; otherwise a reference variable acts just like any other reference.

A reference variable whose object is not in memory (such as a static object after the player leaves that object's cell) is treated as uninitialized.

You set reference variables using the set command, like other variables. Normally, you will want to use a reference variable function, which returns a formID. For example:

set myRef to GetContainer  

Like other variables, you can set a reference variable on another scripted object:

 set BobRef.myRef to GetSelf   

To check if a ref variable is unset:

if myReferenceVariable == 0

To check if a ref variable is set:

if myReferenceVariable != 0

To check if a ref variable is another reference (in this example the player):

if myReferenceVariable == player

Or not another reference:

if myReferenceVariable != player

Note: This is not working consistently. It is advised to use:

myReferenceVariable.GetIsReference player

Example:

scn TheMastersSword ; Whoever equips it gets +50 to blade

ref UserRef

begin OnEquip
  set UserRef to GetContainer
  UserRef.Modactorvalue blade 50
end

begin OnUnEquip
  set UserRef to GetContainer
  UserRef.Modactorvalue blade -50
end
 

Keep in mind, that References to non-persistent objects aren't accessible when the object is not in memory (i.e. in another cell).

The same applies to inventory items that are currently within a container (i.e., items that an actor is carrying). This means that scripted functions which require a reference, such as:

itemRef.pms ghosteffect 10

will only work on an item when it is not in inventory. The CS will warn you about this when using a reference variable on a carryable item, but it will not warn you if you use an implied reference, such as:

pms ghosteffect 10

Trying to access a non-accessible reference might have no effect at all, but it's equally probable that it will crash the game. While there are safeguards in the CS to prevent you from accessing such references directly, you can bypass them by using reference-variables.

Do so at your own risk...