Having trouble with a conveyor belt system, any help?

I’m trying to make a factory builder and here is my issue:

I’m using a tick system using events (like in CodeMonkey’s video) and a grid of tiles to do this. I thought that the tick event I’m using would be triggered in all tiles simultaneously but it isn’t. The tiles in my game are activating from bottom to top, left to right and because of this, there are irregularities in my conveyor belt speeds.

In conveyor belt lines going from right to left, an item spends one tick on each conveyor belt tile (this is what I want).
But with conveyor belt lines going left to right, items instantly teleport from one end of the line to the other. (Because tiles are activated left to right, the tile receives the item and sends it to the next tile on the same tick)

Any help? thank you

I wouldn’t update positions based on “global tiles”. Each belt has a start and end point, and a direction. Of course the belt itself is grid-based but now all you need to do is to take the end point of a belt and update that last position first and then continue updating the tiles closer to the start point.

Junctions you need to deal with though, probably alternating on every update.

Barring very few cases which take extra effort to achieve, NOTHING in Unity is “simultaneous.” There’s only one thread, and it does everything by iterating through the objects one at a time.

In grid/gang systems like you’re discussing, if you want it to behave as if everything happened simultaneously, you will want two copies of the state of the cells. You can then iterate your tick handling exactly as you’re doing now. The calculations in each cell must look ONLY at the previous tick state, and decide the current tick state. Then when the frame is done, you swap/copy the state so that the current tick state of all cells is transferred to the previous tick state.

For example, a conveyor cell would say “if I had something on me in the last tick, advance it off me in this tick; if my upstream neighbor had something on them last tick, now I have that something on me this tick.”

If you look at other automata systems, even Conway’s Game of Life, this scheme is employed to ensure that there are no artifacts of handling the tick state updates in any particular order.

1 Like

Thanks gang.I had thought of checking old tile states,I just wanted to see if there were easier solutions

Not trying to discourage you, but i feel the need to mention that factory builders are some of the most complex topics you can tackle. At least if it’s gonna be more than a proof of concept in a small, finite map. If you want scalability (which is basically a defining feature of the factory building genre) you need performance. Not just any performance, but the best. Factorio is probably one of the best optimized games out there, period. On top of the absolute basic (but still highly advanced) topics like multithreading, you will have to deal with keeping algorithms as simple, and grouping together as many entities, as possible. The factorio devs wrote a lot of really nice blogposts about how they approached some of these topics, and you may want to have a look at… all of them… if you are serious about this project.

There is also someone who implemented the belt system of a factorio clone in Unity and got to a million or so concurrent items being transported at 60 FPS. That would probably be your best starting place. Nowadays you may want to take a look at DOTS for such a task too, which should be even faster, but on the other hand it may be even more complex to implement the required data structures.

1 Like