In this article we are going to see how to use the Invoke method of Unity’s MonoBehaviour class to execute an action with a certain delay. This allows us for example to create mechanisms that work for a while.
Before we start I invite you to watch the video I made to summarize this article. English Subtitles available.
A SHORT BREAK WITH THE LATEST NEWS
LET'S CONTINUE WITH THE ARTICLE
To analyse the Invoke method we’ll be using the GameDevLab’s “Invoke a Method” station, which is a vending machine for the refreshing new GameDevCola that not only quenches thirst but also heals your wounds.
By approaching the machine we can interact with it using the E key. When you press it, the machine will become inactive, that is, you will not be able to interact with it for a while. After a few seconds the machine will eject a can of GameDevCola and seconds later will again be enabled for interaction.
In the script to complete we have the methods “DeliverACola” and “PrepareMachine” that will deliver the drink and enable the machine respectively.
Then we have redefined the OnTriggerStay method that allows us to check if the character is standing in front of the machine.
Within this method we check if the Tag of the GameObject is that of the player, then if the machine is ready (boolean machineReady), we tell the UIManager object that can interact and this is responsible for displaying the message on screen.
Then we check if the E key is pressed. At this point the machine receives a request for a GameDevCola. The first thing you do is deactivate the machine and then in the region with comments is where we must complete the missing code.
Analysis of events in time
I want to emphasize this part because it is important to understand the idea.
When we call a method in C#, the execution of it is done at that precise instant, all instructions are solved even before refreshing the screen image.
So if we called the methods directly in the region marked with comments, what would happen is that the can would be expelled and the machine enabled again in the same instant in which the E key is pressed. As illustrated in the timeline in figure 3.
Figure 3 shows a timeline in which there are two different points in time, the first point being the beginning of the game. Sometime later in time is the second point, in which the player asks for a can, the machine delivers it and it is enabled again, these three actions at the same time.
Figure 4 shows four different time points.
Point 1: Start of the game.
Point 2: The player orders the GameDevCola.
Point 3: X milliseconds after the order is placed, the machine delivers the can.
Point 4: After Y milliseconds of can delivery, the machine is enabled again.
This is what we are going to do using the Invoke method of the MonoBehaviour class.
The Invoke method can be called directly without reference to a class.
We must indicate two parameters, the first is a String with the name of the method we want to invoke, the second parameter is a float to indicate the number of seconds that will pass until the method is finally executed.
Upper and lower case must be respected. If we get confused at this point, a bug may arise that is difficult to track. If the method is called “DeliverACola” and we invoke the “DeliveraCola” method what will happen is that we will try to call that method but as it is not defined, the program will go ahead without executing those instructions.
Figure 5 shows the resolution for the exercise. The can will be delivered two seconds after the player presses the E key and the machine is re-enabled four seconds later.
The Invoke method is used to postpone the execution of a method. Although it is not the only way to achieve this, it is a simple resource, easy to remember and easy to implement.