Spawning not working

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

I have been trying to copy the idea out of the instance example but I cant seem to get it to work.

I created a scene with both a node2d, kineticbody2d and a rigidbody2d all with a sprite or AnimatedSprite and a collisionbox.

Then I used the following code in a node2d object that is a connected to the timer timeout function:

 extends Node2D

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

export (PackedScene) var Enemy = preload("res://enemy.tscn")

func _on_EnemySpawner_tree_entered():

func _on_enemy_spawner_timeout():
	var mEnemy = Enemy.instance()
	mEnemy.global_position = get_global_mouse_position()

It seems like it is actually firing the code because the Fired! and Spawned show in the log but no sprite is spawned at the location when done manually or from the most recent one to use the mouse position.

Anyone have any idea why?

Here are some screenshots:

Do you not get a newly spawned enemy, or is it just not at the location you expect? If you’re unsure, it should be easy to verify using the Remote tree in the editor with game running.

Assuming you’re getting a newly spawned enemy, but just not at the expected location, what happens if you reverse the order of these two lines?

mEnemy.global_position = get_global_mouse_position()

So, add the new enemy to the scene and then set its position.

jgodfrey | 2022-07-19 17:07

Sadly that didnt help. I thought maybe it was the CanvasLayer so I removed it but it didnt work.

Is there a chance its resizing the sprite since new things seem to want to be created with a 0 width and height?

TULOA | 2022-07-19 18:05

Again, it’d be important to know whether you’re 1) not getting ANY spawned enemy or 2) getting a spawned enemy, but just in an unexpected location.

If you’re unsure, the easiest way to verify that is take a look at the Remote scene tree with the game running. Every object in the scene should show up in the tree.

jgodfrey | 2022-07-19 18:52

It is being created but I dont see it and I have no idea if its a position issue or a size issue.

Is there a way to get it to tell me its size when running by script?

TULOA | 2022-07-19 18:56

You can select the node in the Remote scene tree and view its live, in-game inspector properties - including its transform values.

jgodfrey | 2022-07-19 19:19

As far as I can tell it should be at the location and using the right sizes but its not there.

So I am not sure at this point. In a while I will archive the project and put it here to see if it helps figure this out.

TULOA | 2022-07-19 20:13

Ok so I finally found that it did spawn but way off screen. It is also not matching the size of the sprite when I made it in the scene.

I will research the size part more but is there a better way to set an instance spawned sprite that matches the position of the sprite on the screen? My sprite should be at coords 240,484 and the enemy at 100,100 however 100,100 from those coords is way offscreen.

TULOA | 2022-07-19 20:35

Here is what it looks like now.

Here is what the sprite should look like:

TULOA | 2022-07-19 20:53

I can’t really tell what I’m supposed to be looking at in those last images. If you want to post the project, I can try to take a look.

One other question. Is the scene you’re instancing properly located locally? That is, is it at (or very near) 0,0. If it’s way off in space for some reason, it won’t spawn where you expect…

jgodfrey | 2022-07-19 21:07

Project Archive

It should be. I have it at the center of the top left corner of the screen.

TULOA | 2022-07-19 21:26

That project link doesn’t work.

jgodfrey | 2022-07-19 22:45

Yeah it tried to add another http to it.

Try again.

TULOA | 2022-07-19 23:05

:bust_in_silhouette: Reply From: jgodfrey

Looking at your linked project…

The problem is that your root node (WorldMap) has some crazy scale values assigned to it (x 374.557, y 1.651). So, when you add child nodes to that scene (like your enemy), their position and scale values are multiplied by those of their parent.

That locates and scales your enemy in way you obviously don’t expect. I’m not sure why you have such odd scale values, but I think you’re going to want to reset those back to something much more standard (preferably 1,1) to get expected, consistent, predictable results.

To see what I mean, if you divide the “intended” position and scale of your spawned enemy by the root scale values, you’ll get the expected result. So, in the linked project:

mEnemy.position.x = 10 / 374.557
mEnemy.position.y = 10 / 1.651
mEnemy.scale.x = 1/374.557
mEnemy.scale.y = 1/1.651

I was screwing around and just saw that.

I cant believe I missed that lol.
I revisited it because you should be able to take a TextureRect and have it fill and it should match the window and mine wasnt.

Thanks for all the help.

I will just have to figure out why my new project I attempted to make broke with addchild() throwing an error but I can deal with that later now.

TULOA | 2022-07-20 02:03