|Attention||Topic was automatically imported from the old Question2Answer platform.|
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:
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.
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
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
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.
TULOA | 2022-07-19 23:05