Updating Timers Every Update

Isn’t updating timers in every update cycle inefficient? Wouldn’t using something like the code below this cut down on the amount of updates?

Although we are only dealing with a couple of AI controlled characters at the moment and it wouldn’t have much effect - the moment you tried to scale up the project for thousands of AI characters it would.

private void UpdateTimers()
{
	_timeSinceLastSawPlayer += Time.deltaTime;
	// _timeSinceArrivedAtWaypoint += Time.deltaTime;
}

private void PatrolBehaviour()
{
	Vector3 nextPosition = _guardPosition;
	if (patrolPath != null)
	{
		if (AtWaypoint())
		{
			_timeSinceArrivedAtWaypoint = Time.time;
			// _timeSinceArrivedAtWaypoint = 0;
			CycleWaypoint();
		}
		nextPosition = GetCurrentWaypoint();
	}
	if (_timeSinceArrivedAtWaypoint + waypointDwellTime < Time.time)
	// if (_timeSinceArrivedAtWaypoint > waypointDwellTime)
	{
		mover.StartMoveAction(nextPosition);
	}
}

If you find there is a pause before the Guard goes to the first point set the initial value to negative infinity.

private float _timeSinceArrivedAtWaypoint = -Mathf.Infinity;

You may want to check out this video blog by Sam explaining floating point timers.

Cool. Floating point issues are things you end up having to work around at some point during development - multiple times normally…

There are other approaches, though it’s difficult to tell for certain if those approaches are more efficient or not. For example, I use Coroutines to manage many of my cooldowns and Update loops… While the coroutine is running, there’s really not a lot of difference, but when there are no cooldowns or , for example in Fighter, no targets, there’s no point in having an Update loop.

I’m surprised I’ve not seen any co-routines yet or anyone suggesting them. However there is a lot of developers that either love them or hate them. Maybe a supplementary video discussing them if it hasn’t already been done.

We do touch on coroutines through the courses. Two specific examples, one is here in the Core Combat course when we transition between scenes. The other is in Shops and Abilities, where we use them to make the abilities work.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms