How can i add a node to a scene through code?

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

I’m making an app that allows the user to press a TextureButton corresponding to an overwatch character, and it redirects the user to a new page displaying the TextureButtons of other characters that are strong and weak to that character. i have all the data stored in a dictionary with arrays inside of arrays that are inside the dictionary keys, within a script called global.gd

The dictionaries looks like this:

var selected = TYPE_DICTIONARY
var Doomfist = {
        "Stats": ["Doomfist", 250, 0],
        "Counters": [
        ["Good Counter", "res://Buttons/dvaButton.tscn"],
        ["Good Counter", "res://Buttons/genjiButton.tscn"],
        ["Good Counter", "res://Buttons/reinhardtButton.tscn"]
        ],
        "Countered": [
        ["Very Weak Against", "res://Buttons/sombraButton.tscn"],
        ["Very Weak Against", "res://Buttons/pharahButton.tscn"],
        ["Weak Against", "res://Buttons/mccreeButton.tscn"],
        ["Weak Against", "res://Buttons/orisaButton.tscn"],
        ["Weak Against", "res://Buttons/soldier76Button.tscn"]
        ]}

When one of the buttons in the main screen is pressed, this function is called:

func _on_doomfistButton_pressed():
        global.selected = global.Doomfist
        get_tree().change_scene("res://SoloResult.tscn")

When the button is pressed the “selected” variable is set to contain the same data as whatever is in the dictionary of the relevant hero, for example in this case selected would contain all the data within the doomfist dictionary.

The scene is then switched over to the results page, which contains two side-by-side 2 column wide grid containers inside of scroll containers. My goal is to display the button referenced in each array and then the text next to it, however i cannot get this to work.

Currently I’m doing this:

func _ready():
        for counters in global.selected.Counters:
            $Container/Counters/CtrGrid.add_child(global.selected.Counters[0][1])

I’m trying to add the buttons (which i have saved as scenes in “res://Buttons”) as nodes inside of the grid container, but i have no idea what i’m supposed to be doing.

Alternatively i could change the nodes i have inside the grid container (i created dummy nodes of the same type and size) into the nodes i referenced within the dictionary, but i also don’t know how i can do this.

TLDR: How can i import a scene into another scene through code, and how can i access the data within my dictionary? do i need to re-organise the dictionaries? (there’s 28 of them, so RIP)

:bust_in_silhouette: Reply From: coffeeDragon

To load a scene you can either use var scene = load("scene name") or var scene = preload("scene_name").
Load can be used with variables, while preload can only be used with “preknown” strings. Basicly consts and all manual “test” Strings.
Then use var instacedScene = scene.instance()to create a instance of that scene, the result can then be added, allà add_child(instacedScene).
See godot docs, for the offical docs.

I would use enums for the weaknes or strength, instead of strings.
As well as for the heros themself, to avoid misspellings.

Also i would save the dics, in the scenes themself.
With a base method the get the scene of an hero enum, something akin to:

func getHeroButtonSceneFromHero(hero):
   return load("res://Buttons/%sButton.tscn" % heroEnum[hero].to_lower())

You could also add preload("specficHeroButton.tscn") to your dictionaries, if you don’t want to change the structures to much.

Oh my god thank you so much! this program is for my school software development SAT and is due very soon! you’re a life saver!

KaBOOM | 2018-08-15 01:40