I need halp with my code that i have been trying to solve for a couple of hours

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.

File was renamed to FileAccess in 4.0, chat gpt gave you old 3.0 code.

Make sure to format your code

I think you will want this

if FileAccess.file_exists("res://close_stars_and_planets.json"):
    var file := FileAccess.open("res://close_stars_and_planets.json", FileAccess.READ)
    json_data = JSON.parse_string(file.get_as_text())
2 Likes

Thanks a lot I am new to the community and I didnt know how to format from now on I will be try to format the code. Can I ask something about the code now when I try to start the scene it says Invalid access to property or key ‘error’ on a base object of type ‘int’

Sounds like you are using .error on a numeral value. I think this is from this line, which shouldn’t be needed with my example.

var json_result = json.parse(file.get_as_text())
if json_result.error != OK:

The error states that json_result is an int, a numeral value which does not have a property .error, since it’s just a number. It’s like doing 42.error != 0

you may have to post your edited code for further help.

1 Like