This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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)

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