How to add and remove child scenes

VliXionVliXion Posts: 2Member

Hey Guys! First time asking a question here!

I'm working on a menu-based game where the player only uses his mouse to click on buttons. So I got a main scene with two child nodes. One of the children is named HUD for the character UI stuff like health and experience points. The second child is named World. I would like to always display the character UI while the scenes in the World node changes to for example Battle.tscn or Shop.tscn. I was thinking that the way to do this would be by adding the Battle.tscn to the World node when the player clicks on the battle button and when the player clicks on the back button or is done with the battle that the Battle.tscn should be removed and in it's place the Shop.tscn should be added. I have no idea how I should do this since I can't get the examples shown in tutorials to work. I'm also not sure if this is the way I'm supposed to do it since I'm still very new to these kinds of things.

If you guys have any solutions or advice for me how to do and handle this then that would be great!


Tags :

Answers

  • RipleaRiplea Posts: 7Member

    You can just add them all to your main node by clicking instance_child_scene instead of add_child_node in the scene window on the right hand side. then add a script to your main node and use that script to control your world, ui, shop and any other scene you add. You can then directly control all its children from there.

    Let's say you would like to hide your shop window. You can do something like $”Shop”.hide() and $”Shop”.show() to turn it on.

    You can also access scripts inside those nodes. So let's say you have a set health func in your Hud node called set_health() you can do $”HUD”.set_health(-20)

    I'd also make the Hud node a CanvasLayer but that is just me.

  • VliXionVliXion Posts: 2Member

    Oh I see! I was also wondering how many scenes I can instance and if there is such a thing as too many scenes instanced? Because of the basic gameplay of my game I can easily see myself adding at least a 100 scenes if not more if I spent some time on this project.

  • RipleaRiplea Posts: 7Member

    as for "How to add and remove child scenes."
    I would make a function to create your instances like:

    func create_instance(add):
        var scene = load(add)
        var scene_instance = scene.instance()
        return scene_instance
    

    Then use create_instance() to make any scenes you need like:

    var new = create_instance("res://Instances/World.tscn")

    then you can use the variable "new" to edit setting like

    "new.hide()"

    to remove instances use queue_free() in ether the scripted or use the variable "new" like

    new.queue_free()

  • ArandualArandual Posts: 4Member

    Firstly Welcome @VliXion !

    Depending on what you're trying to do you could also preload scenes, to say the main node or any node which has sub scenes.
    You can do this via something like:
    var shop = preload("res://shop.tscn")
    (when using preload you still will need to instance the scene before using it.)

    This causes the scene to be loaded, but not instanced at the start, which could be handy for performance. Since you might not want to load a very big scene while playing and rather do it beforehand in a loading screen so you don't cause lag.
    Smaller scenes shouldn't be a problem but can also cause lag when loaded in larger numbers at the same time of course.

    I was also wondering how many scenes I can instance and if there is such a thing as too many scenes instanced?

    I don't know if godot has a hard limit for this, but theoretically you could continue until you run out of processing power/memory.

    You can do something like $”Shop”.hide() and $”Shop”.show() to turn it on.

    You could do this, but keep in mind that hiding a scene doesn't unload it, this means it could stay active in the background and this will likely also take up performance.

    Hope this was a little helpful and good luck with your project.

    P.S. in order to make a scene appear after instancing it you also need to call get_tree().current_scene.add_child(<your scene here>)
    Or some variant of that.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file