Equivalent to coroutines in my book. Still not appropriate for many things, such as refilling bottles, opening doors, slewing turrets, turning players slowly, etc.
The reason: coroutines / async fail MISERABLY on the edge cases: restarting, interrupting, etc.
The edge cases still exist but they are far easier to reason about.
So here’s my Jetpack Kurt refueler:
- you call it with either “Load X fuel”
It sets that fuel aside in a float
and every update it adds the fueling rate (RateOfRefueling * Time.deltaTime
) to the fuel tank, decrementing the float
by that same amount.
When that float
is zero, it is done.
- or you call it with “Load until full”
It keeps filling at the time-scaled RateOfRefueling
until one of those refueling attempts “bumps” the top of the tank capacity, then it stops.
Obviously you can see the baked in assumption behind “fill-er-up:” you MUST take on fuel faster than you can possibly burn it. That’s an easy thing to enforce, generally. Either disable engine while fueling, or make refueling fast.
Edge cases:
You continue burning fuel while refueling - no problem! Works fine, as expected.
If it’s already fueling and you call it again, it reasons about what you want:
if it was told “Fill to full” and then you call it with “fill 10 units” well it just ignores the second.
If it was told “Fill 10 units” and then gets “Fill 20 units” it just adds it on.
If the player gets destroyed, the refueler is ON the player, so it just quietly dies.
FAR far easier to debug than multiple-started coroutines.