Choosing a Node with Keyboard Input

Trying out this modified:
ButtonProxy.cs

using System;
using UnityEngine;
using UnityEngine.UI;

public class ButtonProxy : MonoBehaviour
{
     Button nextButton;
    [SerializeField] Button firstChoiceButton;
    [SerializeField] Button secondtChoiceButton;    

    private KeyCode key;

    private void Awake()

    {        
        nextButton = GetComponent<Button>();
    }

    public void SetKey(KeyCode keyCode)

    {
        key = keyCode;
    }

    private void Update()

    {

        if (key < 0) return;

        if (Input.GetKeyDown("return"))

        {

            nextButton.onClick?.Invoke();

        }
        if (Input.GetKeyDown("escape"))
        {


        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {

            firstChoiceButton.onClick?.Invoke();
        }
        if (Input.GetKeyDown(KeyCode.Alpha2)){

            secondtChoiceButton.onClick?.Invoke();
        }
    }
}    

Also tried adding a listener in DialogueUI.cs and a serialized field referencing the choices. Does not have to be very complex just 3 choices for example. Clicking next with Enter works just fine but the choices dont work. I was suggested by Brian who discovered why my button was not registering clicks in the first place. He went further as to suggest an awesome method for using the Input Keys and I have some trouble with the choices. The problem lies I think that I do not know how to use an index for the responses choice and not sure if I need to create an extra function specifically for that if I don’t use the mouse for selecting.

In the course, we’re building the response buttons by iterating over the choices available.
What we need is to keep track of an index, so that we can assign the hotkeys.

Here’s the BuildChoiceList from the course’s DialogueUI

        private void BuildChoiceList()
        {
            foreach (Transform item in choiceRoot) Destroy(item.gameObject);
            foreach (var choice in playerConversant.GetChoices())
            {
                var choiceInstance = Instantiate(choicePrefab, choiceRoot);
                var textComp = choiceInstance.GetComponentInChildren<TextMeshProUGUI>();
                textComp.text = choice.GetText();
                var button = choiceInstance.GetComponentInChildren<Button>();
                button.onClick.AddListener(() => { playerConversant.SelectChoice(choice); });
            }
        }

With just a couple of additions, we can put a ButtonProxy script on each button (Simplified, don’t include the nextButton and quitbutton in this, they can get their own ButtonProxy or a custom script just for these).

        private void BuildChoiceList()
        {
            foreach (Transform item in choiceRoot) Destroy(item.gameObject);
            int i=0;
            foreach (var choice in playerConversant.GetChoices())
            {
                var choiceInstance = Instantiate(choicePrefab, choiceRoot);
                var textComp = choiceInstance.GetComponentInChildren<TextMeshProUGUI>();
                textComp.text = choice.GetText();
                var button = choiceInstance.GetComponentInChildren<Button>();
                button.onClick.AddListener(() => { playerConversant.SelectChoice(choice); });
                var buttonProxy = choiceInstance.GetComponentInChildren<ButtonProxy>();
                buttonProxy.SetKey(KeyCode.Alpha1 + i);
                i++;
            }
        }

This would use the ButtonProxy script I pasted in your previous post, which should be on the same GameObject as the button

using System;
using UnityEngine;
using UnityEngine.UI;

public class ButtonProxy : MonoBehaviour
{
    private Button button;
    private KeyCode key;
    private void Awake()
    {
        button = GetComponent<Button>();
    }

    public void SetKey(KeyCode keyCode)
    {
        key = keyCode;
    }

    private void Update()
    {
        if (key < 0) return;
        if (Input.GetKeyDown(key))
        {
            button.onClick?.Invoke();
        }
    }
}

I see! After changing the BuildChoiceList like you suggest, the ButtonProxy script tells me I have a null reference on the last line about invoking the buttononclick:

using System;
using UnityEngine;
using UnityEngine.UI;

public class ButtonProxy : MonoBehaviour
{
    [SerializeField] Button nextButton;  
    private KeyCode key;

    private void Awake()
    {
        nextButton = GetComponent<Button>();
    }



    public void SetKey(KeyCode keyCode)
    {
        key = keyCode;
    }

    private void Update()
    {

        if (key < 0) return;

        if (Input.GetKeyDown("return"))
        {
            nextButton.onClick?.Invoke();
        }      
    }
}    

I’m not sure if I am meant to include the if alpha1 input code lines after the end of the script. Anyways the next button keeps working with return keycode but the choices don’t respond to alpha 1. I got the buttonproxy script on my NextButton, is that what I am supposed to do?

private void BuildChoiceList()
        {
            foreach (Transform item in choiceRoot) Destroy(item.gameObject);
            int i=0;
            foreach (var choice in playerConversant.GetChoices())
            {
                var choiceInstance = Instantiate(choicePrefab, choiceRoot);
                var textComp = choiceInstance.GetComponentInChildren<TextMeshProUGUI>();
                textComp.text = choice.GetText();
                var button = choiceInstance.GetComponentInChildren<Button>();
                button.onClick.AddListener(() => { playerConversant.SelectChoice(choice); });
                var buttonProxy = choiceInstance.GetComponentInChildren<ButtonProxy>();
                buttonProxy.SetKey(KeyCode.Alpha1 + i);
                i++;
            }

            }
        }

I do not need to have anything fancy, just looking for 3 choice options, with keycodes Alpha 1,Alpha2 and Alpha 3 ideally. This course is way to complex for my level honestly but still trying to enjoy! it is fascinating how much there is to learn. :grinning_face_with_smiling_eyes:

This won’t check for the 1, 2, or 3, it will only listen for the return key and try to invoke the NextButton, but since the choice buttons are generated dynamically, this script doesn’t have the next button assigned. (Even if it did, if you had 3 choices set up, pressing enter would call Next three times, not ideal).

This is what you want in the ButtonProxy.

For the Next button’s ButtonProxy, you can set this up in DialogueUI.Start() when you assign the button’s OnClick

        void Start()
        {
            playerConversant = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerConversant>();
            nextButton.onClick.AddListener(Next);
            nextButton.GetComponent<ButtonProxy>().SetKey(KeyCode.Return);
            UpdateUI();
        }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms