Change print speed in generic lines

Share custom commands, script functions, actor implementations and other Naninovel plug-ins you've created.
Forum rules
Please be aware, that we're not providing official support via forum — it's entirely community-driven; see the support page for more information.
Post Reply
User avatar
Elringus
Admin
Posts: 162
Joined: Mon May 11, 2020 6:03 pm

Change print speed in generic lines

Post by Elringus »

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: Wed Jun 24, 2020 8:28 pm

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.

User avatar
IdrilKalean
Posts: 4
Joined: Thu Sep 24, 2020 10:09 am

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)

User avatar
Elringus
Admin
Posts: 162
Joined: Mon May 11, 2020 6:03 pm

Re: Change print speed in generic lines

Post by Elringus »

Just remove using System.Threading;.

Post Reply