Unity advanced or Awaitable promise components

Greetings! Today I would like to share my experience, which I created about two years ago and use in projects today.

I will make a reservation that everything that is described below is invented by me, so I did not steal the idea anywhere. Also, as a result of searching on the Internet, I did not find anything like that. If this is important for you – use it to your health =)

Everything described below will be based on the concept promise for a component-oriented approach – in simple words, in this case, we will create not a promise class, but a component.

And due to the fact that the year is 2023, we will make this promise awaitable following js.

I won’t stop at work async/await, quite a lot has already been written about this, not only on the official website, but also in many places on the Internet. Let us outline only the main points.

To ensure the operation of this mechanism, we are required in the type:

  • Implement INotifyCompletion

  • Add IsCompleted property (bool)

  • Add a GetResult method (you can return a login from it)

  • Implement the GetAwaiter method without parameters – returning the same type (can be done with an extension)

Also, our promise will be able to accept parameters.

For convenience in understanding how it works, I recommend that you familiarize yourself with my articles about forwarding dependencies into components with ten lines of very simple code

So, let’s begin. First, let’s describe the mechanism for accepting parameters in the component itself. In our case it will be like this:

Here we use statics to forward the parameters for new components in the same type. In general, the mechanism is very simple – we put our parameters into statics, and the component introduced into the environment will look into this statics during initialization. The beauty is in the Run method, we will need it in our promise component.

The next step is to implement INotifyCompletion and everything described above:

Very simple code. Let’s break it down a bit and make it clear. When we call the Run command (using await), we will add a bean that will be returned in the GetAwaiter method. Next, the IsCompleted property will be queried, followed by the INotifyCompletion OnCompleted method. It will be called before the execution of the main code, and its parameter is the point of further execution of the program – we will have to run it ourselves, so we will save it. After we have done everything we wanted, we need to destroy the component and continue the program execution. Let’s do this in the IsCompleted property setter.

Evil tongues will say: “But how to return the result?” And we will return the result to them:

That’s it, now we have everything we need to start the component through the await keyword! (And all this takes about 35 lines of code, not counting the spaces between the lines)

Let’s write our first awaitable component. For convenience and simplicity of the example, let’s create a timer that counts 5 seconds for us.

I’ll note right away that you can not only write awaitable timers, you can show popups, open scenes, etc. In such a notation, everything that can be expressed by the component can be done in await style.

We have an archetype, there are its specific child objects, it remains only to launch them. Let’s do it:

That’s all. The logs will be displayed at intervals of five seconds. I hope you liked it and this approach will find its application in your projects. Thank you for your attention!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *