dialogue conditions with c# method

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
alsoknownas-stefan
Posts: 29
Joined: Fri Jul 17, 2020 9:19 am

dialogue conditions with c# method

Post by alsoknownas-stefan »

Hi there,

I recently bought your wonderful product and I am in the process of integrating it with other systems in my project.
My inventory system has a method: GetItemQuantityTotal. I use this to check whether or not an item is in the inventory based on the item name (name of gameobject). I want to steer the dialogue, depending whether or not an item is in my inventory.

I have been looking at this documentation page: http://pixelcrushers.com/dialogue_syste ... erFunction

I am working my way through the example that is provided.

public class MyLuaFunctions : MonoBehaviour {

void OnEnable() {
Lua.RegisterFunction("GameObjectExists", this, typeof(MyLuaFunctions).GetMethod("GameObjectExists"));
}

void OnDisable() {
Lua.UnregisterFunction("GameObjectExists");
}

public bool GameObjectExists(string name) {
return GameObject.Find(name) != null;
}

And that works great. I can use [lua(GameObjectExists("NPC"))] in the dialogue text to show the result in the conversation, true if the gameobject named NPC exists in my scene. In the conditions I can use GameObjectExists("NPC") == true or GameObjectExists("NPC") ~= true to steer the conversation.

I now want to implement the method GetItemQuantityTotal.

using UnityEngine;
using PixelCrushers.DialogueSystem;
using Epitybe.VRInventory;

public class MyLuaFunctions : MonoBehaviour
{
public GameObject VRInventory; //in the inspector I put the gameobject named VRInventory in the required slot.
//On this gameobject is my InventoryManager.cs script that contains my GetItemQuantityTotal method.

void OnEnable()
{
Lua.RegisterFunction("GameObjectExists", this, SymbolExtensions.GetMethodInfo(() => GameObjectExists(string.Empty)));
Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(string.Empty)));
}

void OnDisable()
{
// Note: If this script is on your Dialogue Manager & the Dialogue Manager is configured
// as Don't Destroy On Load (on by default), don't unregister Lua functions.
Lua.UnregisterFunction("GameObjectExists"); // <-- Only if not on Dialogue Manager.
Lua.UnregisterFunction("GetItemQuantityTotal"); // <-- Only if not on Dialogue Manager.
}

public bool GameObjectExists(string name)
{
return GameObject.Find(name); //origineel return GameObject.Find(name) != null;
}
}

Unfortunately, this does not seem to work. Neither [lua(GetItemQuantityTotal("Cube"))], nor GetItemQuantityTotal("Cube") in the dialogue text
shows me the number of cubes in my inventory.

Below is the code in InventoryManager.cs. I read that lua only takes double and not int. So, I hope I casted it correctly.
I hooked up the method to a button and a text on a canvas, so that it shows me the value that is returned when I click the button. It returns the correct number of gameobjects with the name Cube in my inventory (namely 1 cube).


public double GetItemQuantityTotal(string name)
{
int total = 0;
double total2 = 0;
for (int i = 0; i < slots.Length; i++)
{
if (null == slots.inventoryData.item)
{
continue;
}

if (slots.inventoryData.item.displayPrefab.name == name)
{
total += slots.inventoryData.count;
total2 = (double)total;
}
}
return total2;
}


I am baffled. Any suggestions?
Attachments
Dialogue_screenshot.png
Dialogue_screenshot.png (308.19 KiB) Viewed 526 times
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: dialogue conditions with c# method

Post by Tony Li »

Hi,

Change this line:

Code: Select all

Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(string.Empty)));
to this:

Code: Select all

var inventoryManager = VRInventory.GetComponent<InventoryManager>();
Lua.RegisterFunction("GetItemQuantityTotal", inventoryManager, SymbolExtensions.GetMethodInfo(() => inventoryManager.GetItemQuantityTotal(string.Empty)));
This way the script will look for the GetItemQuantityTotal method on the InventoryManager component instead of the script itself.

Alternatively:

Code: Select all

Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => GetItemQuantityTotal(string.Empty)));

double GetItemQuantityTotal(string itemName)
{
    return VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(itemName);
}
A quick way to test it is to open the Dialogue Editor window during play. Go to the Templates tab. In the entry field at the bottom, enter:

Code: Select all

return GetItemQuantityTotal("Cube")
and click Go.

Or you can do the same in play mode or builds using a Lua Console.
alsoknownas-stefan
Posts: 29
Joined: Fri Jul 17, 2020 9:19 am

Re: dialogue conditions with c# method

Post by alsoknownas-stefan »

Tony, you are an absolute genius. :D Thank you very much for the help.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: dialogue conditions with c# method

Post by Tony Li »

Glad to help! :-)
Post Reply