Bug: InputField.text is not writeable by code

I want to preset the InputField with a text.

The Unity Api documentation tells me to do this:
public InputField inputF;
inputF.text = “Preseted text”;

I want to do the same with the Needle Engine.
But in the InputField.js Version “text” is write protected. Because it is a only getter.

How to preset a InputField text?

Hi! To preset the text in a InputField component in Needle Engine where the text property is write-protected, you’ll need to manually set the underlying HTML input element’s value. Here’s how you can accomplish that:

  1. Access the InputField instance and its textComponent.
  2. Directly set the value of the HTML input element that the InputField references.

Here’s a code snippet to illustrate:

import { InputField } from "@needle-tools/engine";

class YourComponent extends Behaviour {
    inputField: InputField;

    start() {
        // Assuming inputField is already initialized and assigned
        this.setInputFieldText("Preset text");
    }

    setInputFieldText(presetText: string) {
        if (this.inputField && this.inputField.textComponent) {
            // Set the underlying input field's value
            InputField.htmlField.value = presetText;

            // Update the textComponent in the InputField
            this.inputField.textComponent.text = presetText;
        }
    }
}

In this example, setInputFieldText will update the displayed value of the input field, along with the text component associated with it. Make sure to call this method after the InputField has been instantiated in your scene.

Let me know if you need further assistance!

1 replies left - Get a PRO license for more AI replies.
thread_baU7XL2U8WpITNGNRujJvZHz
Reply to this message to continue the conversation

Nice very long workaround. But it would be better to fix the bug, that the InputField behaviour matches the Unity documentation.

I understand your perspective, . It would indeed be beneficial for the InputField behavior in Needle Engine to align with Unity’s API for seamless development.

If you believe this is a bug, I recommend documenting this issue clearly and reporting it to the Needle Engine developers. Including specific details about your findings, such as the write-protection of the text property, will aid them in addressing this situation in future updates.

Meanwhile, using the workaround I provided is a practical approach to achieve your desired functionality. If you need help with the reporting process or anything else, feel free to ask!

0 replies left - Get a PRO license for more AI replies.
thread_baU7XL2U8WpITNGNRujJvZHz
Reply to this message to continue the conversation

Fixed in Version 4.2.3.

Thanks!

Great to hear, ! If the issue has been fixed in version 4.2.3, you can now directly set the text property as intended, similar to Unity.

If you have any more questions about using Needle Engine or need assistance with something else, just let me know!

1 replies left - Get a PRO license for more AI replies.
thread_baU7XL2U8WpITNGNRujJvZHz
Reply to this message to continue the conversation

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