Why is the faction array being modified?

Discussion in 'Mod Discussions' started by Quitch, August 15, 2020.

  1. Quitch

    Quitch Post Master General

    Messages:
    5,853
    Likes Received:
    6,045
    Tagging @wondible as maybe he can help with this.

    Galactic War has a problem. I've solved the problem, but I don't understand why the problem exists in the first place.

    In gw_start.js there is a code block at line 484 which works through all the AIs. The problem area are minions. The code pulls a random minion from the faction and writes it to mnn, then updates mnn through the setAIData function. Finally, mnn is pushed to the minions array for the primary system AI.

    Nice and simple.

    Problem is, what happens is that somehow the original array from which the minion is pulled is also being updated. This leads to numerous issues, such as a player's sub commanders having econ values other than 1 if the player cycled the faction list at least once, or AIs having the wrong eco amount for how far into the galaxy they are placed.

    If you want to catch this happening then add the following at the top of the setAIData function:
    Code:
     if (isBoss === false && ai.econ_rate !== 1) console.log(ai.name, "has already been setup")
    Set yourself to the largest galaxy size (Bigger Galactic War helps with this) and you'll see some conflicts come in. The more galaxies you make (change faction a bunch of times for example), the more errors you'll see come in.

    The fix is simply to enclose the sampling in _.clone(). This fixes the problem. But I don't know why. My guess is that _.sample is somehow tying the value in the variable to the array, that it's creating some kind of reference rather than actually writing the data, leading to the original getting changed. Or I'm talking nonsense.

    I've fixed it, but I really want to know why it was broken in the first place.
  2. Quitch

    Quitch Post Master General

    Messages:
    5,853
    Likes Received:
    6,045
    Wondible confirmed this is indeed a references issue.

Share This Page