The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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():
    $enemy_spawner.start()


func _on_enemy_spawner_timeout():
    print("Fired!")
    var mEnemy = Enemy.instance()
    mEnemy.global_position = get_global_mouse_position()
    get_tree().current_scene.add_child(mEnemy)
    print("Spawned")

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:
Screenshots of the steps.

Godot version v3.4.4.stable.official.419e713a2
in Engine by (12 points)

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()
get_tree().current_scene.add_child(mEnemy)

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

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?

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.

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?

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

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.

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.

Here is what it looks like now.

enter image description here

Here is what the sprite should look like:
enter image description here

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...

Project Archive

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

That project link doesn't work.

Yeah it tried to add another http to it.

Try again.

1 Answer

+1 vote

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
by (22,674 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.