@Dissi I was creating the flag in an owned room on one tick, and then on the next tick moving it to a room with no vision. This scenario does not always cause the problem but seems to occasionally.
@SteveTrov Thank you for this input, it led me to do some more testing with interesting results.
I wrote the following code:
let flagName = "test";
let desiredPosition = new RoomPosition(25, 25, "W27N13"); // no vision of this room
let creationPosition = new RoomPosition(6, 29, "W25N13"); // owned room with vision
global.flagTest1 = function() {
let flag = Game.flags[flagName];
if (!flag) {
creationPosition.createFlag(flagName);
} else {
flag.setPosition(desiredPosition);
}
log(`Flag exists: ${flag}`);
};
global.flagTest2 = function() {
let flag = Game.flags[flagName];
if (!flag) {
creationPosition.createFlag(flagName);
} else {
flag.setPosition(Game.time % 2 === 0 ? creationPosition : desiredPosition);
}
log(`Flag exists: ${flag}`);
};
And performed the following tests:
I put flagTest1(); at the top of my main loop while the flag was NOT in existence.
Result: A flag by the name "test" was created, and then proceeded to alternate in and out of existence at creationPosition, never making it to desiredPosition every other tick the log statement would say that the flag did not exist. I do not have any other code in my code base that would be removing this flag. Eventually after some number of ticks (perhaps a dozen or two) the flag successfully moved to desiredLocation and stayed there, behaving as you would expect.
I replaced flagTest1(); with flagTest2(); at the top of my main loop. The flag was already in existence.
Result: The flag "moved" to desiredPosition but also remained at creationPosition. There are now two instances of the flag that are both clickable and selectable in the UI.
I have repeated both of these tests multiple times with similar results.
Another interesting note: While the flag is "duplicated" and exists in both rooms, calling Game.flags.test.remove() returns OK but neither flag is removed. Both stay in existence after repeated calls to .remove() from the console. After clicking one of the flags in the UI and pressing the UI button to remove the flag, it is removed but the other version of it stays in existence and calling .remove() now functions properly on the remaining flag.