Trade Route Bug - Disappearing Trade Units and Self-Generating Trade Routes

Chrisy15

Flower, Beautiful
Joined
Jul 9, 2015
Messages
2,127
Using the method Unit.SetMoves on a Trade Unit is BAD.

Finally managed to solve a bug which I'd occasionally observed while playing, but which I'd struggled to find any discussion about online. It's caused a very specific action in a Lua script, and is easy to just not notice while playing, so I thought it'd be worth making an informational post about so that modders can avoid creating the problem.

The issue is caused by the method Unit.SetMoves being used on a Trade Unit to grant said Unit movement points.

Using SetMoves on a Trade Unit in a foreign Major Civilization's City, or which are not on a City tile, appears to have no consequence.
Using SetMoves on a Trade Unit in a Minor Civilization's City causes the Unit to be deleted but causes the Trade Route itself to continue; this allows a Civilization to construct more Trade Routes than they are meant to as these Unit-less Trade Routes do not contribute towards your Trade Route capacity.
Using SetMoves on a Trade Unit in one of its owner's Cities causes the Unit to move two tiles towards a target City, but if possible the Trade Unit will create a new Trade Route by itself - self-determining a new target City - rather than following the Trade Route which it is on. This again does not delete the previous Trade Route.

The solution to this problem is to take conscious effort to avoid Unit.SetMoves being invoked on Trade Units. Player.Units returns Trade Units as part of its iteration; Plot.GetNumUnits and Plot.GetUnit don't give you Trade Units, but Plot.GetLayerUnit does. Unit.IsTrade returns true for Trade Units, and false for everything else.
These problems can occur on GameEvents.PlayerDoTurn, but will apply whenever SetMoves is invoked on a Trade Unit in a City. Unit.ChangeMoves might not cause these problems, nor does passing a value of 0 into Unit.SetMoves, but the same precautions may as well be employed anyway.

So if you're writing code which uses SetMoves take care with what Units you're invoking the method on, and if you observe one of these issues in your game then this is what you'll want to trawl through your mod list looking for.

Enterprising minds might try and make something useful of the self-generated trade route interaction for some kind of effect, but anyone who does so can test the consequences themselves.
 
Last edited:
Top Bottom