Proper way of creating a cliclable texture button with label

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tofiffe

My current structure is as following:

TextureButton->CenterContainer->Label

The problem I have is that I cannot reduce the size when using it in VBoxContainer and the text is not set properly.

I have the following set as the script of the button, this works for setting the text when running, but not in the editor. Prefixing it with tool works in the editor, but only as long as the scene is open and doesn’t work when running:

extends TextureButton


export(String) var text: String setget set_text


func set_text(text: String) -> void:
    $CenterContainer/Text.text = text

The problem I have is that I cannot reduce the size when using it in VBoxContainer and the text is not set properly.

Not sure if I understand the problem… You cannot control the size of anything you add as a child of a container node - that’s the idea: the container takes control. Also what do you mean by “the text is not set properly”? Is it missing? Cut off? Falsely aligned?

I have the following set as the script of the button, this works for setting the text when running, but not in the editor.

Obviously, as it’s not running in the editor at all.

Prefixing it with tool works in the editor, but only as long as the scene is open and doesn’t work when running

Prefixing a script with the tool-keyword won’t stop it from working when running the game. And why/how do you expect it to work when the scene is not open… ?

njamster | 2020-05-25 10:52

The size of elements is too big, despite me setting the scale lower (0.5 for example).
As for the text, by “not set properly” I meant that it’s not set at all.

As for what I wanted to achieve is, I’ve wanted to add a property to the button that I could set from the editor and see it there as well, without needing to run the scene (which works without tool) and in the editor (which works with tool), both showing the same value, basically behaving like the text property of a label.

tofiffe | 2020-05-25 10:57

Just to be sure: You have a custom texture button scene looking like this

- TextureButton
  - CenterContainer
    - Label

and instance it in a different scene as child of a VBoxContainer like here

- VBoxContainer
  - CustomTextureButton1
  - CustomTextureButton1

… correct?


The size of elements is too big, despite me setting the scale

Then I assume by “elements” you mean your CustomTextureButton’s inside the VBoxContainer? If so, setting the scale won’t do anything. Again: any container can overrule any modification you did to the transformation of it’s content.

by “not set properly” I meant that it’s not set at all.

I cannot reproduce that. I created the scenes as described above and added the script you provided to the CustomTextureButton, prefixed with the tool-keyword. It works fine, both in the editor as well as in-game. Can you provide an example project?

njamster | 2020-05-25 12:38

I have uploaded the sample here: Sample.zip - Google Drive

The structure is slightly different, only the VBoxContainer has some extra containers.

In the sample I’ve uploaded I get the following result:

  1. set the “text” property in Main.tscn to something and it’s updated in the preview
  2. run the scene, there is no text on the buttons
  3. remove “tool” from script, repeat process, preview does not show text, but it’s present when running.

tofiffe | 2020-05-25 12:50

Not entirely sure what’s going on here. But you never actually update the value of the export-variable text in your setter. Doing that seems to fix your problem:

extends TextureButton

export(String) var text: String setget set_text

func set_text(new_text: String) -> void:
    text = new_text
    $CenterContainer/Text.text = text

Might be a bug nonetheless. It works without updating the export variable’s value in the setter when running CustomButton.tscn on it’s own. It doesn’t work when that scene is instanced somewhere else (like in Main.tscn). However, if you right-click on an instance and check “Editable children” if will start working again. I’d guess it has to do with how instanced scenes are treated internally somehow… ¯\_(ツ)_/¯

njamster | 2020-05-25 13:53

Looks like that fixed it! I figured a backing variable was not required since I was setting the label text, but that fixes it. I’ve also tried adding a getter, but as long as the variable itself is not set the result is the same. Looks like setting this is required, which is good enough for me, thanks for your help! :slight_smile:

tofiffe | 2020-05-25 14:05