Hello, I'm currently working on building a 2D RPG and need some help with working with the "Dialogue System" asset by PixelCrushers.
Right now I'm trying to make it so whenever a conversation starts, my player's stats (strength, intelligence, etc.) are synced with the dialogue database in the form of variables. I want to do this so that I can change the flow of conversations based on my players current stats.
At first I thought I could use the "onExecute" section (inspecting the first dialogue entry in a conversation tree) to run a public method from StatsManager that manipulates the variables in the database to reflect what my player's stats are at the point that the conversation started. I guess I don't undersNox VidMate Mobdro
tand how the "onExecute" section works since when I tried dragging my StatsManager.cs Another thing, I tried looking at PixelCrusher's documentation to figure how to do manipulation of variables in the dialogue database and got kinda lost in it all. Any help on how to do that as well would be appreciated.
Help with Dialogue System from asset store.
-
- Posts: 2
- Joined: Sat Mar 09, 2019 4:31 am
Help with Dialogue System from asset store.
Last edited by Jacobcullen on Sat Mar 16, 2019 3:09 pm, edited 1 time in total.
Re: Help with Dialogue System from asset store.
Hi,
Thanks for using the Dialogue System!
The OnExecute() event typically needs to point to an asset in your project such as a prefab or ScriptableObject asset. It's probably not the best feature for your needs.
Instead, the two common approaches to this are:
1. A script with OnConversationStart and OnConversationEnd methods. (more info)
2. Or a script that registers Lua functions. (more info)
Thanks for using the Dialogue System!
The OnExecute() event typically needs to point to an asset in your project such as a prefab or ScriptableObject asset. It's probably not the best feature for your needs.
Instead, the two common approaches to this are:
1. A script with OnConversationStart and OnConversationEnd methods. (more info)
Spoiler
Add a script like this to your Dialogue Manager or player:
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class SyncStatsToDialogueSystem : MonoBehaviour
{
void OnConversationStart(Transform other)
{
// When a conversation starts, copy stats to Dialogue System variables:
var statsManager = FindObjectOfType<StatsManager>();
DialogueLua.SetVariable("Strength", statsManager.strength);
DialogueLua.SetVariable("Intelligence", statsManager.intelligence);
}
void OnConversationEnd(Transform other)
{
// When a conversation ends, copy stats from Dialogue System variables:
var statsManager = FindObjectOfType<StatsManager>();
statsManager.strength = DialogueLua.GetVariable("Strength").asInt;
statsManager.intelligence = DialogueLua.GetVariable("Intelligence").asInt;
}
}
2. Or a script that registers Lua functions. (more info)
Spoiler
The advantage of this method is that the stats change immediately, rather than only at the start and end of conversations. That may or may not be important to you. You can find a starter template script named TemplateCustomLua.cs in the Templates/Scripts folder. Here's an example that adds functions that you can use in a dialogue entry node's Conditions and/or Script field like:
- Dialogue Text: "You look strong. Can you help me lift this?"
- Conditions: GetStats("Strength") > 15
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class StatsLua : MonoBehaviour // Add to Dialogue Manager.
{
void Awake()
{
// Make the functions available to Lua:
Lua.RegisterFunction("GetStat", this, SymbolExtensions.GetMethodInfo(() => DebugLog(string.Empty)));
Lua.RegisterFunction("SetStat", this, SymbolExtensions.GetMethodInfo(() => DebugLog(string.Empty, (double)0)));
}
public double GetStat(string statName) // Given a stat name, return its value.
{
var statsManager = FindObjectOfType<StatsManager>();
switch (statName)
{
case "Strength": return statsManager.strength;
case "Intelligence": return statsManager.intelligence;
default: return 0;
}
}
public void SetStat(string statName, double value) // Set a stat value by name.
{
var statsManager = FindObjectOfType<StatsManager>();
switch (statName)
{
case "Strength": statsManager.strength = (int)value; break;
case "Intelligence": statsManager.intelligence = (int) value; break;
}
}
}