Godot Version
Godot 4.3
Question
I have tried to ask it to AI like Gpt and Gemini but no results so I have come to here
//this is the final code that was “improved” by gpt
extends Node3D
export var json_data: Dictionary
export var planet_scene: PackedScene
export var star_scene: PackedScene
func _ready():
Load the JSON data
var file = File.new()
if file.file_exists(“res://close_stars_and_planets.json”):
file.open(“res://close_stars_and_planets.json”, File.READ)
var json = JSON.new()
var json_result = json.parse(file.get_as_text())
file.close()
Check for errors in JSON parsing
if json_result.error != OK:
print("Failed to parse JSON: ", json_result.error)
else:
json_data = json_result.result as Dictionary # Cast to Dictionary
Load the scenes
planet_scene = preload(“res://planet.scn”) # Adjust the path as necessary
star_scene = preload(“res://star.scn”) # Adjust the path as necessary
Create stars
create_stars()
Create planets
create_planets()
func create_stars():
for star in json_data[“stars”]:
var star_instance = star_scene.instantiate()
star_instance.name = star[“name”]
star_instance.position = Vector3(star[“distance_light_years”] * 10, 0, 0) # Scale the distance for visibility
add_child(star_instance)
func create_planets():
Create Sun
var sun_instance = star_scene.instantiate() # Reuse star scene for the sun
sun_instance.name = “Sun”
sun_instance.scale = Vector3(2, 2, 2) # Adjust scale for the sun
add_child(sun_instance)
for planet in json_data[“solar_system”][“star”][“planets”]:
var planet_instance = planet_scene.instantiate()
planet_instance.name = planet[“name”]
Position planets around the Sun
planet_instance.position = Vector3(planet[“distance_from_sun”] * 10, 0, 0) # Adjust the distance as needed
add_child(planet_instance)
errors
Line 9:Identifier “File” not declared in the current scope.
Line 12:Identifier “File” not declared in the current scope.