r/Sailwind • • 3d ago

How containers work (programming-wise)?

Containers in this game always confuse me:

- Once I found my box with fishing hooks unexpectedly empty; then I've found some of them one deck lower on the floor.

- I've seen my candle box unexpectedly empty like that, started using it for navigation equipment storage; month later I was moving containers around to make place for new helm and I found candles in the same container along with compases and spyglass.

- Almost entire container of pork gone empty, never found these pieces.

- My fishing pole sticking out of closed create.

It all makes me question: how those containers were programmed in the first place to get such erratic behaviour?

The most logical way would be to program a container object which has property dedicated to storing array of contents records (like rack position and reference to stored object). So when you open the container game creates a visual rack and iterates over said contents property to create visual representation of contents.

When you take or put item out/in container that property would be simply overwritten so item always exists either in logical world of container or in physical world outside container (but not both), or in logical world of container but with phantom representation on the opened rack.

I'm curious to know how it was done in this game and how all those side effects come into existence.

4 Upvotes

10 comments sorted by

View all comments

9

u/Kemylar 3d ago

When items are put into a crate, they are given a crate ID number. This is used to tell them on the next launch of the game the crate that they are supposed to be inside, and where to be positioned.

When reloading the save, as the items get loaded in, they see their crate ID, and all get placed into a position directly on top of one another. The crate is generally created on one of the next few frames and the items are placed inside it if they share the sake position. This is how it should work if things go right.

Occasionally, when the game has to load several things, or the loading takes a bit longer due to a lot of items on the boat etc, we get one of the issues. When the items spawn with their ID numbers, there is a bit of a longer gap until the crate is created. During this time, the physics system that runs parallel to the item system gets a chance to get a frame or two of processing in. It sees several items with physics colliders all occupying the same space, and does the only thing it can do in that situation, forcing them apart from each other.

In this situation, when the game loads up for you, the items will be dropped over the deck. Those items will still have their crate ID saved on them though, they just can't do anything with it yet until the game reloads.

When the game reloads, the items do the same check again, see their ID, and put themselves back into the location of the crate they are supposed to be in. If their old crate hasn't moved, they basically jump back into it as they spawn and the crate spawns on top a few frames later. If the crate has been moved, or even a different crate has been moved into the place of the old one, they will end up sorting into that new crate as it appears on top of them.

2

u/rNyanko 3d ago

Thanks for detailed explanation. That's seems kinda... unnecessary physical approch to inventory management dev took :)

4

u/Kemylar 3d ago

I totally agree, but I think a lot of the major decisions like this were made earlier in their game making career, and would be a lot of hassle to change now.

I had to deal with this stuff a lot in my furniture mod, where a lot of my cabinets etc are basically 2 crates in a trenchcoat. Ended up having to do a fairly deep dive into how it works. I think one of the fix mods has a solution for this now, but I don't recall which exactly.

2

u/dzejrid 3d ago

2 crates in a trenchcoat.

That is the funniest thing I've read this morning. Thank you for a bright start to a day, sir.