Create a search and Callback System — part 4 — The Actual Callback Part.

Before we get into the callback section, let’s put our successfully downloaded case into our apps activeCase:

UIManager.Instance.activeCase = downloadedCase;

Let’s search for case 329 and see if everything populates correctly.

Everything I entered is there! Great success!

So on to our Callback system: What is a callback? According to Unity answers:

A callback is a general term to say a method is meant to be called back whenever something happens. In C#, Events, Actions, and in Unity, UnityEvents, all allow to create callbacks.

In our case, we want a method to be run when the GetList function has successfully downloaded a case and the method will activate the next panel.

Currently, our GetList command only has one parameter, but remember our AWSManager now has an onComplete action that requires a method.

What if there were a way to pass that method in a parameter? That’s where delegates come in. Delegates allow you to treat a variable like a function. Placing a delegate in as a parameter is called a callback system.

Let’s go to our AWSManager; We’re going to modify the Getlist function to accept a callback.

We pass an action along with our caseNumber string. An action is just a delegate that allows us to pass information. We want to know when the files were loaded, so we call our Action “onComplete”, we can even set it to null though we’ll be assigning a method to it later.

At the bottom of the function where we have successfully downloaded and parsed our file, we can include an onComplete() function to trigger the action in our GetList parameters.

Back in the search panel, we’ll prepare the select panel to turn on once the file is loaded, and normally we’de set it active in the function, but now, we’ll do it immediately inside of the GetList function and we do this using a lambda.

a lambda is usually formated parenthesis, an equals and greater than, and then brackets for a function. () => {}.

So let’s pass in an empty lambda that will be assigned to onComplete:

If all goes well, if a case file isn’t found, nothing will happen, but if it is, it will turn on the select panel:

Searching for 000 gets us nothing, of course.

Searching for case 329 which is filled will bring us to the next panel!

We haven’t populated the information, but that’s an assignment for another day, for now we have successfully created our callback system!

cartoonist, game artist, and wanabe gamedev.