Godot Version
4.3 `
Question
Hi I am very new to godot and I was making a game where there is a scene full of buttons, and each of those buttons gets unlocked (disabled is turned off) when a corresponding character is unlocked. So the buttons will obviously be a child of the scene that displays it, I was wondering if its safe to also make each of the buttons a child of the corresponding character scene as well as it will be easy to turn off the buttons disabling that way? I was thinking potentially of the future if i ever need to get _parent for the buttons it could cause some messed up logic, I would appreciate any help i can get on this , Thank you
Hi,
If you’re very new to Godot (and game development in general?), you should go for the logic you understand and that works for you. If having buttons being instantiated in different scenes makes sense and does the job, then go for it.
Don’t worry too much about the future consequences; if you’re beginning, you will eventually find out flaws in your project, and you can always refactor things once you have too many issues with your code or project architecture. Everything does not have to be perfectly implemented right away (aside from codes that are very important and “architecture heavy” like complex save systems or things like that, but we’re not talking about such a feature here).
Anyway, I know it’s an actual answer to your question, but I hope that helps. If you still need a more precise answer, it would be cool to add a bit more context like screenshots or something, as I’m not sure I fully understand your issue here.
1 Like
@sixrobin is absolutely correct. You need to do what works best for you.
If you’re asking if UI elements can be safely reused in different scenes, the answer is yes. I do it all the time.
I wouldn’t worry about calling get_parent. It will return the current parent of the node.
Forgive me if I am reading too much into your post, but I suspect that you are thinking of defining each button only once and passing that definition between different scenes. If this is the case, I would suggest that you focus on making the data available between the scenes - and having the controls in the scenes be instances of a class that loads that data.
Here is an example of how to do that:
class_name ButtonBase
extends Button
func _ready() -> void:
load_button_state.call_deferred()
func load_button_state() -> void
# Does nothing at this level
# Defined so it can be overridden
pass
class_name ButtonOne
extends ButtonBase
func load_button_state() -> void:
# Get the state of this specific button and apply it
@onready var button_one: ButtonOne= %ButtonOne
How you store the data retrieved by load_button_state is a matter of personal preference. You just need it to be available as you move between scenes.