C# bool to lua?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
buhubuhu
Posts: 5
Joined: Wed Jan 27, 2021 7:52 am

C# bool to lua?

Post 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. :|
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: C# bool to lua?

Post 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
buhubuhu
Posts: 5
Joined: Wed Jan 27, 2021 7:52 am

Re: C# bool to lua?

Post 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?
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: C# bool to lua?

Post 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
buhubuhu
Posts: 5
Joined: Wed Jan 27, 2021 7:52 am

Re: C# bool to lua?

Post 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?'
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: C# bool to lua?

Post 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.
buhubuhu
Posts: 5
Joined: Wed Jan 27, 2021 7:52 am

Re: C# bool to lua?

Post 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?
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: C# bool to lua?

Post 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)));
buhubuhu
Posts: 5
Joined: Wed Jan 27, 2021 7:52 am

Re: C# bool to lua?

Post 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. :)
User avatar
Tony Li
Posts: 22166
Joined: Thu Jul 18, 2013 1:27 pm

Re: C# bool to lua?

Post by Tony Li »

Glad to help!
Post Reply