How to make multiple Scenes read-modify-write global memory safely

Godot Version

Godot Engine v4.4.stable.official

Question

` I have an enemy scene, which I will duplicate in the main scene to create multiple enemies.
I plan to makes these AI enemies circle the player-character and attack the player one at a time.

This is a 2D topdown game.

For this I wanted to keep state of these enemies global, so that every enemy can read every other enemy state and see if it can go for attack state, if not just wait a random time and try again.

What is the correct way to program this behaviour?
On the internet I couldn’t find how to make these multiple enemy-processes safely read-modify-write to that global state so that there is no race-condition.

Please help, thank you.
`

The only way you’d end up with any race conditions is if you were using multiple threads to control your enemies. Using a single thread you’d be looping through your enemies one at a time, so their state would always be accurate.

Unless you plan on having a lot of enemies or complex logic, you probably don’t need to bother with threads. This sentence is on purposely vague as exactly how to quantify “a lot” and “complex” varies depending on many factors. I don’t know enough to give any specific guidance for these, but in general the best way to know for sure is to just test your specific game.

If you do plan on using threads, the official docs have a section about using threads that’s worth reading.