Difference between revisions of "Variables and Properties"

4,106 bytes removed ,  12:14, 14 February 2012
Formatting tweaks and general overhaul to the "Getting Properties of a Quest Script" subsection. In my opinion it was rather complex and poorly formatted. This edit states the same information in a simple and clean manner.
imported>Evernewjoy
imported>Delfofthebla
(Formatting tweaks and general overhaul to the "Getting Properties of a Quest Script" subsection. In my opinion it was rather complex and poorly formatted. This edit states the same information in a simple and clean manner.)
Line 83: Line 83:
int property myInt = 5 auto
int property myInt = 5 auto
</source>
</source>


==Auto Read-only Properties==
==Auto Read-only Properties==
Line 91: Line 92:
int property myReadOnlyInt = 20 autoReadOnly
int property myReadOnlyInt = 20 autoReadOnly
</source>
</source>


==Conditional Properties==
==Conditional Properties==
Line 102: Line 104:
More information on the conditional keyword can be found in the [[Papyrus_Introduction#Writing_Custom_Functions|Papyrus Introduction]]
More information on the conditional keyword can be found in the [[Papyrus_Introduction#Writing_Custom_Functions|Papyrus Introduction]]


=Getting Properties of a quest script=
=Getting Properties of a Quest Script=
==From result script owned by the same quest==
==From result script owned by the same quest==
Often you will need to get a property of a quest script, and use it in a result script somewhere else. This is one of the more tricky things, but once you understand what's happening, it makes sense. First look at the example, then we'll describe what's happening.
Often you will need to get a property of a quest script, and use it in a result script somewhere else. This is one of the more tricky things, but once you understand what's happening, it makes sense. First look at the example, then we'll describe what's happening.
Line 137: Line 139:
</source>
</source>


==From a non-owned fragment / other quest script==
Need to write this... basic gist: You need to define a property in your script (and set it through the editor interface to be the other quest whose properties you want access to), then you can access that property's properties. In other words, your script has a property of the other quest; then you access that property's properties.
----
'''Accessing a Script's Variables From Another Script'''
----
''Here are the steps to using a variable from one script in another script, please note the script could be of any type, not just a quest script.''
'''1.''' Define the variable you want to pass to other scripts as a property as explained above.
Here is a full script containing a property / publicly-available variable


==From Within a Magic Effect Script==


If you want to access any of the Functions or Variables within a quest script (or any script for that matter), you must access that script's properties.
All scripts can access other scripts, however they can ''only'' access the script's properties.


Let's look at an example where a spell accesses a quest's properties:
<source lang="papyrus">
<source lang="papyrus">
Scriptname RamaJRMasterScript extends ObjectReference
Scriptname myQuestNameScript extends Quest


;this value is private to the script and cannot be
Int Property PublicInt Auto ; This value is defined as a property and can be accessed from outside this script
;accessed
int hiddenInt = 144 ;initialized value


;the name of the property/var you want to use in
Int PriviteInt = 144 ; This value is private to the script and cannot be accessed from outside this script
;other scripts, which is just a wrapper for the
;hidden var


int property publicInt
Function DamageTarget(Actor akTarget)
int function get()
;This code will damage the akTarget for PublicInt damage
return hiddenInt
akTarget.DamageAV("Health", PublicInt)
endFunction
EndFunction
 
function set(int value)
hiddenInt = value
endFunction
endProperty
</source>
</source>


(code above was essentially copied from earlier in this article, but this is a complete running script)
Now that we have defined our quest script and created an accessable property, we can control it from the outside.
 
 
'''2.''' In the other script, where you want to access the variable from the script above, you must define the following:
 
* the script that contains the variable you want to access, you must define the script as a property, note that the type of the property is the name of the script that contains the variable you want.
 
* a variable that is going to be used to refer to the script property in A (above)
 
 
''See below for a working script with some especially important lines emphasized.''
 
 
Note that the message used in this script must be created by you if you want to use this script example, use the %.0f twice in your message to accommodate the two variables I am passing. I pass one variable just so you have a way to know your message itself is working properly:
 
 
<source lang="papyrus">
<source lang="papyrus">
Scriptname RamaWorkingSCRIPT extends ObjectReference 
Scriptname mySpellEffectScript extends activemagiceffect
 
;you must set these "property" values outside this
;script.
;see number 3 in article (below)
 
message property msg auto
 
 
;***
RamaJRMasterScript property masterScript auto
RamaJRMasterScript scriptVar
;***
 
;the scriptVar above is very important because you
;cannot directly access a property script, you must
;use a variable that is private to this script.
 
 
bool done = false ;ensures switch runs only once
int test = 2
 
;any event could be used here, like onTriggerEnter,
;make a trigger volume in the editor and attach
;this script to it and use onTriggerEnter for
;easy testing
 
;this example works with an activator of some kind
;like a button or door
 
EVENT onActivate (objectReference triggerRef)
 
if (!done)
 
  ;only run if player is the one activating this
  ;object
  if(triggerRef == game.getplayer())
      done = true  ;makes button run only once
;--- External Variable Script Section Begins
 
;***
scriptVar = masterScript
;***
 
;this line above is VERY important, you have to
;set the script var within the script itself,
;to the script-as-a-property that contains the
;the variable you want to access
 
;Note you must set the scriptVar here, do not try
;to initialize scriptVar to masterScript, this line
;must be in the body of the script itself


myQuestNameScript Property myQuestRef auto


scriptVar.publicInt += 1
Event OnEffectStart(Actor akTarget, Actor akCaster)
 
myQuestRef.PublicInt = 20 ;Set the Public Int to 20
;you can now manipulate the other script's hidden
myQuestRef.DamageTarget(akTarget)
;var via its public property name and functions,
EndEvent
;using the scriptVar
 
msg.show(scriptVar.publicInt,test)
 
;a sample message to prove you have accessed the
;other script correctly, should get a value of 145
;for the script var when it is first displayed
  endif
endif
 
endEVENT
</source>
</source>




'''3.''' Final steps, setting the properties
=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 must have an object in the game world / your dungeon / cell for each script.'''
For a list of objects you can use as a type that are already within the game, visit the [[Script Objects]] page.
 
 
''You need an instance of the script that has your variable that you want, in this case RamaJRMasterScript.
 
You need this even if the script is just a variable storage device / reservoir''
 
 
A. Once you create the object, could be an activator of any kind or an actor, or anything really, attach the RamaJRMasterScript script.
 
B. Name this object something clear, like "sampleReferenceWithScriptInstance"
 
C. Now make the switch object, I used a dwarven button, you can use any activator you want.
 
D. Now attach the script RamaWorkingSCRIPT to the button/activator.
 
 
E. '''Now set the properties of the button/activator:'''
 
msg = your sample message that you create that contains two %.0f for use with the variables
 
masterScript = point this to the INSTANCE/Object of the script that you have, the name will appear as the fancy name you created above.
 
Plus it is very likely to be the only option the list
 
 
'''Things to Remember'''
 
* If you do not make an instance of the script that contains the variable you want to access, you cannot use the variable, you must have an object with the variable-reservoir script attached to itself.
 
* Remember to set the scriptVar to the script-as-a-property, the highlighted line in the script itself.
 
 
If you follow all these steps you will now be able to access and modify variables from one script in another script.
 
 
'''Example uses:
'''
 
1. keeping track of a total of enemies killed, where each enemy has a death script to increment a value in a master script
 
2. keeping track of number of switches activated, where each switch increments a master script variable
 
3. basically infinity different very important and wonderful things, hee hee!
 
Infinite ♥ and thank you's to our friends at Bethesda for giving us such an awesome game as Skyrim and such an beautiful editor as the Creation Kit!
 
 
The Creation Kit is incredibly stable, robust, and a joy to use.
 


Thank you!


=Warnings=
=Warnings=
Anonymous user