Page 1 of 1

Minigames and puzzles

Posted: Sun Jun 23, 2024 12:26 pm
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

Re: Minigames and puzzles

Posted: Sun Jun 23, 2024 1:34 pm
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)

Re: Minigames and puzzles

Posted: Mon Jun 24, 2024 12:23 am
by AvivXR
Thank you!

Re: Minigames and puzzles

Posted: Mon Jun 24, 2024 8:00 am
by Tony Li
Glad to help!

Re: Minigames and puzzles

Posted: Mon Jun 24, 2024 1:21 pm
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!

Re: Minigames and puzzles

Posted: Mon Jun 24, 2024 1:50 pm
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.

Re: Minigames and puzzles

Posted: Tue Jul 09, 2024 4:53 am
by AvivXR
Thanks.
That worked perfectly.
Can I also pass variables from the conversation to my code?
i.e. "PlayGame(1)"

Re: Minigames and puzzles

Posted: Tue Jul 09, 2024 8:24 am
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.