How to set data on node after preloading scene

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

I am loading a JSON file, and instantiating scenes based on the JSON. I’m able to preload the scene, set it’s position dynamically from the values in the JSON, but I’m confused how I would set other arbitrary data onto a node’s script inside the new scene i created.

I have some code like this:

func _build_normal_star(star_data):
    var star = preload("res://Scenes/Stars/Star.tscn").instance()
    # this works fine
    star.position.x = star_data["x"]
    star.position.y = star_data["y"]
    # The root node has a script with variables i'd like to set.. 
    # This doesn't complain but not what i want.
    # I've tried `get()` to access these attrs after i set them on the node, but that doesn't seem to work.
    star.get_node(".").set("ships", star_data["ships"])
    star.get_node(".").set("player_id", star_data["player"])
    star.get_node(".").set("star_id", star_data["id"])
    $Stars.add_child(star)

Is there a different way? star.get_node(“.”).get_script() is not the instantiated script to be able to set attributes. Looking for the correct pattern for loading data like this.

Thanks for any help, really enjoying learning Godot!

:bust_in_silhouette: Reply From: kidscancode

You’re already doing it with the positionproperty. You access an object’s properties with the . - node.property'.

star.player_id = star_data["player"]

I guess I was confused about the difference between the “scene” i loaded and the root “node” of the scene.

That definitely worked, thanks so much!

rudedudejk | 2021-03-06 18:26

A “scene” is just a collection of nodes. Ultimately, everything is a node in the scene tree.

kidscancode | 2021-03-06 18:45