Using Wait command in custom command

Using Naninovel C# APIs: adding custom actor implementations or commands, overriding engine services, integrating with other systems, etc.
Post Reply
juan.delcastillo
Posts: 30
Joined: 02 Feb 2021 19:11

Using Wait command in custom command

Post 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?

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Using Wait command in custom command

Post by Elringus »

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

Elringus
admin
Posts: 521
Joined: 11 May 2020 18:03

Re: Using Wait command in custom command

Post by Elringus »

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

juan.delcastillo
Posts: 30
Joined: 02 Feb 2021 19:11

Re: Using Wait command in custom command

Post 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

juan.delcastillo
Posts: 30
Joined: 02 Feb 2021 19:11

Re: Using Wait command in custom command

Post 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?

idaot
support
Posts: 262
Joined: 01 Aug 2020 08:25

Re: Using Wait command in custom command

Post 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.

Post Reply