How to DELAY a FUNCTION using INVOKE method

IMPORTANT UPDATE

This arti­cle belongs to an old series of the chan­nel, cur­rent­ly there is a video avail­able that explains bet­ter how to use the Invoke func­tion to make a func­tion be called with delay, you can watch it here:

🟢 How to DELAY a FUNCTION using INVOKE method

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Old Article

Introduction

In this arti­cle we are going to see how to use the Invoke method of Unity's MonoBe­hav­iour class to exe­cute an action with a cer­tain delay. This allows us for exam­ple to cre­ate mech­a­nisms 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 sum­ma­rize this arti­cle. Eng­lish Sub­ti­tles available.

Procedure

To analyse the Invoke method we'll be using the GameDevLab's "Invoke a Method" sta­tion, which is a vend­ing machine for the refresh­ing new GameDe­v­Co­la that not only quench­es thirst but also heals your wounds.

create games in unity, fundamental series, invoke method, program the operation of a vending machine
Fig. 1: GameDe­vLab "Invoke a Method" sta­tion. GameDe­v­Co­la vend­ing machine.

By approach­ing the machine we can inter­act with it using the E key. When you press it, the machine will become inac­tive, that is, you will not be able to inter­act with it for a while. After a few sec­onds the machine will eject a can of GameDe­v­Co­la and sec­onds lat­er will again be enabled for interaction.

script unity, ontriggerstay, invoke method. Development of games in Unity.
Fig. 2: Script InvokeAMéto­do sin completar. 

In the script to com­plete we have the meth­ods "Deliv­er­A­Co­la" and "Pre­pareMa­chine" that will deliv­er the drink and enable the machine respectively.

Then we have rede­fined the OnTrig­ger­Stay method that allows us to check if the char­ac­ter is stand­ing in front of the machine.

Here is the arti­cle that ana­lyzes the OnTrig­ger­Stay method.

With­in this method we check if the Tag of the GameOb­ject is that of the play­er, then if the machine is ready (boolean machineReady), we tell the UIMan­ag­er object that can inter­act and this is respon­si­ble for dis­play­ing the mes­sage on screen.

Then we check if the E key is pressed. At this point the machine receives a request for a GameDe­v­Co­la. The first thing you do is deac­ti­vate the machine and then in the region with com­ments is where we must com­plete the miss­ing code.

Analysis of events in time

I want to empha­size this part because it is impor­tant to under­stand the idea.

When we call a method in C#, the exe­cu­tion of it is done at that pre­cise instant, all instruc­tions are solved even before refresh­ing the screen image.

So if we called the meth­ods direct­ly in the region marked with com­ments, what would hap­pen is that the can would be expelled and the machine enabled again in the same instant in which the E key is pressed. As illus­trat­ed in the time­line in fig­ure 3.

a timeline specifying four points at the time of execution of a video game
Fig. 3: Dia­gram of events in time. This would hap­pen if we called the meth­ods directly. 

Fig­ure 3 shows a time­line in which there are two dif­fer­ent points in time, the first point being the begin­ning of the game. Some­time lat­er in time is the sec­ond point, in which the play­er asks for a can, the machine deliv­ers it and it is enabled again, these three actions at the same time.

a timeline specifying four points at the time of execution of a video game
Fig. 4: Dia­gram of events in time. This is what we want to achieve.

Fig­ure 4 shows four dif­fer­ent time points.

Point 1: Start of the game.

Point 2: The play­er orders the GameDevCola. 

Point 3: X mil­lisec­onds after the order is placed, the machine deliv­ers the can.

Point 4: After Y mil­lisec­onds of can deliv­ery, the machine is enabled again.

This is what we are going to do using the Invoke method of the MonoBe­hav­iour class.

Solution

The Invoke method can be called direct­ly with­out ref­er­ence to a class. 

We must indi­cate two para­me­ters, the first is a String with the name of the method we want to invoke, the sec­ond para­me­ter is a float to indi­cate the num­ber of sec­onds that will pass until the method is final­ly executed.

Upper and low­er case must be respect­ed. If we get con­fused at this point, a bug may arise that is dif­fi­cult to track. If the method is called "Deliv­er­A­Co­la" and we invoke the "Deliv­er­a­Co­la" method what will hap­pen is that we will try to call that method but as it is not defined, the pro­gram will go ahead with­out exe­cut­ing those instructions.

script unity, ontriggerstay, invoke method. Development of games in Unity.
Fig. 5: InvokeAMethod script completed.

Fig­ure 5 shows the res­o­lu­tion for the exer­cise. The can will be deliv­ered two sec­onds after the play­er press­es the E key and the machine is re-enabled four sec­onds later.

create games in unity, fundamental series, invoke method, program the operation of a vending machine. It shows a sign indicating that you can interact with the vending machine.
Fig. 6: When approach­ing the machine, a sign appears indi­cat­ing that it is pos­si­ble to interact.

create games in unity, fundamental series, invoke method, program the operation of a vending machine, machine delivers a can.
Fig. 7: After a few sec­onds of inter­act­ing with the machine, we are giv­en a can.

Conclusion

The Invoke method is used to post­pone the exe­cu­tion of a method. Although it is not the only way to achieve this, it is a sim­ple resource, easy to remem­ber and easy to implement.

Scroll to Top
Secured By miniOrange