How to spawn a scene at a specific location?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kinoko4Tsuki

Just started using Godot engine yesterday and I’m liking the way it works. I’m trying to make a 2D Megaman-esque game with all the platforming & shooting. After using some tutorials, I found a way to spawn a scene (in this case, an image of an animated water bolt with collision) at the origin using this code:

const WATER_BOLT = preload("WaterBolt.tscn")
	if Input.is_action_just_pressed("ui_focus_next"):
		var waterbolt = WATER_BOLT.instance()

After using this code and finding it works, I added the next line, to spawn it at the Position2D node I had set up in the player scene:

	waterbolt.position = $Position2D.global_position

This is where things broke. The game would freeze up, and the output would give me the error:

Invalid set index ‘position’ (on base: ‘Node’) with value of type

I’ve searched up a couple of tutorials on projectile spawned, and did some error searching, to no avail.

This is the original tutorial, linked to where the guy adds the lines of code similar to what I did:

I don’t know what to do here, the tutorial seems recent enough, and I don’t see any specific typos that would cause the error. Please try to help me with my issue.


:bust_in_silhouette: Reply From: eons

From the error, your scene seems to be a Node and that class does not have position.

Also it does not freeze, the executions breaks on error (probably pointing you to the line with the problem).

To fix that, change the root of the WaterBolt.tscn to be a Node2D (you can change the node type wit right click), save it and now it does have a position property.

ps: be careful on when and how do you set the position, if doing it before adding the scene to the tree, it will be a global, if you set it after entering the tree the new position will be relative to the parent’s position (if the parent has a transform).

Thank you so much! This fixed the problem.
Didn’t know that the parent node I had everything under was supposed to be a Node2D, I’ll keep this in mind from now on.

Kinoko4Tsuki | 2018-11-12 04:27

I’m using a Node2d and its still not working for me.

toivocat | 2022-06-10 15:15