I'm trying to use one script for the communal storage of "global" variables, and other scripts can require that script to see those variables, but that appears to not be the right way to do it.
So I have a script called "gamedata.js" that looks like this:
var players = {};
exports.players = players;
In one script:
var gamedata = require('./gamedata');
var players = gamedata.players;
players[player_id] = new player(. . .);
for (var pid in players){
console.log(pid + ' is online.'); // This runs correctly
}
Then, later, in another script (I know this is later; it's actually in a loop).
var gamedata = require('./gamedata');
var players = gamedata.players;
for (var pid in players){
// Doesn't even run once
}
Obviously this isn't the right way to do this. How can I do something like this?
Update:
The information necessary to answer this question was not included in this post. For the second example, when it didn't even run once, it was in a different scope, when "players" did in fact mean []. I'm not accepting a correct answer for this because all of the information I included should work correctly, and therefore there cannot be a solution.