0 votes

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"])

Is there a different way? star.getnode(".").getscript() 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!

Godot version 3.2.3
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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

star.player_id = star_data["player"]
by (21,529 points)
selected by

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!

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

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 Frequently asked questions and 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 [email protected] with your username.