← Devlog

The tick that pays for itself

Galaxy Trader runs its economy on a fixed step, and the first thing that broke was the port that could not keep up.

The economy in Galaxy Trader updates on a fixed step of one second, not on the frame. A port that prices its goods sixty times a second would be expensive and pointless; a port that prices them once a second is cheap and behaves the same on every machine. That decision was easy. The consequences were not.

One second is a long time

The ship moves every frame, the economy moves once a second, and the player does not notice the difference until they buy a good and the price does not change for a second. In the prototype that read as a bug: you sell, the number stays put, and you press the button again.

The fix was not a faster simulation. It was letting the price reflect the stock the trade will leave behind, computed at the moment of the click, and letting the once-a-second pass catch up to it. The simulation stays cheap; the interface stays honest. The engine never has to know that a player clicked.

Where it broke

At forty ports the economy pass crossed two milliseconds. At eighty it crossed five, and the frame budget line started to matter. The profiler put almost all of it in one place: every port rebuilt its list of neighbours every tick, and every port’s price function walked that list.

The list is static. Ports do not move. It is now built once, when the sector loads, and the per-tick pass only reads it. That single change took the pass from five milliseconds to under one, and the code got shorter.

The prototype build, which is not public yet. When it is, this frame will be an itch.io player that loads only after you press play.

What I would do differently

I would have written the step first and the ports second. The first version of this ran the economy inside _process, which made every port’s cost depend on the frame rate and made the profiler lie to me for a week. A fixed step is not a performance feature — it is the thing that makes a measurement mean anything.

Next: the save format, which is currently the whole economy serialised as a list of numbers and a promise that the order never changes.