This week - Persistence and Ephemerality
November 14, 2019 - Thursday afternoon
This week so far -- I can save!
I've implemented saving the game. I make heavy use of Discriminated Unions in FSharp, which are serializable by default using BinaryFormatter
. So almost no extra work needed there. Sweet!
Saving took just a little bit of a rework CSharp side (I'll get into the FSharp/CSharp design in some other post). Unity's Vector3
and Color
weren't serializable either, but stick in some SerializationSurrogate
's and you're good to go.
This was particularly exciting for me, as I went with a design making heavy use of state machines (implemented as DU's) based on a cursory scan of the internet telling me they're easily serializable. Turns out - yes! Good stuff. The save files clock in at a <200kb with a few hundred residents. A-OK.
I'd made some concessions in terms of determinism to get performance up, but if savefiles are that small -- it could be possible to still have multiplayer just by syncing the whole world every now and then.
Coming up this week -- Recycling
Next up is to implement a game object recycler - not all residents are going to be visible all the time. When they go away, their instantiated body GameObject
can be re-used by the next resident (a la Altered Carbon).
I'm hoping this will speed up performance a bit, but this is not solely a performance change - residents' bodies shouldn't just hang around inside other models once they've "entered" a building. I expect, just as in real life, most residents are going to be hidden away in buildings more often than not.
This should bump up the number of max residents even more -- but at this point performance has well surpassed my initial goal of smooth 60fps for 500 residents on a Pixel 2 (what google told me was the minimum number experts will start calling a place a 'village'). The main performance bottleneck now is NavMeshAgent
pathfinding, and since nav stuff happens in separate threads this only bottlenecks how fast residents start walking, not the framerate.
Next week -- More interesting behavior
With most of the plumbing stuff out of the way I can start focusing on pure game logic. Residents only have a basic routine now - find a home, find a workplace, work until they're tired, go and sleep, go work until tired, etc. Though this already covers 90% of the adult human experience, there are some more things I need to implement!
Probably the main reason I started working on this game was to create some sort of socio-economic simulator. This means residents need to be able to buy things! And figure out what they want to buy! And from where! And where to work! And who they talk to! And.. and..
This stuff will probably take more than week. But from a set of simple behavior, how they come together to produce a complex system I'm excited to see.
Here's to me continuing with this blog!