Change print speed in generic lines

Posted: 21 Jul 2020 13:03
by Elringus

Implemented in v1.20

With Naninovel 1.20 and later changing speed in generic line is possible out of the box with generic parameters: https://pre.naninovel.com/guide/naninov ... parameters


For Naninovel v1.19 or lower

This modification will allow changing reveal speed right inside generic text lines, eg:

Code: Select all

Kohaku: [s 0.1] This text is printed ten times slower than usual. [s 2] This is printed twice as fast than usual.
Yuko: This text is printed normally.

Copy paste the code below to a new csharp script stored anywhere inside the Unity project:

Code: Select all

using Naninovel;
using Naninovel.Commands;
using UniRx.Async;

[InitializeAtRuntime]
public class ChangeNextRevealSpeedService : IStatefulService<GameStateMap>
{
    [System.Serializable]
    class GameState { public float Modifier; }

    public float NextRevealSpeedMofifier { get; set; }

    public UniTask InitializeServiceAsync () => UniTask.CompletedTask;

    public void ResetService () => NextRevealSpeedMofifier = 1;

    public void DestroyService () { }

    public void SaveServiceState (GameStateMap stateMap)
    {
        var state = new GameState() { Modifier = NextRevealSpeedMofifier };
        stateMap.SetState(state);
    }

    public UniTask LoadServiceStateAsync (GameStateMap stateMap)
    {
        NextRevealSpeedMofifier = stateMap.GetState<GameState>()?.Modifier ?? 1;
        return UniTask.CompletedTask;
    }
}

[CommandAlias("s")]
public class ChangeNextRevealSpeedCommand : Command
{
    [ParameterAlias(NamelessParameterAlias), RequiredParameter]
    public DecimalParameter Modifier;

    public override UniTask ExecuteAsync (CancellationToken cancellationToken = default)
    {
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier = Modifier;
        return UniTask.CompletedTask;
    }
}

[CommandAlias("print")]
public class MyCustomPrintCommand : PrintText
{
    protected override float AssignedRevealSpeed => base.AssignedRevealSpeed * 
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier;

    public override async UniTask ExecuteAsync (CancellationToken cancellationToken = default)
    {
        await base.ExecuteAsync(cancellationToken);
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier = 1f;
    }
}

Then re-import any existing naninovel script assets (right-click over a folder they're stored at, then click "Reimport") in order for the changes to take effect.

For more information on custom commands and engine services see the guide:
https://naninovel.com/guide/custom-comm ... om-command
https://naninovel.com/guide/engine-serv ... m-services


Re: Change print speed in generic lines

Posted: 26 Jul 2020 14:58
by Nysalie

Cool command, thanks for sharing! This is also a nice example on how to serialize gamestates. I've only used them by inheriting CustomUI but this makes it very clear if a more independent solution is needed.


Re: Change print speed in generic lines

Posted: 26 Sep 2020 10:25
by IdrilKalean

error CS0104: 'CancellationToken' is an ambiguous reference between 'Naninovel.CancellationToken' and 'System.Threading.CancellationToken':

line:
public override async UniTask ExecuteAsync (CancellationToken cancellationToken = default)
put:
public override async UniTask ExecuteAsync (Naninovel.CancellationToken cancellationToken = default)

line:
public override UniTask ExecuteAsync (CancellationToken cancellationToken = default)
put:
public override UniTask ExecuteAsync (Naninovel.CancellationToken cancellationToken = default)


Re: Change print speed in generic lines

Posted: 26 Sep 2020 10:26
by Elringus

Just remove using System.Threading;.


Re: Change print speed in generic lines

Posted: 25 Jun 2022 09:09
by idaot

Updated version compatible with 1.17+:

Code: Select all

using Naninovel;
using Naninovel.Commands;

[InitializeAtRuntime]
public class ChangeNextRevealSpeedService : IStatefulService<GameStateMap>
{
    [System.Serializable]
    class GameState { public float Modifier; }

    public float NextRevealSpeedMofifier { get; set; }

    public UniTask InitializeServiceAsync() => UniTask.CompletedTask;

    public void ResetService() => NextRevealSpeedMofifier = 1;

    public void DestroyService() { }

    public void SaveServiceState(GameStateMap stateMap)
    {
        var state = new GameState() { Modifier = NextRevealSpeedMofifier };
        stateMap.SetState(state);
    }

    public UniTask LoadServiceStateAsync(GameStateMap stateMap)
    {
        NextRevealSpeedMofifier = stateMap.GetState<GameState>()?.Modifier ?? 1;
        return UniTask.CompletedTask;
    }
}

[CommandAlias("s")]
public class ChangeNextRevealSpeedCommand : Command
{
    [ParameterAlias(NamelessParameterAlias), RequiredParameter]
    public DecimalParameter SpeedModifier;

    public override UniTask ExecuteAsync(AsyncToken asyncToken = default)
    {
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier = SpeedModifier;
        return UniTask.CompletedTask;
    }
}

[CommandAlias("print")]
public class MyCustomPrintCommand : PrintText
{
    protected override float AssignedRevealSpeed => base.AssignedRevealSpeed *
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier;

    public override async UniTask ExecuteAsync(AsyncToken asyncToken = default)
    {
        await base.ExecuteAsync(asyncToken);
        Engine.GetService<ChangeNextRevealSpeedService>().NextRevealSpeedMofifier = 1f;
    }
}

Re: Change print speed in generic lines

Posted: 11 Mar 2023 08:20
by Kozzie

Main question (because its drivin me crazy), is there a way to have the vscode extension not read this as an error?

Secondly, will this ever be fully implemented into NN outside of print? I'd love to just have native support of a crucial feature like this
I did use the 1.17 update posted a few months ago


Re: Change print speed in generic lines

Posted: 11 Mar 2023 10:24
by Elringus

Here is the guide on how to make IDE extension aware of custom commands: https://naninovel.com/guide/ide-extensi ... e-metadata