0 votes

Hi,...I have for example two objects (planets) ,player ship travels through the universe and
when is close (raycasting from player ship) to the planet I need some info about planet is
shown...so I have Label node for that.
So I wrote json formatted text....something like
name : Planet X25
weight : 250000
info : Planet X25 is an old abandoned planet, there were mining colonies.
name : Planet C678
weight : 120000
info : Planet C678 is mainly covered with lava and stones.
......script for parse json works but I get a whole json text...so how I get for example only info text or weight and info ?
....so far i do not understand the arrays and the dictionary of things,yet.
Thanks for your help.

in Engine by (450 points)

1 Answer

+2 votes
Best answer

By what I understand of your question,

First you need to parse the json file into a dict like this:

var planet_info = {}

func load_json(json):
    var file = File.new()
    file.open(json, File.READ)
    planet_info = parse_json(file.get_as_text())

That should send the json data into the dict.
Then you can access it in the normal way through the dict with the key.

This should work.

by (416 points)
selected by

Thanks,...but what is json in this script?...variable for a json file?
.....I need open "res://planets.json"
so file.open(json, File.READ) do not open anything

yep json is the variable for the json file.

Yes..Thanks very much..., could you still show me how to use the dictionary solution?
...I do not get dictionary procedure,yet.

Suppose you want to get the weight you can do planet_info["weight"] . That should work.

Yeah,....now ,it's works fine....Thanks very much for your help!

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.