Calculating a Value Every X Seconds instead of Every Frame – Unity3D Bolt Micro-Tutorial

This entry is part of a series of tips I am creating as I make my game.

You can use Bolt for free by downloading it from the Asset Store.

Details

What we need: Increase efficiency of the game by performing a calculation less often (for example, every 3 second instead of every frame).

Input: The calculation you want to perform. In my example below, I needed to calculate the distance between a GameObject in the scene and the current position of ‘this’ GameObject.

Output: A State Machine that executes said calculation every X seconds (3 Second in this example).

How this saves you time: Quit typing out all those timers in your C# scripts!

Summary: Efficiency is a huge deal in video game coding, and one way we can increase efficiency is by decreasing the number of calculations our game is performing. This can be done with the use of timers, which in olden days would be several lines of code in an Update() or required a Coroutine. This method uses the State Machine in Bolt.

Output

You will end up with a calculation that is performed once every X seconds (3 Seconds shown in the example gif above) instead of every Frame, increasing performance in your game.

How to Implement

First, you will need to create a State Machine, not a Flow Machine and attach it to a GameObject of your choice (I try to attach mine to game objects with names that describe what it does).

Next, click on Edit Graph in the inspector (open the State Machine). You should be looking at this:

Right click on the Start and click Make Self Transition.

Double click on the Yellow bubble (Represents a Flow Graph, indicated via red arrow). You should see this:

Add an OnEnterState and a Timer, connect them and set the Duration value on the timer to how often this calculation should be done. Note: You can also expose the duration to an external variable connected to say, a GUI Element, so that you could modify this easily instead of going into the Flow Graph view inside this State Machine.

Back out to the State Graph, and double click on Start to open it up.

Delete the Update() and the OnExitState() Events. All we need is the OnEnterState(). It shouldn’t matter when we do this calculation, because the timer is the limiting factor. Throw your calculation in here; in my case I’m calculating the distance between two Entities in my game and outputting the result to the Debug Log.

At this point you should be done! Here’s a demonstration of my calculation in action:

1 comment

  1. Interesting idea with the self transition. I will give this a try in one of my state machines. This could even be optimized by turning the On Enter State into a coroutine and replace the timer with a simple wait unit, so the timer does not have to calculate all the output values.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: