Minigames and puzzles

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
AvivXR
Posts: 19
Joined: Wed Jun 19, 2024 4:17 pm

Minigames and puzzles

Post by AvivXR »

Hi
New here.
I am creating a visual novel - based on the demo scene.
The conversation has a continue button to move forward.

I want to add interactive elements and minigames inside the conversation.
i.e. npc says: try to hack the code and then a minigame should appear. at the end of the minigame I want to continue the conversation.
Btw I am using scene events to call for the games to start, but the conversation is still happening regardless.
What is the best way to set it up?
Thank you
Aviv
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Minigames and puzzles

Post by Tony Li »

Hi Aviv,

Set the dialogue entry's Sequence to:

Code: Select all

Continue()@Message(FinishedMinigame)
When your minigame is done, use the C# method:

Code: Select all

PixelCrushers.DialogueSystem.Sequencer.Message("FinishedMinigame");

Some other options/variations:
  • If you want to make the player click continue before starting the minigame, and hide the dialogue UI while playing the minigame, add an empty dialogue entry after the "try to hack" entry, and set its Sequence to something like:

    Code: Select all

    SetDialogueUI(false);
    required SetDialogueUI(true)@Message(FinishedMinigame);
    Continue()@Message(FinishedMinigame)
  • An alternative to using a scene event is to write a custom sequencer command that waits until the minigame is done. Then you could set the Sequence to something like:

    Code: Select all

    PlayHackingMinigame()->Message(FinishedMinigame);
    Continue()@Message(FinishedMinigame)
AvivXR
Posts: 19
Joined: Wed Jun 19, 2024 4:17 pm

Re: Minigames and puzzles

Post by AvivXR »

Thank you!
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Minigames and puzzles

Post by Tony Li »

Glad to help!
AvivXR
Posts: 19
Joined: Wed Jun 19, 2024 4:17 pm

Re: Minigames and puzzles

Post by AvivXR »

One more question regarding this.
A Game Manager object handles all the mini games. I am using the Scnene Events in the dialogue entry to call them.
It worked well as long as I had one scene. I implemented the Visual Novel structure that contains two scenes (Start and Gameplay) and it seems the events are not registering.
All the games are in the template canvas under the Dialogue Manager (in both scenes)

My question is, basically - how do I run a C# method from the dialogue (i.e. GameManager.StartPuzzle())?

Thank you!
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Minigames and puzzles

Post by Tony Li »

Hi,

This is one of the reasons for registering C# methods with Lua or using (custom) sequencer commands. They're not tied to a specific scene. This page has an explanation of the pitfalls of using UnityEvents when going across scenes: How To: Manage Player Controls and Scene Changes.

Say your GameObject is a singleton with a C# method PlayMinigame1(), like:

Code: Select all

public class GameManager : MonoBehaviour
{
    public static GameManager Instance {get; private set;} = null;
    void Awake()
    {
        if (Instance == null) 
        {
            Instance = this; 
            DontDestroyOnThis(this.gameObject);
        }
        else
        {
            Destroy(this.gameObject);
        }
    }
    
    public void PlayMinigame1() { ... }
You could register PlayMinigame1() with Lua:

Code: Select all

public class GameManager : MonoBehaviour
{
    public static GameManager Instance {get; private set;} = null;
    void Awake()
    {
        if (Instance == null) 
        {
            Instance = this; 
            DontDestroyOnThis(this.gameObject);
            Lua.RegisterFunction(nameof(PlayMinigame1), this,  SymbolExtensions.GetMethodInfo(() => PlayMinigame1()));
        }
        else
        {
            Destroy(this.gameObject);
        }
    }
    
    public void PlayMinigame1() { ... }
and then call PlayMinigame1() in the Script field of a dialogue entry.
AvivXR
Posts: 19
Joined: Wed Jun 19, 2024 4:17 pm

Re: Minigames and puzzles

Post by AvivXR »

Thanks.
That worked perfectly.
Can I also pass variables from the conversation to my code?
i.e. "PlayGame(1)"
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Minigames and puzzles

Post by Tony Li »

Hi,

Yes. Use the DialogueLua class to access Dialogue System (Lua) variables in your C# code:

Code: Select all

int score = DialogueLua.GetVariable("Score").asInt;
To give your dialogue database content, such as Conditions and Script fields, access to your C# variables, see Registering Lua Functions and How To: Connect C# Variables to Dialogue System's Lua.
Post Reply