Page 1 of 1

dialogue conditions with c# method

Posted: Fri Jul 17, 2020 12:29 pm
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?

Re: dialogue conditions with c# method

Posted: Fri Jul 17, 2020 12:58 pm
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.

Re: dialogue conditions with c# method

Posted: Fri Jul 17, 2020 2:11 pm
by alsoknownas-stefan
Tony, you are an absolute genius. :D Thank you very much for the help.

Re: dialogue conditions with c# method

Posted: Fri Jul 17, 2020 2:32 pm
by Tony Li
Glad to help! :-)