Difference between revisions of "Variables and Properties"

241 bytes removed ,  22:20, 9 February 2017
imported>GigaPoint
imported>Cdcooley
Line 179: Line 179:


==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.


<br>'''Example:'''
'''Example:'''
<source lang="papyrus">
<source lang="papyrus">
ScriptName ScriptA extends Quest
ScriptName ScriptA extends ObjectReference


int Property intProperty Auto
int Property count Auto
float Property floatProperty Auto
float Property weight Auto
GlobalVariable Property gvProperty Auto
</source>
Shout Property shoutProperty Auto




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.


</source>
'''Example:'''
<source lang="papyrus">
ScriptName ScriptB extends ObjectReference


Event OnInit()
    ScriptA me = self as ScriptA
    me.count = 1
    me.weight = 12.9


Since we want to get and/or change ScriptA's properties, we'll set up another script called ScriptB:
    (self as ScriptA).count += 7  ; this in-line method works too
<source lang="papyrus">
EndEvent
ScriptName ScriptB extends Quest
</source>


int iVar
float fVar
GlobalVariable gVar


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.


ScriptA property MyScriptProperty
'''Example:'''
ScriptA Function Get()
<source lang="papyrus">
If !GetData
ScriptName ScriptB extends ObjectReference
GetData = True
ReturnVal = (Self as Quest) as ScriptA
EndIf
return ReturnVal
EndFunction
Function Set(ScriptA NewValue)
If !SetData
SetData = True
ReturnVal = NewValue
EndIf
EndFunction
Endproperty
Bool GetData = False
Bool SetData = False
ScriptA ReturnVal


ScriptA Property remoteObject Auto


Event OnInit()
Event OnInit()
        MyScriptProperty.intProperty = 10    ;sets intProperty to 10
    remoteObject.count = 1
        MyScriptProperty.floatProperty = 5.5  ;sets floatProperty to 5.5
    remoteObject.weight = 12.9
        MyScriptProperty.gvProperty.SetValue(10)  ;sets gvProperty to 10
EndEvent
        MyScriptProperty.gvProperty.GetValue()    ;returns 10
        MyScriptProperty.shoutProperty   
endEvent


Event OnActivate(ObjectReference akActionRef)
    if (akActionRef as ScriptA)
        (akActionRef as ScriptA).count += 7
    endif
EndEvent
</source>


</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.
Anonymous user