problem persistence dialogue conditions c# method across scenes

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

problem persistence dialogue conditions c# method across scenes

Post by alsoknownas-stefan »

Hi there,

I am experiencing the following problem: I have registered the c# method GetItemQuantityTotal() as per instructions, so that I can use it in the dialogue system. I also have Quest machine installed and have integrated the two packages. I have 2 scenes with portals between them, set up just as instructed. I use the function to count the number of cubes in my inventory (using the name of the gameobject that I want to count) and to steer the dialogue based on that number. When I accept the quest in scene 1, collect an additional cube in scene 1 (I start with 2 cubes and I need 3 to advance the quest) and then talk again to the NPC, it returns me the correct number of cubes in the dialogue (which is 3 cubes) as well as the correct conversational dialogue line.

Now the scenario that goes wrong: I accept the quest in scene 1, I go to scene 2 using a portal, I collect a cube in scene 2, I return to scene 1 using a portal, then I get the wrong number of cubes (2 cubes) and the wrong conversational dialogue line. I have no idea why. I hooked up the method to a button/canvas in both scenes and it displays the correct number of cubes at every moment when pressed. The cubes/contents of my inventory persist nicely through both scenes. The scenes are identical, with the exception that the questgivernpc is not in scene 2.

Another scenario where it goes wrong: I collect a cube in scene 1, I go to scene 2 and collect a cube (total 3), I go to scene 1 and collect a cube (total 4), I go hand in my quest and lo and behold, the dialogue says I have 3 cubes (even though when I press the button on the canvas in scene 1, it says 4). I think this has to do with the registering of values/methods and that it only remembers what I have at the moment of scene transition? Which is odd, because I expect the registered method to just execute and note the number of cubes at the moment the method is called...

Extra info:
Adding/removing Dont destroy gameobject scripts on the Quest machine gameobject or the inventory does not seem to have any effect, by the way. The state of the quest (active, success etc.) persists nicely across scenes as do all other things integrated with the save system (animation state, position player).

Below is the script I use to register the method.

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

public class MyLuaFunctions : MonoBehaviour
{
public GameObject VRInventory;

void OnEnable()
{
Lua.RegisterFunction("GameObjectExists", this, SymbolExtensions.GetMethodInfo(() => GameObjectExists(string.Empty)));
//Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(string.Empty)));
//dit werkt niet
//post op forum: https://www.pixelcrushers.com/phpbb/vie ... 272#p18272

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
//Lua.RegisterFunction("GetItemQuantityTotal", this, SymbolExtensions.GetMethodInfo(() => GetItemQuantityTotal(string.Empty)));
//double GetItemQuantityTotal(string itemName)
//{
// return VRInventory.GetComponent<InventoryManager>().GetItemQuantityTotal(itemName);
//}
}



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;
}


}


The actual function looks like this:

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 have also added some screenshots.
Attachments
screenshot_3.png
screenshot_3.png (504.06 KiB) Viewed 425 times
screenshot_2.png
screenshot_2.png (281.38 KiB) Viewed 425 times
screenshot_1.png
screenshot_1.png (279.3 KiB) Viewed 425 times
alsoknownas-stefan
Posts: 29
Joined: Fri Jul 17, 2020 9:19 am

Re: problem persistence dialogue conditions c# method across scenes

Post by alsoknownas-stefan »

I have solved it! :D It seems that putting my register function script under the Dialogue system object did not work in my situation. This together with disabling the lines beneath had no effect.

Lua.UnregisterFunction("GameObjectExists"); // <-- Only if not on Dialogue Manager.
Lua.UnregisterFunction("GetItemQuantityTotal"); // <-- Only if not on Dialogue Manager.

What did work was putting my register function script under a gameobject other than the Dialogue system object and enabling above lines. I am leaving this solution here, in case anyone has the same problem in the future.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: problem persistence dialogue conditions c# method across scenes

Post by Tony Li »

Thanks for posting how to got it working.
Post Reply