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?
dialogue conditions with c# method
-
- Posts: 29
- Joined: Fri Jul 17, 2020 9:19 am
dialogue conditions with c# method
- Attachments
-
- Dialogue_screenshot.png (308.19 KiB) Viewed 529 times
Re: dialogue conditions with c# method
Hi,
Change this line:
to this:
This way the script will look for the GetItemQuantityTotal method on the InventoryManager component instead of the script itself.
Alternatively:
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:
and click Go.
Or you can do the same in play mode or builds using a Lua Console.
Change this line:
Code: Select all
Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(string.Empty)));
Code: Select all
var inventoryManager = VRInventory.GetComponent<InventoryManager>();
Lua.RegisterFunction("GetItemQuantityTotal", inventoryManager, SymbolExtensions.GetMethodInfo(() => inventoryManager.GetItemQuantityTotal(string.Empty)));
Alternatively:
Code: Select all
Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => GetItemQuantityTotal(string.Empty)));
double GetItemQuantityTotal(string itemName)
{
return VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(itemName);
}
Code: Select all
return GetItemQuantityTotal("Cube")
Or you can do the same in play mode or builds using a Lua Console.
-
- Posts: 29
- Joined: Fri Jul 17, 2020 9:19 am
Re: dialogue conditions with c# method
Tony, you are an absolute genius. Thank you very much for the help.
Re: dialogue conditions with c# method
Glad to help!