0 votes

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)

in Engine by (12 points)

1 Answer

+3 votes

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.

by (256 points)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.