Invalid set index 'size' (on base: 'Area2D') with value of type 'int'

Hello,

I am making a simple Asteroids game, where the player shoots and destroys asteroids to accumulate points. I am currently building the asteroid logic - when the large asteroids are hit they will break into medium asteroids, and when the mediums are hit, they will turn into small asteroids, and then they will be fully destroyed.

However, when I shoot the asteroid I am hit with the following error:
Invalid set index ‘size’ (on base: ‘Area2D’) with value of type ‘int’.

Here are the four functions that the message points to:

scripts/game.gd:35

func spawn_asteroid(pos,size):
	var a = asteroid_scene.instantiate()
	a.global_position = pos
	a.size = size
	a.connect("exploded", _on_asteroid_exploded)
	asteroids.add_child(a)

scripts/game.gd:26

func _on_asteroid_exploded(pos, size):
	for i in range(2):
		match size:
			Asteroid.AsteroidSize.LARGE:
				spawn_asteroid(pos, Asteroid.AsteroidSize.MEDIUM)
			Asteroid.AsteroidSize.MEDIUM:
				spawn_asteroid(pos, Asteroid.AsteroidSize.SMALL)
			Asteroid.AsteroidSize.SMALL:
				pass

scripts/asteroid.gd:47

func explode():
	emit_signal("exploded", global_position, size)
	queue_free()

scripts/laser.gd:18

func _on_area_entered(area):
	if area is Asteroid:
		var asteroid = area
		asteroid.explode()
		queue_free()

I am new to Godot, so if there is anything else that is needed please let me know

Does the asteroid script have a property called size?

enum AsteroidSize{LARGE, MEDIUM, SMALL}
@export var size := AsteroidSize.LARGE

this is in the asteroid.gd script

Can you try this?

var a: Asteroid = asteroid_scene.instantiate()

Do you call spawn_asteroid or .size = ___ anywhere else?

Try adding type notations to your function

func spawn_asteroid(pos: Vector2, size: Asteroid.AsteroidSize) -> void:
	var a := asteroid_scene.instantiate() as Asteroid
	a.global_position = pos
	a.size = size
	a.connect("exploded", _on_asteroid_exploded)
	asteroids.add_child(a)

spawn_asteroid and .size = __ are not used anywhere else.

I tried adding type notation and the error changed to this:

is asteroid_scene preloaded?

Must be your asteroid_scene does not actually have the Asteroid script attached, can you show it’s scene tree?

I apologize for my ignorance, but I should put all this in func spawn_asteroid()?

Beautiful! Thank you so much, this was the issue. I am unsure how the script changed tbh, don’t recall fiddling with it. I’ve solely use UE for the last 3 years so honestly I have no idea how to debug with Godot lol

I really appreciate the help though! thank you

1 Like

No problem those “script disapeared” ones are always crazy, seen a lot on the forum the past few days maybe a 4.3 issue? Type annotation help tons of course.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.