Jump to content

[LE] Global variable counter doesn't update properly.


powerofthree

Recommended Posts

I have a counter global variable in a script attached to about 60 actor aliases which will update on Event OnDying(). This is then displayed in my MCM.

 

Works for individual cases where only one actor dies at a time but if I kill them all at once, the counter does not update properly. Debug.Trace confirms it.

[12/11/2017 - 03:52:40PM] Torbjorn Shatter-Shield is dead. no is 86.000000
[12/11/2017 - 03:52:40PM] Hermir Strong-Heart is dead. no is 87.000000
[12/11/2017 - 03:52:40PM] Oengul War-Anvil is dead. no is 87.000000
[12/11/2017 - 03:52:40PM] Citizen is dead. no is 88.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is dead. no is 89.000000
[12/11/2017 - 03:52:40PM] Citizen is dead. no is 90.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is dead. no is 91.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is dead. no is 93.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is dead. no is 94.000000
[12/11/2017 - 03:52:40PM] Angrenor Once-Honored is dead. no is 95.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is dead. no is 95.000000
[12/11/2017 - 03:52:40PM] Lortheim is dead. no is 95.000000
[12/11/2017 - 03:52:40PM] Windhelm Guard is deade. no is 95.000000

Any ideas on fixing this?

Link to comment
Share on other sites

That looks like you're hitting a multi-thread issue. If you've got anything of the structure X.Set(X.Get()), what can happen is that if you have multiple things executing it at the same time, they'll all get the same value, and then if they're all, say, trying to increment it by one, they'll all set it to the same value. The usual way around this is to write a wrapper mod function to enforce a complete increment before the next one is processed.

Link to comment
Share on other sites

Have you had a look at the Threading Notes page on the CK wiki? You may have to make a single entity that updates the global variable when the other scripts call a specific function in a script attached to that entity. The function can use e.g. a spin lock to queue up the execution of the rest of the function and thus the incrementation of the global variable.

 

Alternatively, you could have your scripts chuck a token item into a container and attach a script, which periodically clears out the container and increments the global variable by the number of token items, to the container. If you don't want to have the container script checking all the time and you don't need the global variable to update instantly, then you could set up a delay system that uses the OnItemAdded event to unregister any previous updates, and register for a single update. Or you could use the OnItemAdded event to switch into an active state (clear out the container every N seconds) and after some time of inactivity switch back to an idle state.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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