Page 1 of 1

C# bool to lua?

Posted: Mon Feb 15, 2021 8:24 am
by buhubuhu
I'm not very experienced in coding. Need some help.
I have player, player have script and bool in it.
And now I want - when this bool is true or false converstation with npc starts with different sentence.

This whole lua thing is very confusing for me and I don't understand how can I do that - sorry if that is a basic question. :|

Re: C# bool to lua?

Posted: Mon Feb 15, 2021 11:03 am
by Tony Li
Hi,

Write a C# method that returns the value of your bool. Example:

Code: Select all

public bool MyBool;

public bool GetMyBoolValue()
{
    return MyBool;
}
This video tutorial shows how to make your method (GetMyBoolValue) available to Lua:



Then use it in the Conditions fields of your dialogue entry nodes:

Node 1:
  • Dialogue Text: "MyBool is false."
  • Conditions: GetMyBoolValue() == false
Node 2:
  • Dialogue Text: "MyBool is true."
  • Conditions: GetMyBoolValue() == true

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 10:28 am
by buhubuhu
Sorry for long break... I had to deal with different problems and now I comeback to this one.

So... I guess that I'm to stupid. I understand how to do it on dialog tab side but I have no idea how I suppose to register this. I already registered string and int (its in tutorial so I just follow it) but now I really need bool and have no idea how to do it:

Code: Select all

 public bool isFollowing;

    public bool GetIsFollowing()
    {
        return isFollowing;
    }
    private void OnEnable()
    {
        Lua.RegisterFunction("GetIsFollowing", this, SymbolExtensions.GetMethodInfo(() => GetIsFollowing()));
        
    }
    private void OnDisable()
    {
        Lua.UnregisterFunction("GetIsFollowing");
        
    }
Its obviously not working... How it should look?

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 10:42 am
by Tony Li
Hi,

What's not working?

Can you try using the starter template script? It's Assets / Plugins / Pixel Crushers / Dialogue System / Templates / Scripts / TemplateCustomLua.cs

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 10:45 am
by buhubuhu
I got this error:
Dialogue System: Lua code 'return GetIsFollowing() == false' threw exception 'Tried to invoke a function call on a non-function value. If you're calling a function, is it registered with Lua?'

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 10:49 am
by Tony Li
That means the function isn't registered. Two possible reasons the function may not be registered:

1. The script isn't on an active GameObject in the scene.

2. Or you put it on a Don't Destroy On Load GameObject such as the Dialogue Manager but didn't add the extra "didIRegister" check that's in CustomLuaTemplate.cs.

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 10:58 am
by buhubuhu
I put it on NPC script. All of them is active, all of them have the same dialog triggers and the same conversation. They are not Don't Destroy On Load.
Actually I think about it now and I guess that is the problem... previously I was registering and checking on player. Now I want to have bool check on every NPC and it should be independent.

Conversation should check if this specific NPC isFollowing and on that condition show different dialog text.
How I should do it?

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 11:11 am
by Tony Li
Hi,

When you register a method with Lua, it's globally accessible. You can't register GetIsFollowing() on an NPC named Ann and then try to register the same function name on an NPC named Bob.

Here are two possible solutions:

1. Include the NPC's name in the Lua function name:

Code: Select all

Lua.RegisterFunction($"Is{name}Following", this, SymbolExtensions.GetMethodInfo(() => GetIsFollowing()));
If you put the script on a GameObject named "Ann", it will add a function named IsAnnFollowing().
If you put the script on a GameObject named "Bob", it will add a function named IsBobFollowing().

---

2. Or write a single method that accepts a GameObject name:

Code: Select all

public bool GetIsFollowing(string gameObjectName)
{
    return GameObject.Find(gameObjectName).GetComponent<YourScript>().isFollowing;
    // (Note: You should check if GameObject.Find and GetComponent return null.)
}

Lua.RegisterFunction($"IsFollowing", this, SymbolExtensions.GetMethodInfo(() => GetIsFollowing(string.Empty)));

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 11:27 am
by buhubuhu
Ok, I think I finally starting to understand it. I make it like that:

Code: Select all

public bool GetIsFollowing()
    {
        return DialogueManager.currentConversant.gameObject.GetComponent<Knight>().isFollowing;
    }
and I registered it on player.

Looks like it working.

Thanks. :)

Re: C# bool to lua?

Posted: Fri Mar 12, 2021 11:55 am
by Tony Li
Glad to help!