• 2D
  • I get the "Invalid set index 'global_position' (on base: 'PackedScene) with value of type 'Vector2'

Hey yall, new to godot learning and i've been having fun with the engine! however i'm kind of stuck on this exact script's lines, as they all make sense yet they still come with an error!

the code so far for the script is:

extends Area2D

var speed = 200
var vel : Vector2
const exploding = preload("res://Explosion.tscn")

func _ready():
	vel = Vector2(speed, 0).rotated(Global.player.degForBullet)
	pass

func _process(delta):
	position += vel * delta

func _on_bullet_body_entered(body):
	exploding.instance()
	exploding.global_position = global_position
	add_child(exploding)

any help really appreciated!

Hi,

I think the issue cause lines 14-17. I think they need to be changed like:

func _on_bullet_body_entered(body):
    var explosionNode = exploding.instance()
    explosionNode.global_position = global_position
    add_child(explosionNode)

In your code exploding is just preloaded scene which you can instance many times, but to hold actual instance you need separate variable for instance() method return value.

@GlyphTheWolf said: Hi,

I think the issue cause lines 14-17. I think they need to be changed like:

func _on_bullet_body_entered(body):
    var explosionNode = exploding.instance()
    explosionNode.global_position = global_position
    add_child(explosionNode)

In your code exploding is just preloaded scene which you can instance many times, but to hold actual instance you need separate variable for instance() method return value.

ah yes, that was the problem.. thank you so much for you solution it's really appreciated!