Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EditText UI element IsSelectionActive throws object reference error #2569

Open
BlackCurtainStudio opened this issue Dec 19, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@BlackCurtainStudio
Copy link

BlackCurtainStudio commented Dec 19, 2024

Release Type: Official Release

Version: 4.2.0.2282

Platform(s): Windows

Describe the bug
When enabling and focusing a EditText UI element using IsSelectionActive = true, the error Object reference not set to an instance of an object is thrown. However, the EditText element seems to be responsive and accepts input anyway.

To Reproduce
Repro project: https://github.com/BlackCurtainStudio/Stride-EditTextSample

Expected behavior
No errors should occur.

Screenshots
Image

Log and callstacks
[EditTextSample.UiScript]: Error: Object reference not set to an instance of an object. [EditTextSample.UiScript]: Error: at Stride.Core.ServiceRegistryExtensions.GetSafeServiceAs[T](IServiceRegistry registry) in C:\BuildAgent\work\b5f46e3c4829a09e\sources\core\Stride.Core\ServiceRegistryExtensions.cs:line 22 at Stride.UI.Controls.EditText.ActivateEditTextImpl() in C:\BuildAgent\work\b5f46e3c4829a09e\sources\engine\Stride.UI\Controls\EditText.Direct.cs:line 125 at Stride.UI.Controls.EditText.set_IsSelectionActive(Boolean value) in C:\BuildAgent\work\b5f46e3c4829a09e\sources\engine\Stride.UI\Controls\EditText.cs:line 160

@BlackCurtainStudio BlackCurtainStudio added the bug Something isn't working label Dec 19, 2024
@TranquilAbyss
Copy link
Contributor

Providing some research I did for whom ever picks this up.

Problem
I think this exception is happening when UIComponent is enabled and IsSelectionActive = true happens in the same frame.

Work Around

  • Wait another frame before running UIComponent IsSelectionActive = true
  • Enable UIComponent from the start and avoid the problem entirely.

Problem Explanation
This happens due to the EditText not having a Services set until the update run. This seems to be a unique problem to EditText.Direct (not that I tested it) due to UIElementServices.Services.GetSafeServiceAs<InputManager>(); being in EditText type Direct, but other UIElements may require it too.
The null Services is a IServiceRegistry and they all get set by their parent during run time by the UIElement.Update, where the parent passes their Services (aka registry) to the child. This is why running IsSelectionActive = true on the next frame after UIComponent is enabled, works.

protected virtual void Update(GameTime time)
{
    if (Parent != null && !Parent.UIElementServices.Equals(ref uiElementServices))
        uiElementServices = Parent.UIElementServices;
}

I believe the UIRenderFeature DrawInternal that kicks off this process by setting a Page's rootElement which passes down Services to the children through Update.

Two suggestion for solutions

  • Try to pass in the Services on the UIElement SetParent, Which may also mean before the UIRenderFeature DrawInternal as well.
  • Find a different way for the code to handle ActivateEditTextImpl without needing to accessing Services early. DeactivateEditTextImpl most likely wont need to change, but it could help just in case some tries to call it on the same frame.

This UIElementServices.Services.GetSafeServiceAs<InputManager>(); appears to only be called by EditText Direct from searching the entire solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants