Access variable from c#
Access variable from c#
Hello
If I set a variable in Dialogue manager, how do get access to it from a C# script?
Thanks
If I set a variable in Dialogue manager, how do get access to it from a C# script?
Thanks
Re: Access variable from c#
Hi,
Use DialogueLua.GetVariable(). Example:
Use DialogueLua.GetVariable(). Example:
Code: Select all
using PixelCrushers.DialogueSystem; // (Put at top of script)
...
bool value = DialogueLua.GetVariable("My Variable").asBool;
Re: Access variable from c#
Hi Tony
I tried like this and attached it to an empty GameObject.
But i get this console error: shown image below
I tried like this and attached it to an empty GameObject.
But i get this console error: shown image below
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class getDialogueVariable : MonoBehaviour
{
bool value = DialogueLua.GetVariable("AnimatorPlay").asBool;
public GameObject mobileUI;
void Update()
{
if (value == true)
{
mobileUI.SetActive(false);
}
else if (value == false)
{
mobileUI.SetActive(true);
}
}
}
Re: Access variable from c#
Move this line:
into the Update method:
However, you shouldn't need to check the variable every frame. It's not efficient and may severely impact your framerate on mobile devices. It looks like this might be related to your other question about disabling GameObjects. If so, use that instead of this script.
Code: Select all
bool value = DialogueLua.GetVariable("AnimatorPlay").asBool;
Code: Select all
void Update()
{
bool value = DialogueLua.GetVariable("AnimatorPlay").asBool;
However, you shouldn't need to check the variable every frame. It's not efficient and may severely impact your framerate on mobile devices. It looks like this might be related to your other question about disabling GameObjects. If so, use that instead of this script.
Re: Access variable from c#
Thank you.
I get it would be a performance hit when in update. How would you check then in a C# scripts to see if the bool is true/false?
I get it would be a performance hit when in update. How would you check then in a C# scripts to see if the bool is true/false?
Re: Access variable from c#
You can do the check; just don't do it every single frame in Update if you can find another way.
Re: Access variable from c#
Ok thanks Tony.
I have a dialogue where NPC ask me if i have a key. I have set a variable gotKey for this.
But how do i actually when i get an item say the key set the gotKey variable to true?
I have a dialogue where NPC ask me if i have a key. I have set a variable gotKey for this.
But how do i actually when i get an item say the key set the gotKey variable to true?
Re: Access variable from c#
Since you're using Invector, don't use a gotKey variable. Use the special Invector Lua function vGetItemCount(). For example, say your Invector item has ID 15. Then your conversation might look something like:
- NPC Dialogue Text: "Do you have the key?"
- Player Dialogue Text: "Yes."
Conditions: vGetItemCount(15) > 0 - Player Dialogue Text: "No."
- Player Dialogue Text: "Yes."
Re: Access variable from c#
Thanks Tony.
How awesome the integration with Invector is so nicely crafted..
BTW:
With the lua method can I still in a conversation check for a condition to see the key is obtain?
How awesome the integration with Invector is so nicely crafted..
BTW:
With the lua method can I still in a conversation check for a condition to see the key is obtain?