Page 1 of 1

Access bool from own script

Posted: Wed Mar 09, 2022 4:52 pm
by hrohibil
Hello Toni

If I set a bool to to be true in one of m dialogues, how do i access this bool from my own custom script where I just ant to check if that bool is true then i do something in my script..

Thanks

Re: Access bool from own script

Posted: Wed Mar 09, 2022 4:59 pm
by Tony Li
Hi,

Use DialogueLua.GetVariable("variable-name").asBool:

Code: Select all

bool talkedToAdam = DialogueLua.GetVariable("TalkedToAdam").asBool;

Re: Access bool from own script

Posted: Wed Mar 09, 2022 5:15 pm
by hrohibil
Forgive me Toni.

I am a noob.

I pasted that code in the conversations sequence field, and conditions field but that did not help.
In variables I have a bool set to false called "gunMap".
In the conversation field script I have this currently:

Variable["gunMap"] = true

What i am trying to do is to enable a script or a gameObject when the bool is true....

So my two noob questions:

1:
Where and how i insert that code?

2:
And how do i reference to it from my own script?

Re: Access bool from own script

Posted: Wed Mar 09, 2022 8:44 pm
by Tony Li
hrohibil wrote: Wed Mar 09, 2022 5:15 pmIn the conversation field script I have this currently:

Variable["gunMap"] = true

What i am trying to do is to enable a script or a gameObject when the bool is true....
In the same dialogue entry node's Sequence field, you can use SetEnabled() to enable a script or SetActive() to activate a GameObject. See the Cutscene Sequence Tutorials and Sequence Command Reference for more info.
hrohibil wrote: Wed Mar 09, 2022 5:15 pm1: Where and how i insert that code?
2: And how do i reference to it from my own script?
Insert it where you want to check the value of the variable. I don't know the script, so I can't tell you any more than that. Here's an example:

Code: Select all

public class SomeScript : MonoBehaviour
{
    public void CheckVariableValue()
    {
        bool gunMap = DialogueLua.GetVariable("gunMap").asBool;
        Debug.Log("The current value of gunMap is: " + gunMap);
    }
}

Re: Access bool from own script

Posted: Thu Mar 10, 2022 2:37 am
by hrohibil
Thank you Toni..

That did it !!!

I am so happy right now :-)

Re: Access bool from own script

Posted: Thu Mar 10, 2022 5:09 am
by hrohibil
Oh and what about the other way around. Instead of GetVariable can I somehow setVariable ?

So if I pickup an item then I want to set a bool to true ?

Re: Access bool from own script

Posted: Thu Mar 10, 2022 8:02 am
by Tony Li
Yes. SetVariable():

Code: Select all

DialogueLua.SetVariable("gunMap", true);