Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

I have been struggling with this error for a good time now. I created a rat spawner very similar to the one we used in Alien Attack, the rats spawn and are visible when I add them as a child in the Rat Spawner scene. However, now I am updating score based on rat's death I need to use a signal. However when I connect them using this signal, the rats are invisible but I know they're there because I can see them spawning in the remote tab.
Here is my relevant code for the rat spawner

signal enemy_spawned(enemy_instance)

var enemy_scene = preload("res://scenes/rats.tscn")

@onready var spawn_positions = $RatSpawnPositions

func _on_timer_timeout():

spawn_enemy()

func spawn_enemy():

var spawn_positions_arrays = spawn_positions.get_children()
var random_spawn_position = spawn_positions_arrays.pick_random()

var enemy_instance = enemy_scene.instantiate()

#add_child(enemy_instance)
enemy_instance.global_position = random_spawn_position.global_position
emit_signal("enemy_spawned",enemy_instance)

And here is what I'm doing with it in the game scene

green-connected-arrow

func _on_rat_spawner_enemy_spawned(enemy_instance):

add_child(enemy_instance)
asked in CSC380Jan2024 by (1 point)
0

Thank you for all the answers, I finally solved it by using $RatSpawner.add_child(enemy_instance)

So now I'm still adding the child nodes to the Rat Spawner but I also have access to them in the game script, excellent!

2 Answers

+1 vote
 
Best answer

It looks like you are just creating an instance of the child as the signals callable. This will make the instance of the rat, but wont add anything like its position or adding it to the scene tree. Consider using a whole function for the signal callable, in which you can instantiate the rat, change its position, and add it to the scene tree.

answered by (1 point)
selected by
+2 votes

One idea might be to double-check that the rats are visible upon being made upon the scene. I would do the same for the rat spawner since it being not visible might cause the rats to be not visible. Or it could be that they can't move once on the scene so there's a velocity/move_and_slide issue.

answered by (1 point)
...