Coin Distraction — Daily Progression Update

Create an event that passes information for subscribers to use

Jean-Noel Seneque
3 min readSep 23, 2021

In the last article, Using Events to Destroy all the Enemies, we saw good use of how events can let game objects know something has happened without the other knowing who is listening to the event.

In this article, I will expand on this by passing more information about the event to the subscriber, so now not only do they know something they are interested in has occurred but now can act on the information being passed to them. In this case, our main character will throw a coin to distract the guards and make them move to where the coin was thrown.

The pseudo-code would go like this:

Let’s get started. We will only give the character one coin to throw so let’s implement a flag to check if the coin has been thrown. In my PlayerController script, I created a private variable for this. By default, it is set to false.

Next, we check if the right mouse button has been clicked and call a function. Noticed that we also have a check to see if the coin has already been thrown.

This function does most of the heavy lifting, checks where the mouse was clicked, create a coin and noise at that position, and triggers off an event. The one extra piece we do for this event is passing the coin’s location, so anything listening for this event may use this information.

The event and argument have been declared at the top of the class as follows.

So with our event define and passing the coin’s position, let's go into the guard script and subscribe to the event.

First, we declare a reference to the player, a variable to store the coin position information, and a flag to determine if the coin has been thrown.

In the Awake method, let’s search for the player using its “Player” tag and subscribe to the event

The function that will be called when the event is triggered will flag the coin that has been thrown, store the coin position and unsubscribe to the event.

I have made a change to the Update method to check if the coin has been thrown so the guard stops moving between way-points but now moves to the coin’s position instead.

That’s it! The main takeaway from this article is that we can now create events that pass additional information to its subscribers that they can use.

If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.

--

--

Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com