Singletons. Or maintaining one set of data between multiple

Post » Mon Mar 25, 2013 10:25 am

I'm trying to architect a mod where a script associated with one quest needs to save values that can be accessed or updated by one or more other scripts. Unfortunately globals can only take an int, long or float value, so to store ObjectReference values I need something similar to a Singleton class that maintains a single instance of my values across the entire game.

Papyrus doesn't seem to have any concept of static variables (I'm a Java programmer by trade), so I'm left wondering how I would implement a singleton via Papyrus script.

Any help would be appreciated.
User avatar
Marta Wolko
 
Posts: 3383
Joined: Mon Aug 28, 2006 6:51 am

Post » Sun Mar 24, 2013 11:40 pm

You'll have to forgive me, as I've forgotten the exact scripting you'd need to input, but you can inherit properties/etc from other scripts.

It's essentially setting up a script like a property. So make a quest with a quest script, that can contain all of your variables and data, then inherit from that.

Let me look up one of my scripts so I can get you the exact line.

Edit: Got it-

ScriptName Property PropertyName Auto

So if your script was called MyScript, and you wanted to refer to it as... MainS, you would type:

MyScript Property MainS Auto

Then, to access it's properties, you just do MainS.Property. So lets say you make a property within that master script called MyInt, you could then manipulate that property, on the master script, with the following:

(MainS.MyInt) += 1

That would add 1 to the value of 'MyInt' in the master script.

Using this you can have one master script, and access it in any other script in the game. :smile:

I don't think I'm forgetting anything. I use this to count how many enemies are alive during certain quest events. I have a master script on the quest with a variable, and then in a script attached to the enemies, in an OnDeath() event I subtract 1 from the value of the master int, and if it's set to 0, I advance the quest. The result is that the player has killed all enemies, and the quest advances.
User avatar
Kayleigh Mcneil
 
Posts: 3352
Joined: Thu Jun 29, 2006 7:32 am

Post » Mon Mar 25, 2013 12:54 am

I'm assuming that the quest associated with this script is one that never gets started, but is there simply to act as a "container" for the script. Correct?

Or do I start the "container" quest which, as part of its OnInit() event handler starts the other quests that make use of it for data storage purposes?
User avatar
Chelsea Head
 
Posts: 3433
Joined: Thu Mar 08, 2007 6:38 am

Post » Mon Mar 25, 2013 3:50 am

My quest does start, as it's an actual quest the player does. However I believe the script should work just fine whether the quest is running or not. So just make this quest with the script, then you should be able to access the values stored in it in any other script using the method described above.

EDIT: Another note, I have the property named after the quest the script is attached too, I'm not entirely sure if that's important or not, so just to be safe I'd make them match.

I.e. you have MyScript and MyQuest, so type 'MyScript Property MyQuest Auto'. It may tell the game where to find that script. (I knew at one point, but I've forgotten as it's been so long!)
User avatar
Amy Gibson
 
Posts: 3540
Joined: Wed Oct 04, 2006 2:11 pm

Post » Mon Mar 25, 2013 2:38 am

Managed to locate a mod that appears to be doing something similar to what I am trying to achieve. It looks like it's using the script associated with the "container" quest as a property. I'm going to experiment a bit and see what I come up with.
User avatar
Lisa Robb
 
Posts: 3542
Joined: Mon Nov 27, 2006 9:13 pm


Return to V - Skyrim