Jump to content

Photo

papyrus beginner - how to add to global variable?!

papyrus global variable

  • Please log in to reply
14 replies to this topic

#11
lofgren

lofgren

    Resident poster

  • Supporter
  • PipPipPipPipPip
  • 3,408 posts

It's not "bad practice," it's just not "best practice." Thread safety is only relevant if you have multiple scripts modifying the same variable.



#12
Ghaunadaur

Ghaunadaur

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,222 posts
You still didn't tell why it's bad, Mr. Klugscheisser, you're just saying it's bad.

#13
lofgren

lofgren

    Resident poster

  • Supporter
  • PipPipPipPipPip
  • 3,408 posts
As was mentioned previously it is slightly slower and not thread safe.

#14
cdcooley

cdcooley

    Resident poster

  • Premium Member
  • 4,807 posts

My apologies to the original poster and any other beginning scripters who find this thread. I really don't want to debate this topic any longer. But since experienced programmers keep dismissing the importance avoiding techniques that are known to cause problems I feel the need to respond one more time and then I'm done.
 
Using the Mod function to add one to a global variable will always work and never cause problems. Using the GetValue/SetValue strategy will work most of the time but will sometimes result in the variable not being updated properly. You may never run into the problem cases with your mod, but why take the risk.
 
 

From a compiled code point of view there is little to no difference. 
When you call mod() it is doing exactly what the other call set up is "Property.SetValue(Property.GetValue() + 1)".
(with a few more micro fast reg calls to the cpu) In the end one is a bit more tidier.

The thing which makes using Mod different is not how the code compiles, it's where the code runs. The Mod function is attached to the global variable itself and therefore the generated code will be thread safe. Doing exactly the same thing in your own script is not thread safe and will require the system to lock and unlock the global variable multiple times. It's not enough to consider the generated code but also how the system will run that generated code.
 

It's not "bad practice," it's just not "best practice." Thread safety is only relevant if you have multiple scripts modifying the same variable.

Thread safely is not some obscure theoretical problem like it used to be just a few years ago. And topics like these are found and used as a reference by other people. Just because the particular scenario described here doesn't appear to be a case where concurrent access is a problem doesn't mean the next person who finds this thread won't have such a problem and then run into trouble simple because experienced programmers who should know better are not giving out the best advice possible.

 

Skyrim is a highly threaded game and Papyrus is fundamentally different than most traditional programming languages because of the multi-threaded nature of the game. Even if you don't think a particular action would result in any sort of concurrent access conflict you should still program as if it might. At this point a mod may only be modifying it's own global variable in a specific and controlled way, but it might not stay that way in a future update and you don't know how other mods or the game itself might interact with it. The entire point of storing a value in a global variable is to make it available to multiple scripts and game functions. Ignoring best practice in such an environment really is bad practice.

 

Furthermore sharing a method that is known to cause problems in some situations and justifying is as a general solution is not advancing the collective wisdom of the modding community in a useful way. It might make some sense if the best practice method was somehow more involved or complicated but that's not the case here.



#15
lofgren

lofgren

    Resident poster

  • Supporter
  • PipPipPipPipPip
  • 3,408 posts

I'm not saying you shouldn't correct people. Just saying there is no need to incite panic about it. "Don't ever do it" is the kind of thing you say about the big red button that fires the Illudium PU-36 Space Modulator, not something you say about a function that is slightly less than optimal but works perfectly fine 99.9% of the time.







Also tagged with one or more of these keywords: papyrus, global variable

IPB skins by Skinbox
Page loaded in: 1.035 seconds