Jump to content

Papyrus variables -- const


SuperElitist

Recommended Posts

The search function seems to be broken right now, so apologies if this has been clearly explained before. I also did some looking around on the web, but I haven't seen any clear explanations for this.

 

Let's say I'm writing a script, and I want it to use a value that can be tweaked by the player. I want to be able to change it from the plugin, but maybe I also want to be able to change it from a holotape or MCM... a while ago I tried to write such a script, and I don't remember exactly what I did, but I ended up 'baking' the value into the save file--no matter what I did after that, every time the script was run it would use the original value. I'd like to avoid that.

 

So let's say I define the following:

GlobalVariable Property DTAG_Radius Auto

What happens if my script sets this property to a value other than what is defined in the plugin? What happens if I change the value defined in the plugin, either before or after the script is run?

Link to comment
Share on other sites

If you set the variable's owning script to be const, then you have to set the variable itself to be const (all properties must then be const). This is checked by the compiler. The const value is meant to be constant (changed only in the initialization in the plugin form in the CK and no further).

 

So if you don't set const, then you can change the variable from either the object itself (some function attached to the object, named either by DTAG_Radius, or by self.DTAG_Radius) or from a foreign object that knows both the formID and the type of the object (if you know the type from a different script, you should cast the foreign object reference before using (e.g. SuperElitist:DTAGObject dtag = randomObjRef as SuperElitist:DtagObject; dtag.DTAG_Radius = 3.14159265). You can even load objects from other plugins which are not loaded in the plugin you are editing (using GetForm and CastAs) and mutate their properties as long as you know the formID of the object, the type of the object, and the method or property to be used.

 

Obviously, if you change the value before the script is run, you won't necessarily be doing much that is useful. The variable will probably be initialized but not to the value that "makes sense" based on what you are planning. You can initialize the value either in the declaration (e.g. Int Property MagicNumber = 2.71828 Auto) in which case it will be available from the beginning, once the mod is loaded, before any events are captured, or subsequently/after, in response to a naturally useful Event, such as OnLoad (Event OnLoad() \ dtag.DTAG_Radius = 0 \ EndEvent).

 

In short, you can definitely change property values (as opposed to local variables, declared as, e.g. int DTAG_Radius = 0) from outside the object and have the change represented in the save. Admittedly, the CK wants you to declare forms and thus all their owned properties as const by default. You can safely ignore this. Specifically, you can create a new script in the CK and edit it to remove the const keyword. Then you can declare properties that are not const, despite what the GUI wants to set up for you. If this did not work, then scripted objects would only be able to refer to properties that were held in common for the whole class (in other words, individual instances would not be able to retain their own data).

Link to comment
Share on other sites

Global Variables are somewhat unusual in that once a script has loaded one, the script seems to ignore any static changes made to the value in the ESP using, say, xEdit. As found by hundreds of users trying to xEdit SKK mod settings through the exposed GlobalVariables and complaining about it not working.

 

The principle to work with is that once a Global Variable is published in an ESP, the only way to modify the value for use is .SetValue() & GetValue() or console [ set Variable TO value; show Variable ]. Assuming the Constant box has not been checked on the GV form of course.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...