On Dialogue Start#
Game Creator / Dialogue / On Dialogue Start
Description#
Executes when a Dialogue component starts playing
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
using VisualScript.Runtime;
namespace GameCreator.Runtime.Dialogue
{
[Title("On Dialogue Start")]
[Image(typeof(IconDialogue), ColorTheme.Type.Lime)]
[Description("Executes when a Dialogue component starts playing")]
[Category("Game Creator/Dialogue/On Dialogue Start")]
[Keywords("Dialogue", "Line", "Text", "Script", "Narration", "Begin")]
[Serializable]
public class TriggerDialogueDialogueStart : Trigger
{
// EXPOSED MEMBERS: -----------------------------------------------------------------------
[SerializeField] private TValueOrAny<GetGameObject> m_Dialogue =
new TValueOrAny<GetGameObject>(
ValueOrAny.Any,
new GetGameObject(new TypeGameObjectDialogue())
);
// MEMBERS: -------------------------------------------------------------------------------
[NonSerialized] private Guid m_SubscriptionDialogue;
[NonSerialized] private Dialogue m_CurrentDialogue;
// PROPERTIES: ----------------------------------------------------------------------------
protected override string GetTitle(GameObject source) =>
this.m_Dialogue.IsSpecific
? $"On Start {this.m_Dialogue}"
: "On Start any Dialogue";
// INITIALIZERS: --------------------------------------------------------------------------
public override void OnEnable()
{
base.OnEnable();
using Args args = Args.Get(this.Component, this.Component);
if (this.m_Dialogue.IsAny)
{
Dialogue.EventStart += OnStartAny;
}
else
{
this.m_SubscriptionDialogue = this.m_Dialogue.Value.Subscribe(this.OnChangeDialogue, args);
this.OnChangeDialogue(args);
}
}
public override void OnDisable()
{
base.OnDisable();
if (this.m_CurrentDialogue != null)
{
this.m_CurrentDialogue.EventDialogueStart -= this.OnStartDialogue;
}
Dialogue.EventStart -= OnStartAny;
this.m_Dialogue.Value.Unsubscribe(this.m_SubscriptionDialogue);
}
// CALLBACK METHODS: ----------------------------------------------------------------------
private void OnStartAny(Dialogue dialogue)
{
using Args args = Args.Get(this.Component.gameObject, dialogue);
this.Run(args);
}
private void OnChangeDialogue(Args args)
{
if (this.m_CurrentDialogue != null)
{
this.m_CurrentDialogue.EventDialogueStart -= this.OnStartDialogue;
}
this.m_CurrentDialogue = this.m_Dialogue.Value.Get<Dialogue>(args);
if (this.m_CurrentDialogue != null)
{
this.m_CurrentDialogue.EventDialogueStart += this.OnStartDialogue;
}
}
private void OnStartDialogue(Dialogue dialogue)
{
using Args args = Args.Get(this.Component.gameObject, dialogue);
this.Run(args);
}
}
}