Fibers are amazing

Especially in the context of game dev! In path of exile, there was a league years ago called Breach. Your character would run into a Breach and an expanding aura will glow around it, then monsters started spawning one after another.

On the backend, crystal makes that super easy so you don’t need to fiddle with your main game loop, add variables to track a counter/timer, etc. In my case, I just do spawn start_cosmic_bloom(zone, map_entity) and inside that, have a loop with sleep xx to send the monsters. Doesn’t block the IO stream, and even better, we can pass arguments to it… just incredibly easy!

Another case where you can use them is when doing something similar to Diablo 2’s Baal Run (the boss spawns waves of enemies). Although I have my own class for that because there’s a lot going on but it’s a similar concept

When creating anything on the backend that needs to happen sequentially, they are just fantastic! There are probably countless other things they can be used for too… :O

4 Likes

Fibers have one problem in a context of gamedev. Their state cannot be serialized. So in your example you spawn a fiber, then player press “save&exit” and what would you do? You have to somehow terminate fiber while keeping information about which line is executing (and then somehow resume execution at this line).
This limits uses of fibers to games where saving is not allowed or things that are purely visual.

2 Likes

Good point, I actually never thought about that.

But I think this isn’t too much of a restriction, there are still certain cases where a simple state variable might suffice - or you don’t need to track the state, for example in platformers, where you can only save outside of levels (or at specific save points).