Cross Script Variables

Revision as of 22:31, 22 December 2010 by imported>Bruneauinfo


If you are looking for a way to get the value of a variable from one script running on one object to a script running on another object I can imagine you may have entered the above article name in your search or something similar to it. As you first begin writing mods you may notice that the scripting language of Oblivion assumes the only universally accessible variables should be Global variables. This would not be terrible except that all Global variables are treated as floats. (See Floating Point.)

It is possible a time will come in writing a script when you will wish to have a repository to store variable values in that can be read from and written to by any script. Fortunately this is possible. This article addresses a method for not only storing and retrieving non-float numeric values, but object references as well. This could prove very handy when creating dynamic game scenarios.

The Basics

A scenario for creating and testing this type of functionality includes at least three features:

1 - A quest script that acts as the database for storing values. (One might want to read over the article Quest scripts to get familiar with the source of this idea.)

2 - A script that writes a value to one of the variables in the "database" Quest script when it is activated.

3 - A script that reads a value from one of the variables in the "database" Quest script when it is activated.

Using this basic model you should be able to easily construct a simple scenario to see this functionality in action.

The Quest Script

The quest script acts as a database. Unfortunately Oblivion doesn't provide the powerful database features you would find in an SQL database service. Still, it does provide the quest script where the most basic database functionality can be achieved - a command syntax for reading and writing to its variables from outside of the script from external scripts.

The script:


 Scriptname MyQuestScriptDatabase

 ref refVar ; reference values can be stored and retrieved

 float floatVar ; float values can be stored and retrieved

 short ShortVar ; integer values can be stored and retrieved

 begin GameMode

 ; you don't have to put anything in here
 ; in fact you probably shouldn't. nothing is required here. 
 ; if you set the value of a variable here it will be locked in
 ; instead you will set the values of these variables from outside scripts  

 end

Make sure you set the Script Type as "Quest" in the script editor window and then save your script.

The script is very simple. You just declare all the variables you need for storing values. Too bad there isn't a way to create variables dynamically as with an SQL database. Still, this functionality is much better than nothing at all!

Notes

  • You should NOT assign starting values to your variables. If you need to do this, do it using some other method. Setting values within this script essentially locks the value in. There may be a workaround for doing this, but more testing will be required.
  • The variable names listed in the script can be named whatever you like - preferably something meaningful to their purpose.

The next part of this step is to create a quest. You can read up on the steps for creating a quest on the wiki. Just keep in mind that the only setting needed for creating this functionality is the "Script" setting under the Quest Data tab. Once you create and name your quest select your quest script from above under the script setting for the quest. (For these examples the quest is named MyQuest.)

Storing a Value

Okay! Now that you have your quest and a quest script associated with it you need to know how to write values to it.

The script:

 Scriptname MyWritingScript

 ref refVar ; reference values can be stored and retrieved

 float floatVar ; float values can be stored and retrieved

 short ShortVar ; integer values can be stored and retrieved

 begin onActivate
 
   set "MyQuest".refVar to "ObjectReference" ; or some reference variable

   set "MyQuest".floatVar to 1234567890 ; or some float variable

   set "MyQuest".ShortVar to 2012 ; or some integer variable

 end

That's it! Now associate this script with some object that can be "activated". When the player or an actor activates the object the values listed above will be assigned to the script variables in the quest script.

It's very important to notice where quotes were used in the example script. These are required. If the quotes are not used the script will fail to work properly. Yes, the script will compile, but the values will not be written to the quest script. References as well as the reference for the quest require these quotation marks. However, in the case of a reference variable quotes are only required when setting the value of the reference variable.

set "MyQuest".refVar to ThisReferenceVar 

Reading a Value

Now that you know how to write values to your quest script database you just need to know how to read them.

The script :

Scriptname MyReadingScript

 ref refVar ; reference value as stored

 ref refVarRetrieved ; reference value as retrieved

 float floatVar ; float value as stored

 float floatVarRetrieved ; float value as retrieved

 short ShortVar ; integer value as stored

 short ShortVarRetrieved ; integer value as retrieved

begin OnActivate

set refVarRetrieved to "MyQuest".refVar

set floatVarRetrieved to "MyQuest".floatVar 

set ShortVarRetrieved to "MyQuest".ShortVar

MessageBoxEx "See my reference: %n See my float: %g See my integer: %.0f", refVarRetrieved, floatVarRetrieved, ShortVarRetrieved
			
end

Again the quotes around the name of your quest are critical for this to work. Also, if you use this script exactly as written the message box will show the name of your reference rather than the reference. See MessageBoxEx for more. But the reference value is there and ready for use.


See Also