Main script running twice
-
Hello, I'm trying to understand the architecture of this game, but I'm having some issues.
I'm playing in my standalone server and I'm experiencing this strange behaviour:
Main code is executed twice on the start and then keep running in two different instances. Every tick the system runs only one of those two, but they are not alternate. It looks more like that system randomly runs only one of the two instances every tick. This is the code I'm using to show the problem I'm talking about:
var scriptnum= Math.floor((Math.random() * 1000) + 1); console.log('start script number '+scriptnum) var loopnum = 0
module.exports.loop = function() { // executed every tick console.log("time:"+Game.time+'; script '+scriptnum+'; loop num '+loopnum + ";") loopnum++ }
As I launch this code this is the output:
[14:09:34]start script number 414 [14:09:34]time:15467; script 414; loop num 0; [14:09:35]start script number 386 [14:09:35]time:15468; script 386; loop num 0; [14:09:36]time:15469; script 386; loop num 1; [14:09:37]time:15470; script 386; loop num 2; [14:09:38]time:15471; script 414; loop num 1; [14:09:39]time:15472; script 386; loop num 3; [14:09:40]time:15473; script 386; loop num 4; [14:09:41]time:15474; script 414; loop num 2;
Why is the code executed twice in this strange way?
Thank you
-
The game is run on a distributed architecture. In that architecture, there is a single DB node and multiple processing nodes. On any given tick your code could be run on any one of the processing nodes.
So in your example there are two processing nodes and you code is randomly run on one of them on each tick. You can see the "time" value incrementing on each tick. Your loopnum counter is only increamented on a single node each tick.
If you wish to store state between ticks, then the easiest way to do it is to use the Memory object. You can freely add properties to this object the game engines handles keeping this object in sync between multiple nodes.
You can store information on the node locally as you have done with scriptnum, but as you have seen this does not persist across nodes. This is useful for information that is static (such as a cost matrix). The downside of using Memory is that there is a small serialise / deserialse cost on every tick.
Also note that globals local to nodes will get periodically reset.
HTH
-
Ok, thank you. It's much clearer now. I understand that use javascript global variables is deprecated and it's better to use the Memory method if I want to maintain persistence between the different ticks.
What do you mean with "Also note that this memory will get periodically reset"? Is there any reliable way to store values between the script iterations?
-
Ah my post a bit confusing at the end so I have edited it to make it more clear.
You should use Memory for persistent memory storage and it is not reset. Its the global variables local to nodes that get periodically reset. I have updated my post accordingly.
-
Thanks! Now is perfectly clear
-
@vianellos you may want to read through the caching docs as they go over this a bit.