How to DELAY a FUNCTION using INVOKE method

IMPORTANT UPDATE

This article belongs to an old series of the channel, currently there is a video available that explains better how to use the Invoke function to make a function be called with delay, you can watch it here:

🟢 How to DELAY a FUNCTION using INVOKE method


Old Article below

Introduction

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.

Go to the project’s main page

Before we start I invite you to watch the video I made to summarize this article. English Subtitles available.

Procedure

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.

Fig. 1: GameDevLab “Invoke a Method” station. GameDevCola vending machine.

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.

Fig. 2: Script InvokeAMétodo sin completar.

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.

Here is the article that analyzes the OnTriggerStay method.

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.

Fig. 3: Diagram of events in time. This would happen if we called the methods directly.

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.

Fig. 4: Diagram of events in time. This is what we want to achieve.

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.

Solution

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.

Fig. 5: InvokeAMethod script completed.

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.

Fig. 6: When approaching the machine, a sign appears indicating that it is possible to interact.

Fig. 7: After a few seconds of interacting with the machine, we are given a can.

Conclusion

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.

Exit mobile version
Secured By miniOrange