Editing
Safely increment variable from multiple scripts
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Papyrus Tutorials]] =The Problem= Say the player needs to kill X of some creature. One common way to do this is put an OnDeath script on all the creatures, increment a quest variable, then do something when the count gets to the right number. This can cause problems because it isnβt thread safe β meaning if two enemies die at exactly the same moment, one may end up bashing the other, preventing the player from getting credit for one of the kills. =The Solution= Instead of incrementing the variable from the other scripts, it's better to use a function on the quest script (where applicable) which increments the variable and does whatever it needs to when the count gets to the right number. This ''is'' thread-safe (because it is a function call). So, for example, instead of doing this: <syntaxhighlight lang="papyrus"> ; WRONG WAY Event OnDeath(Actor akKiller) ; increment dead count MS06Script myQuestScript = GetOwningQuest() as MS06Script myQuestScript.DeadCultists = myQuestScript.DeadCultists + 1 if myQuestScript.DeadCultists >= myQuestScript.TotalCultists GetOwningQuest().SetStage(100) endif endEvent </syntaxhighlight> I changed it to this on the actor script: <syntaxhighlight lang="papyrus"> ; RIGHT WAY Event OnDeath(Actor akKiller) ; increment dead count MS06Script myQuestScript = GetOwningQuest() as MS06Script myQuestScript.IncrementDeadCultists() endEvent </syntaxhighlight> And then put this on MS06Script: <syntaxhighlight lang="papyrus"> function IncrementDeadCultists() DeadCultists = DeadCultists + 1 if DeadCultists >= TotalCultists setStage(100) endif endFunction </syntaxhighlight>
Summary:
Please note that all contributions to the CreationKit Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
CreationKit:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Landing page
Tutorials
Game Systems
Editor Reference
Scripting Reference
Recent changes
Help
Tools
What links here
Related changes
Special pages
Page information
Page values