Enabling script on seperate object on variable check

Announcements, support questions, and discussion for the Dialogue System.
Trashrat
Posts: 25
Joined: Fri Oct 13, 2023 11:38 am

Re: Enabling script on seperate object on variable check

Post by Trashrat »

On dialogue nodes, so using the script feature to set a variable to true or false etc on certain nodes.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Enabling script on seperate object on variable check

Post by Tony Li »

Hi,

That should work. Make sure OnEnable and OnDisable are really being called at the correct times at runtime. Feel free to send a reproduction project to tony (at) pixelcrushers.com if it still isn't working.

Alternatively, you could write a C# method to turn the outline on or off. Then register that method with Lua and use it directly in the dialogue entry's Script field. This way you don't have to deal with bools at all.
Trashrat
Posts: 25
Joined: Fri Oct 13, 2023 11:38 am

Re: Enabling script on seperate object on variable check

Post by Trashrat »

Hi Tony, brilliant thank you, I almost have it perfect now by registering a function. So now I can enable and disable the outline on a dialogue node, but unfortuantely can only get that working on the item the dialogue is taking place on. So say I wanted to have a dialogue option trigger if it can be outlined on a seperate object I can't figure that out.

Apologies for this and thank you again.

Code: Select all

using PixelCrushers.DialogueSystem;
using System.Collections.Generic;
using UnityEngine;

public class EnableOutline : MonoBehaviour
{
    Outline outline;


    public bool showOutline = true;
    
    private void Awake()
    {
        if (!TryGetComponent(out outline)) return;
        outline.enabled = false;
    }

    void OnEnable()
    {
        Lua.RegisterFunction(nameof(CanOutline), this, SymbolExtensions.GetMethodInfo(() => CanOutline(false)));
    }

    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(nameof(CanOutline)); // <-- Only if not on Dialogue Manager.
    }

    public bool CanOutline(bool canOutline)
    {
        if(canOutline == true)
        {
            showOutline = true;
            return true;
        }
        else
        {
            showOutline = false;
            return false;
        }
    }

    void Update()
    {
        if (!outline) return;
        if(CanOutline(showOutline))
        {
            outline.enabled = Input.GetKey(KeyCode.Tab);
        }
    }
}
EDIT: Apologies, in fact this isn't working at all it would seem. And i'm only making matters worse apologies. I will try again in the evening. Thank you again
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Enabling script on seperate object on variable check

Post by Tony Li »

Hi,

Lua.RegisterFunction() registers the function globally. You can't have a separate CanOutline() method on more than one GameObject. You could specify the GameObject by name as a parameter to the function.

However, you may find it easiest to use an OnExecute() scene event:

sceneEventOutline.png
sceneEventOutline.png (39.85 KiB) Viewed 402 times
Trashrat
Posts: 25
Joined: Fri Oct 13, 2023 11:38 am

Re: Enabling script on seperate object on variable check

Post by Trashrat »

Hi Tony,

Thank you so very much, that works perfetly using Scene Events and is very easy to set up. Thank you again for your time and your help, it's greatly appreciated.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Enabling script on seperate object on variable check

Post by Tony Li »

Glad to help!
Post Reply