Instance Scenes parameters

Godot Version

4

Question

Let me give some perspective… I have a scene called “level_block”, its a small panel that will show two labels, SceneName and SceneID. I want this “level_block” to instantiate via a forloop to fill a HBoxContainer. I also have a global array stored in a Resource that has 2 custom “house” resources, just a name and id for each. Everthing works fine, i can read and write to the resources BUT I want each new instance of that “level_block” to read it’s name and id from the resources as it’s itterated through.

How could I set each new instance of “level_block” to have a unique name and id as defined in the resources? I have tried using a constructor for “level_block” but cant pass parameters… Any suggestions?

Sorry, can only upload one photo… please let me know should you need more perspective.

Choosing_menu
The above here is where the loop populates the container.
All I need is to pass the parameters to the scene as separate parameters coming from the resources…

Any help would be appreciated, been struggling with this. Thank you!

Just set a variable before add_child.
Like this:

# a.gd
extends Node2D;

var foo: String;

func _ready() -> void:
    print(foo);
# main.gd
extends Node2D;

var scene_a = preload("res://a.tscn");

func _ready() -> void:
    var inst = scene_a.instantiate();
    inst.foo = "Hello world!";
    add_child(inst);
2 Likes

Thank you ever so much! I tried the constructor so many times and could not get it to work, days went by and nothing… I tried your method and it works amazing, thank you for your time! All the best to you and your loved ones. Thank you once again.

1 Like

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