Difference between revisions of "Variables and Properties"

4,842 bytes added ,  04:33, 13 February 2012
imported>Fowl
m (→‎Conditional Properties: add link to papyrus introduction f)
imported>Evernewjoy
Line 139: Line 139:
==From a non-owned fragment / other quest script==
==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.
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.
===
Rama Edit:
''Here are the steps to using a variable from one script in another 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
[[User:Evernewjoy|Evernewjoy]]
Scriptname RamaJRMasterScript extends ObjectReference
;this value is private to the script and cannot be
;accessed
int hiddenInt = 144 ;initialized value
;the name of the property/var you want to use in ;other scripts, which is just a wrapper for the
;hidden var
int property publicInt
int function get()
return hiddenInt
endFunction
function set(int value)
hiddenInt = value
endFunction
endProperty
[[User:Evernewjoy|Evernewjoy]]
(code above was essentially copied from earlier in this article, but this is a complete running script)
'''2.''' In the other script, where you want to access the variable from the script above, you must define the following:
A. 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.
B. a variable that is going to be used to refer to the script property in A (above)
Here is 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 messege 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:
[[User:Evernewjoy|Evernewjoy]]
Scriptname RamaWorkingSCRIPT extends ObjectReference 
;you must set this property outside this script
;see number 3 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  ;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
scriptVar.publicInt += 1
;you can now manipulate the other script's hidden ;var normally, 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 10
;for the script var when it is displayed
  endif
endif
endEVENT
[[User:Evernewjoy|Evernewjoy]] end of script
'''3.''' Final steps, setting the properties
You must have an object in the game for each script.
You need an instance of the script that has your variable that you want, in this case RamaJRMasterScript.
Once you create the object, could be an activator of any kind or an actor, or anything really, attach the RamaJRMasterScript script.
Name this object something clear, like "sampleReferenceWithScriptInstance"
:)
Now make the switch object, I used a dwarven button, you can use any activator you want.
Now attach the script RamaWorkingSCRIPT to the button/activator.
'''Now set the properties of the button/activator:'''
msg = your sample message that you create that contains two %.f 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
***
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.
***
And 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
:)
Rama


=Warnings=
=Warnings=
Anonymous user