currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Announcements, support questions, and discussion for the Dialogue System.
mgregoirelds
Posts: 106
Joined: Wed Aug 23, 2017 4:10 pm
Location: Canada

currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by mgregoirelds »

Hello Tony,

I think I have a problem that looks like to be related to Dialogue System. Here is my case.

I have GameObject which implement OnTriggerEnter2D that is calling DialogueManager.ShowAlert(_ActivationAlertText, 99999f) and OnTriggerExit2D that is calling DialogueManager.DialogueUI.HideAlert() (by the way, why is ShowAlert() directly under DialogueManager while HideAlert() is under DialogueManager.DialogueUI?). This Alert message is displaying something like "Press 'A' to open the crafting station". When the user is pressing 'A' for the first time, I'm calling DialogueManager.DialogueUI.HideAlert() AND I'm displaying a custom UI (not going through DS UI). In that custom UI, I'm calling:

Code: Select all

EventSystem.current.SetSelectedGameObject(null);
yield return null;
_ContinueButton.Select();
_ContinueButton.OnSelect(null);
to set focus on and select my continue button. However, the EventSystem.current.currentSelectedGameObject does get reset to null. By investigating a little bit while looking at the debug info from the EventSystem, when an ShowAlert is called, it looks like DS is setting the currentSelectedGameObject to null since upon exiting the trigger zone (without entering the tutorial), my previous currentSelectedGameObject is set back.

So, my main problem now is that it seems like I can't set properly my selectedGameObject on my Continue button in my tutorial screen because DS is setting it back to null upon calling one of the Alert methods.

Any idea on how to fix this issue? Thanks!
Unity 2022.3.17f1
Dialogue System 2.2.44.1
OpenAI Addon 1.0.12
User avatar
Tony Li
Posts: 22057
Joined: Thu Jul 18, 2013 1:27 pm

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by Tony Li »

Hi,

When a UIPanel (such as the Alert panel) becomes active, it deselects other UI elements so the user can navigate the panel.

I can add an option to not deselect other UI elements. In the meantime, you can comment out the two lines indicated below in Plugins / Pixel Crushers / Common / Scripts / UI / UIPanel.cs:

Code: Select all

            // Deselect the previous selection if it's not ours:
            m_previousSelected = (UnityEngine.EventSystems.EventSystem.current != null) ? UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject : null;
            if (m_previousSelected != null && !selectables.Contains(m_previousSelected))
            {
                // !!! COMMENT OUT THE TWO LINES BELOW: !!!
                //var previousSelectable = m_previousSelected.GetComponent<UnityEngine.UI.Selectable>();
                //if (previousSelectable != null) previousSelectable.OnDeselect(null);
            }
I think that should fix your issue.
mgregoirelds
Posts: 106
Joined: Wed Aug 23, 2017 4:10 pm
Location: Canada

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by mgregoirelds »

Hello,

I'm still having the issue where I do have a button selected and that upon calling ShowAlert(), the selected game object is changed. Now, with the removed lines, the previously selected button is not associated anymore, which isn't what I want. Is it even possible for an Alert to have a Continue button? If that is not the case, maybe calls related to Alert should not remove the focus from previously selected item?

Also, I'm not really sure about the timing when Dialogue System change the selected game object. I tried this code:

Code: Select all

DialogueManager.DialogueUI.HideAlert();
customUI.SetActive(true);
In the OnEnable() method of my customUI, I call the code that I pasted in my previous message to set the selectedGameObject to my button. I do see in the EventSystem debug info that the selectedGameObject is set to my button, but a few milliseconds later, it is set back to null. Could it be that HideAlert() is setting asynchronously the selectedGameObject so that the call to SetSelectedGameObject or Deselect is done after my call to _ContinueButton.Select(), even if it HideAlert() is called before?
Unity 2022.3.17f1
Dialogue System 2.2.44.1
OpenAI Addon 1.0.12
User avatar
Tony Li
Posts: 22057
Joined: Thu Jul 18, 2013 1:27 pm

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by Tony Li »

Hi,

Please try this UIPanel.cs: UIPanel_2018-07-19.unitypackage

Inspect your Alert Panel, and set these values in the UI Panel component:
  • First Selected: None
  • Focus Check Frequency: 0
I'm finishing work for the day, but I'll check back first thing in the morning.
mgregoirelds
Posts: 106
Joined: Wed Aug 23, 2017 4:10 pm
Location: Canada

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by mgregoirelds »

