Jump to content

[Papyrus]Value with GlobalVariable


Recommended Posts

Can someone shed some light on this please?

 

I've always been under the impression that GetValue() / GetValueInt() / SetValue() / SetValueInt() were required to get and set the value of a GlobalVariable within script. However, I've found a mod that simply uses Value. The author has not, as of yet, located the documentation where they learned to do this. They're wondering if it was removed from the CK wiki when Fallout 4 stuff was added.

 

At any rate, I was hoping that someone might know some more information about this. Is it an actual shortcut to the longer forms above? Or is there something that allows papyrus to determine if it should be Get or Set based upon code context?

 

Example usage where SomeVar is a GlobalVariable property variable name:

If SomeVar.Value == 0

SomeVar.Value += 1

EndIf

 

 

Link to comment
Share on other sites

That's just using the property name instead of the specialized function. It's on the Wiki page:

 

http://www.creationkit.com/index.php?title=GlobalVariable_Script#Properties

 

and also used in some vanilla scripts.

 

Your link points to the following which I've seen before:

 

 

  • float Value [read-write]: The current value of this global variable object.

I always understood it to mean that global variables by default are floats rather than integers. What I don't understand is how one gets SomeVar.Value from it.

Link to comment
Share on other sites

If you're interested in the best script performance there are only 3 ways you should access global variables.

  • GetValue() to view the current value.
  • SetValue() to change it to a specific number.
  • Mod() to change it by some fixed amount.

Even that last one isn't really efficient but it's needed if you want to ensure your script is thread-safe and safety is more important than performance.

 

If want to treat the value as an integer just pass an integer value tot he SetValue or Mod functions and use "GetValue() as int" instead of GetIntValue().

 

The developers created the "convenience" functions GetIntValue() and SetIntValue() along with the Value property but those are all just wrappers around GetValue and SetValue. There's the definition of the Value property:

; Easy access to the global's value
float Property Value Hidden
  float Function get()
    return GetValue()
  EndFunction
  
  Function set(float afValue)
    SetValue(afValue)
  EndFunction
EndProperty

It says "easy access" but fails to mention that it also doubles the time it takes to access the value.

 

Here are the bad and better ways to access global variables.

; float values

float now = GameHour.Value  ; slow

float now = GameHour.GetValue()  ; fast


GameHour.Value = 12.5  ; slow

GameHour.SetValue(12.5)  ; fast


; int values

int gold = InvestAmount.Value as int ; slow and needs cast

int gold = InvestAmount.GetIntValue()  ; slow

int gold = InvestAmount.GetValue() as int  ; fast


InvestAmount.Value = 800 ; slow

InvestAmount.SetIntValue(800)  ; slow

InvestAmount.SetValue(800)  ; fast



; The special case of adjusting values by a fixed amount

MG03BooksFound.Value = MG03BooksFound.Value + 1  ; NEVER do this!

MG03BooksFound.Value += 1 ; NEVER do this, it is just like the line above

MG03BooksFound.SetIntValue(MG03BooksFound().GetIntValue() + 1)  ; NEVER do this either, it is even slower.

MG03BooksFound.Mod(1)  ; this is how you change an existing value

FFR10SaltCount.Mod(-1) ; you can also use negative numbers with mod

You can find plenty of cases where the developers used the slow and inefficient methods, but that still doesn't make it a good idea and when you really look at the code hopefully you're agree the slow methods don't look that much better considering they take twice (or more) of the game's time to accomplish the same task.

 

You can also probably find few cases where the game developers used the bad way of updating a variable instead of the Mod function. Don't follow their bad examples, it's things like that which lead to broken quests.

Link to comment
Share on other sites

Thank you. That provides quite a bit of useful information. And I'll definitely stick to using GetValue() and SetValue(). Won't go back and change existing mods just to turn GetValueInt() and SetValueInt() into the faster cast method, but if there is some other reason to edit them they can be updated then.

 

At the very least, I now understand how that other mod was able to use Value and get away with it working. I was scratching my head wondering why it worked when there were no explicit instructions on the wiki explaining it.

Link to comment
Share on other sites

And I think all of those options are a case of the developers coming from different programming backgrounds and adding their own preferred style of accessing things. I expect after a few more games one of the styles will win and the others will be removed because it's bound to be just as confusing for new content developers as it is for us.

Link to comment
Share on other sites

 

That's just using the property name instead of the specialized function. It's on the Wiki page:

 

http://www.creationkit.com/index.php?title=GlobalVariable_Script#Properties

 

and also used in some vanilla scripts.

 

Your link points to the following which I've seen before:

 

 

  • float Value [read-write]: The current value of this global variable object.

I always understood it to mean that global variables by default are floats rather than integers. What I don't understand is how one gets SomeVar.Value from it.

 

 

It sounds like CDCooley answered your question, but just so you know if you look at the properties section of each script page on the wiki it gives you properties that are just like properties you might create in your own scripts. For example if you look at the ObjectReference page under properties you see:

 

 

  • float X [read-only]: The current X position of the reference.
  • float Y [read-only]: The current Y position of the reference.
  • float Z [read-only]: The current Z position of the reference.

 

 

 

These are values that can be called just like you would call a property on a script that you write. For example
float MyZ = MyObject.z
will set the MyZ variable to the current Z position of the reference MyObject.
Link to comment
Share on other sites

Just a wild guess here but, if there are commands to read and wright as a global (by the same name) ... I would assume it would act as any other variable would in the code.

It just wouldn't be setting the value in the Global until you called SetValue().

 

Be easy to test that out ...

Make a Global, then in the script SetValue() to something. Then change it as you would any other non-global variable and echo it back to see if it changed ...

Then call GetValue(), If the variable is the same as it was set with SetValue() and not what you changed it to ... then that is the case.

Edited by NexusComa
Link to comment
Share on other sites

  • Recently Browsing   0 members

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