Update vs FixedUpdate

Hi all,

Just wondering what is the best way to ensure a uniform timeframe exists for all objects in my game. I’m looking at making something like Factorio, where each conveyor/building needs to ‘move’ or ‘interact’ with an object in a consistently timed way. Adding or removing objects can be done at any time, but their onUpdate needs to be synchronized with every other object. I would also need to have a fixed time needed for certain things like 2 seconds to interact with an object, and after 2 seconds have passed that it triggers/interacts with it.

Is a plain Update sufficient and/or will FixedUpdate be better?

Can I have each object use it’s own Update or do i need to have a centralized loop?

Do I use the Time.time or similar to determine if 2 seconds have passed or will that not be synchronized well with other objects?

Thanks,
Primoz

AFAIK Factorio assumes the game is ran ta 60fps (and if for some reason your computer becomes slower, the simulation also slows down).

So if that’s your goal, you could just update everything each frame and assume 16.67ms has passed each frame and lock your game to work no higher than 60fps.

You could have issues with some of Unity’s systems though (like animation/animator), since those auto use Time.deltaTime and I don’t think you can override that.

Do you need to ensure that your objects are updated in a consistent order, or merely that they all update with the same frequency?

If you just need matching frequency, then giving each thing its own Update function should be fine. (Or FixedUpdate, but pick one and use it for everything.)

If you also need to control the order in which they update within a cycle, then you will either want to play with the settings for script execution order, or else write a single Update function that takes care of everything in the specific order that you want.

Thank you both for your inputs.

I do wish to mix it with animator. Would using Update with Time.deltaTime make these synch fine?

It’s really hard to know about order. I keep thinking yes and then no - I guess the issue is that when one object passes an item from itself to another, i would potentially need the new/next object to know about it and use it if needed. The problem is that I have no way to determine what is the correct order to ensure everything updates correctly, so probably just assume there is no order? Does that sound about right?

Could I ‘merge’ missed updates with Time.deltaTime? Say if I get a one second lagspike, can i run the first half second doing one thing, then if there’s time left over proceed to do something else?

You’re writing the code. You can make the Update function do anything that you can rigorously describe. But splitting time between multiple actions will be more work for you to code.

Programming an object for how it’s supposed to react to receiving a new material from a neighbor that doesn’t arrive until 5/8ths of the way through the current time-step sounds pretty hard to me.

If the timing of hand-offs between different entities is a big issue for your game, then one possible way to solve it would be to divide each time-step into two phases, where first they process any materials they already had from last frame, and only after that do they receive any new materials sent to them in the current frame. Essentially, you have two separate variables for their current stuff, there’s “stuff I have” and “stuff I’m about to get”, and when someone sends new stuff to you they put it into your “stuff I’m about to get” variable, and sometime “between” time-steps you move everything from there into the “stuff I have” variable.

I haven’t spent a long time thinking about it, but I think this probably only makes sense if you’re going to use fixed-size time-steps. (Otherwise, the delay between when a material moves and when you notice it is variable anyway.) So if you were going to do that, you’d probably want a single controller entity with a FixedUpdate function that first tells every object in your game to do phase 1, and then (once everything is done with phase 1) tells everything to do phase 2 (i.e. moving stuff from “about to get” to “currently have”).

1 Like

Awesome - think that’s exactly what i needed - a multi step approach.