Can I add some custom attributes to Button?

Godot Version

Godot v 4.2.1

Question

I’m creating a button in the code, and I want to assign it a custom variable to use later. What ways can I do this?
I know about “expands”, but I don’t have a separate node for this button to declare variables in .gdscript of this node, because I create it in the code itself.
I want something like:

var a = TextureButton.new()
a.set("value",123) 
print("My value is: ",a.value)

‘set’ doesn’t return an error, but it doesn’t set my custom attribute to the button.
Error pops up in print: Invalid get index ‘value’ (on base: ‘TextureButton’).

You should be able to setup a button as a separate scene with the code, etc. you need, then instantiate it into your main scene with code:

Oh, I’ve been trying to find other ways to do this. Earlier, when I was creating a separate scene, I had a problem that I couldn’t press this button. However, now I have tried and I can.

But I still want to know if it is possible to assign custom variables to standard nodes such as Button

Try using the meta.

	var btn=TextureButton.new()
	btn.set_meta ( "name", 123 )
...
...

	var x = btn.get_meta("name")

Thank you very much! It really helped.
I will try to study about meta, because I would like simpler ways to access these meta, but in this case this is the ideal way. Thanks again!

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