Multiple speakers highlight

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

Multiple speakers highlight

Post by Elringus »

In some cases, when using speaker highlight feature, it could be required to highlight multiple characters at the same time (eg, when more than one character is the author of the printed message).

The following custom command handle such scenarios:

Code: Select all

using Naninovel;
using Naninovel.Commands;
using System.Collections.Generic;
using UniRx.Async;

[Documentation("Overrides speaker highlight feature for the specified characters and (optionally) applies tint to them.")]
public class ForceHighlight : Command
{
    [RequiredParameter, ParameterAlias(NamelessParameterAlias)]
    public StringListParameter CharacterIds;
    [RequiredParameter]
    public BooleanParameter Enable;
    public StringParameter Tint;
    public DecimalParameter Time = .35f;

    public override async UniTask ExecuteAsync (CancellationToken cancellationToken = default)
    {
        var charManager = Engine.GetService<ICharacterManager>();
        var tasks = new List<UniTask>();
        foreach (var id in CharacterIds)
        {
            var meta = charManager.Configuration.GetMetadataOrDefault(id);
            meta.HighlightWhenSpeaking = Enable;

            if (Assigned(Tint))
            {
                var command = new ModifyCharacter {
                    IdAndAppearance = new NamedString(id, null),
                    TintColor = Tint,
                    Duration = Time
                };
                tasks.Add(command.ExecuteAsync());
            }
        }

        await UniTask.WhenAll(tasks);
    }
}

It can be used as follows (given Highlight When Speaking is enabled for both "Yuko" and "Kohaku"):

Code: Select all

; Only the author of the message is highlighted.
Yuko: Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Kohaku: Aenean lobortis vestibulum aliquet.

; Temporary disable auto-highlight feature for both characters and tint them white.
@forceHighlight Kohaku,Yuko enable:false tint:white
Both: Phasellus non nisi posuere, consectetur dui id, luctus odio. 
; Restore auto-highlight feature.
@forceHighlight Kohaku,Yuko enable:true

; The chars are now highlighted as usual.
Yuko: Quisque eget velit at augue consequat viverra.
Kohaku: Duis ut laoreet urna. 
Post Reply