Hello,

I've updated to the latest version, made the change for the Focus Check Frequency to both my Alert and Conversation UI Panel component and I'm still having the same issue. Is there any place where I can add a breakpoint and see where Dialogue System could set back the selectedGameObject value? I tried adding one in CheckFocus(), but the code is never triggered. I'm really trying to find out what/why this is happening.
Unity 2022.3.17f1
Dialogue System 2.2.44.1
OpenAI Addon 1.0.12
User avatar
Tony Li
Posts: 22057
Joined: Thu Jul 18, 2013 1:27 pm

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by Tony Li »

Hi,

Did you import the unitypackage in my previous reply? (There's an even newer one at the bottom of this reply.)

The code is in UIPanel.cs. The unitypackage changes this code in OnDisable() (line 142):

Code: Select all

            if (InputDeviceManager.autoFocus && UnityEngine.EventSystems.EventSystem.current != null && m_previousSelected != null && !selectables.Contains(m_previousSelected))
            {
                UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(m_previousSelected);
            }
This only takes effect in "auto focus" mode -- that is, if the Input Device Manager has detected that you're using a gamepad or keyboard without mouse. If you've used the mouse, this will switch the Input Device Manager to mouse mode. In mouse mode, the code above will be skipped.

OnDisable() is called when the panel's Hide animation is done.


Here are the other areas of code that can chnage the UI selection:


In Close() (line 167), if one of the panel's selectables is currently selected, we select null:

Code: Select all

            if (UnityEngine.EventSystems.EventSystem.current != null && selectables.Contains(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject))
            {
                UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);
            }
(But this only takes effect if a selectable in the panel is currently selected.)

Close() is called as soon as the panel starts its Hide animation.


In OnVisible() (line 190), if a selectable that is not in the panel is currently selected, we deselect it:

Code: Select all

            if (InputDeviceManager.autoFocus && firstSelected != null && m_previousSelected != null && !selectables.Contains(m_previousSelected))
            {
                var previousSelectable = m_previousSelected.GetComponent<UnityEngine.UI.Selectable>();
                if (previousSelectable != null) previousSelectable.OnDeselect(null);
            }
OnVisible() is called when the panel's Show animation is done.


Finally, if the Input Device Manager is in "auto focus" mode (gamepad or keyboard), and the focusCheckFrequency is non-zero, it will automatically select a selectable inside the panel. If there are no selectables inside the panel, it will select null. This happens in CheckFocus().

This updated UIPanel.cs changes CheckFocus() so it will not select null if there are no selectables in the panel:

UIPanel_2018-07-20.unitypackage

Here's a test scene:

TestAlertNoSelect_2018-07-20.unitypackage

If you play the scene, it will select a button named "Button". After 1 second, an alert will appear. The button should stay selected during the alert and even after it.

If that doesn't help, can you send me a scene that reproduces the issue? (Let me know what version of Unity to use.)
mgregoirelds
Posts: 106
Joined: Wed Aug 23, 2017 4:10 pm
Location: Canada

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by mgregoirelds »

Hello Tony,

It is the OnDisable() call that put back the previous selected item right after I selected another button, resulting in my button not having the focus anymore. It does make sense... I send HideAlert(), then I select my custom Continue Button, then the PanelUI is disabled, resulting in selectedGameObject to be set back to the previous value saved by DS.

Any change could be added into DS to bypass this (other than by commenting this line)?
Unity 2022.3.17f1
Dialogue System 2.2.44.1
OpenAI Addon 1.0.12
User avatar
Tony Li
Posts: 22057
Joined: Thu Jul 18, 2013 1:27 pm

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by Tony Li »

Oh, I understand now. I'll make it an option to not do that. I'll try to have a patch ready by Monday.
mgregoirelds
Posts: 106
Joined: Wed Aug 23, 2017 4:10 pm
Location: Canada

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by mgregoirelds »

Great! Thanks a lot for helping me with this issue, it is really appreciated!
Unity 2022.3.17f1
Dialogue System 2.2.44.1
OpenAI Addon 1.0.12
User avatar
Tony Li
Posts: 22057
Joined: Thu Jul 18, 2013 1:27 pm

Re: currentSelectedGameObject is set to null when calling ShowAlert and/or HideAlert

Post by Tony Li »

Maxime,

Patch 20180724 on the customer download site adds a checkbox Select Previous On Disable. You can untick this checkbox to turn off the behavior that's causing a problem with your scene.
Post Reply