Skip to content

Instantly share code, notes, and snippets.

@alijaya
Last active February 13, 2022 17:20
Show Gist options
  • Save alijaya/6cadff054131423b241d12533766cedb to your computer and use it in GitHub Desktop.
Save alijaya/6cadff054131423b241d12533766cedb to your computer and use it in GitHub Desktop.
Yarn Return Jump Command
title: Start
---
Hello, this is a trial for callback stack
-> Yes
<<push SomeNode Start_1>>
-> No
Some spill over
===
title: Start_1
---
This is the continuation
It's kinda hacky but why not
Want to try it again?
-> Yes
<<push SomeNode Start_2>>
-> No
Okay then
<<jump Start_2>>
===
title: Start_2
---
Want to do some multiple jump?
-> Yes
<<push SomeJump Start_3>>
-> No
<<jump Start_3>>
===
title: Start_3
---
This is the end
===
title: SomeNode
---
This is some node that asked something
Do you want to go back?
-> Yes
<<pop>>
-> No
Some spill over
===
title: SomeJump
---
Jump Again?
-> Yes
<<push SomeJumpAgain SomeJump_1>>
-> No
<<jump SomeJump_1>>
===
title: SomeJump_1
---
End of Jump
<<pop>>
===
title: SomeJumpAgain
---
This is another jump.
Going back now.
<<pop>>
===

This is a workaround about returning call. There are two new command:

  • <<push toNode returnNode>>
  • <<pop>>

Push Command

Basically it's the same as jump. It will jump to toNode, and push the returnNode to a stack.

Pop Command

It will pop the top of a stack and jump into that node.

Usage

Put YarnReturnjump.cs as a Component along side with DialogueRunner.

Caution

Please don't mix a <<jump>> and <<push>> with a same node. If that node expect something to be popped, it will behave strangely if you don't push something to the stack.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yarn.Unity;
public class YarnReturnJump: MonoBehaviour
{
private DialogueRunner dialogueRunner;
private Stack<string> stack = new Stack<string>();
public void Awake()
{
dialogueRunner = GetComponent<DialogueRunner>();
if (!dialogueRunner) return;
dialogueRunner.AddCommandHandler<string, string>("push", pushHandler);
dialogueRunner.AddCommandHandler("pop", popHandler);
}
private void pushHandler(string node, string returnNode)
{
stack.Push(returnNode);
StartCoroutine(JumpNextFrame(node));
}
private void popHandler()
{
StartCoroutine(JumpNextFrame(stack.Pop()));
}
private IEnumerator JumpNextFrame(string node)
{
// need to wait another frame, so this will execute after the command handler is finished
yield return null;
dialogueRunner.Stop();
dialogueRunner.StartDialogue(node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment