Using Wait command in custom command

Posted: 24 May 2021 18:39
by juan.delcastillo

I know I can use Wait on a C# script by creating an instance and then using ExecuteAsync(). However, I would like to use it on an custom command. If i call the command inside the ExecuteAsync method of my custom command, I cannot use it properly. The script does not have any errors and yet Wait seems to be ignored. Here's the code:

Code: Select all

using Naninovel;
using Naninovel.Commands;
using UniRx.Async;
using UnityEngine;
[CommandAlias("disableObject")]
public class DisableObject : Command
{
    public StringParameter objectName;
    GameObject gameObject;
    Collider collider;
    Animator animator;
    Wait waitCommand;
    public StringParameter waitTime;
    public override UniTask ExecuteAsync(CancellationToken cancellationToken = default)
    {
        gameObject = GameObject.Find(objectName);
        animator = gameObject.GetComponent<Animator>();
        animator.Play("FadeObject");
        waitCommand = new Wait();
        waitCommand.WaitMode = waitTime;
        await waitCommand.ExecuteAsync();
        collider = gameObject.GetComponent<Collider>();
        collider.enabled = false;
        return UniTask.CompletedTask;
    }
}

In line 21, the program throws a warning:
warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

However, putting await doesn't work either because await can only be used in a asynchronous method. I then tried to create all the Wait logic on a separate function called Wait() and set it as async

Code: Select all

    public override UniTask ExecuteAsync(CancellationToken cancellationToken = default)
    {
        gameObject = GameObject.Find(objectName);
        animator = gameObject.GetComponent<Animator>();
        animator.Play("FadeObject");
        WaitFunction();
        collider = gameObject.GetComponent<Collider>();
        collider.enabled = false;
        return UniTask.CompletedTask;
    }
    async void WaitFunction()
    {
        waitCommand = new Wait();
        waitCommand.WaitMode = waitTime;
        await waitCommand.ExecuteAsync();
    }

The object is deactivated but there is no wait time. Any tips?


Re: Using Wait command in custom command

Posted: 24 May 2021 19:13
by Elringus

Just await the time directly, no need to use the wait command. See the wait command implementation for example.


Re: Using Wait command in custom command

Posted: 24 May 2021 19:14
by Elringus

Also, not sure why waitTime param is string and what "waitCommand.WaitMode = waitTime" suppose to mean.


Re: Using Wait command in custom command

Posted: 24 May 2021 19:22
by juan.delcastillo
Elringus wrote: 24 May 2021 19:14

Also, not sure why waitTime param is string and what "waitCommand.WaitMode = waitTime" suppose to mean.

WaitMode is a StringParameter so I declared waitTime as such. I thought that waitMode was the way the wait command works, as in the wait command https://naninovel.com/api/#wait

So I understood the waiting time needs to be passed as a string


Re: Using Wait command in custom command

Posted: 24 May 2021 21:05
by juan.delcastillo
Elringus wrote: 24 May 2021 19:13

Just await the time directly, no need to use the wait command. See the wait command implementation for example.

Where can I find this implementation example?


Re: Using Wait command in custom command

Posted: 25 May 2021 10:10
by idaot

You can find out how the Wait command works by checking out Wait.cs. You are correct in that the command parameter for Wait is a string type, by awaiting you can use float directly without parsing.