Difference between revisions of "Variables and Properties"

2,572 bytes added ,  19:28, 12 February 2017
imported>Cipscis
m ("Fixed" headers so highest level is h2)
imported>Marth
 
(10 intermediate revisions by 8 users not shown)
Line 1: Line 1:
==Description==
==Description==


Variables and Properties are similar things, they both "hold" values and objects. A variable is "private" meaning that only that script is aware of them, can set their contents, and get their contents.  A Property is essentially a variable that other scripts can access, their contents can be set and get by a other scripts.
Variables and Properties are similar things, they both "hold" values and objects. A variable is "private" meaning that only that script is aware of them, can set their contents, and get their contents.  A Property is essentially a variable that other scripts can access, their contents can be set and get by a other scripts. If a variable or property holds a numeric value, like an integer, get/set returns its value. If a variable or property holds an object, you can access that object's properties and functions. (This is analogous to a reference variable from the old scripting system.)


If a variable or property holds a numeric value, like an integer, get/set returns its value. If a variable or property holds an object, you can access that object's properties and functions. (This is analogous to a reference variable from the old scripting system.)


==Declaring Variables==
==Declaring Variables==
Line 176: Line 175:
EndEvent
EndEvent
</source>
</source>
After this point, you will need to go to the properties window for whatever your script is attached to (in this case, the script properties windows of the script for mySpellEffectScript) and then set the property for myQuestRef to the Quest associated with the quest script.


==Getting Properties From Any Other Script==
==Getting Properties From Any Other Script==
You can use the above example regarding the Magic Effect script as a basis for your script. You must define a property in your script, with the "Type" of your object you are wishing to access. If your object has a custom script, define the type as your object's script name. Be careful not to declare it as the object's name. All objects can have multiple scripts, so you must specify the ''script name'' you want to access.
You can access the properties of any script attached to any object not just those attached to quests. But you will be accessing a specific script attached to a specific object.
 
'''Example:'''
<source lang="papyrus">
ScriptName ScriptA extends ObjectReference
 
int Property count Auto
float Property weight Auto
</source>
 
 
If both scripts are attached to the same game object (Quest, Perk, ObjectReference, etc.) accessing the variables from the other script is simply a matter of casting self to the correct type.
 
'''Example:'''
<source lang="papyrus">
ScriptName ScriptB extends ObjectReference
 
Event OnInit()
    ScriptA me = self as ScriptA
    me.count = 1
    me.weight = 12.9
 
    (self as ScriptA).count += 7  ; this in-line method works too
EndEvent
</source>
 
 
If the other script is attached to some other object then you'll need a way to access that object. The most common way is a property but you might also get access through some Event argument instead.
 
'''Example:'''
<source lang="papyrus">
ScriptName ScriptB extends ObjectReference
 
ScriptA Property remoteObject Auto
 
Event OnInit()
    remoteObject.count = 1
    remoteObject.weight = 12.9
EndEvent
 
Event OnActivate(ObjectReference akActionRef)
    if (akActionRef as ScriptA)
        (akActionRef as ScriptA).count += 7
    endif
EndEvent
</source>
 
 
For a list of objects you can use as a type that are already within the game, visit the [[Script Objects]] page.
For a list of objects you can use as a type that are already within the game, visit the [[Script Objects]] page.


'''See Also: [[Function_Reference#Accessing_Functions_From_Other_Scripts|Accessing Functions From Other Scripts]]'''


==Warnings==
==Warnings==
Be careful with variables and auto properties on scripts that are extended by other scripts - especially where some script somewhere else may have a property pointing to the base script, or trying to cast to the base script. This is because it would be possible to have two copies of a script attached to the same object, thereby creating two copies of the variable/auto property - and the other scripts that refer to the base script may randomly pick which one to talk to.
Be careful with variables and auto properties on "base" scripts, which are extended by multiple other scripts. This is because it would be possible to have multiple scripts containing the same variable or auto property attached to the same object, and the game may not reliably select the appropriate instance of the variable or property.
 
For example, consider the following 3 short scripts:<source lang="papyrus">ScriptName Base extends ObjectReference


This is doubly-true of scripts with native functions, as the game can attach these to in-game objects at any time if it needs to, thereby creating another copy of the variable or auto property.
Int Property MyValue Auto</source>
<source lang="papyrus">ScriptName Derived1 extends Base</source>
<source lang="papyrus">ScriptName Derived2 extends Base</source>
Because both Derived1 and Derived2 extend Base, they each inherit its MyValue property.
 
Now, imagine that an object is created and the scripts Derived1 and Derived2 are both attached to it. Trying to access the value of MyValue by casting this object to Base will not reliably return the same value:<source lang="papyrus">(MyObjectReference as Base).MyValue</source>
 
This is doubly-true of variables and auto properties declared in [[:Category:Script_Objects|native script objects]], such as the [[Actor Script]]. This is because the game can attach these to in-game objects at any time if it needs to, thereby creating another copy of the variable or auto property.
 
In order to avoid these problems, avoid editing native script objects, and in the case where a single object has multiple scripts attached to it that inherit the same property, make sure you cast it to its most derived form before attempting to access that property. In the above example, that would mean using syntax like this:<source lang="papyrus">(MyObjectReference as Derived1).MyValue</source>


==Notes==
==Notes==
*The list of properties in properties dialog is only updated after adding a new property or after compiling the script with the build-in editor.
*The list of properties in properties dialog is only updated after adding a new property or after compiling the script with the build-in editor.
*You should avoid adding or removing Properties to a base item's script if one or more of its child ObjectReference's is already baked into a current save game.


==See Also==
==See Also==
*[[Variable Reference]]
*[[Property Reference]]
*[[Property Reference]]
*[[Default Value Reference]]
*[[Cast Reference]]


[[Category: Papyrus]]
[[Category: Papyrus]]
[[Category: Papyrus Tutorials]]
[[Category: Papyrus Tutorials]]
{{Languages}}
Anonymous user