Change print speed in generic lines

Share custom commands, script functions, actor implementations and other Naninovel plug-ins you've created.
Post Reply
Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Change print speed in generic lines

Post 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

Nysalie
Posts: 29
Joined: 24 Jun 2020 20:28

Re: Change print speed in generic lines

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

IdrilKalean
Posts: 4
Joined: 24 Sep 2020 10:09

Re: Change print speed in generic lines

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

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

Re: Change print speed in generic lines

Post by Elringus »

Just remove using System.Threading;.

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

Re: Change print speed in generic lines

Post 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;
    }
}
Kozzie
Posts: 1
Joined: 11 Mar 2023 08:18

Re: Change print speed in generic lines

Post 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

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

Re: Change print speed in generic lines

Post by Elringus »

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

Post Reply