Hi everyone, I'm new to godot and programming. I was doing the tutorial ( check the link:
)
About 8:34 he start to make a function for enemy to spawn in different locations.
Well I did the same but It dont work for me. Do you have any Ideas?
Hi everyone, I'm new to godot and programming. I was doing the tutorial ( check the link:
)
About 8:34 he start to make a function for enemy to spawn in different locations.
Well I did the same but It dont work for me. Do you have any Ideas?
You might want to post your code because it's hard to help you otherwise.
Yes, I forgot to do that sorry
There are the photos
Yes, I forgot to do that sorry
There are the photos
I've copied my code here, I see those images are not too clear:
extends Node2D
var spawn_positions = null
var Enemy = preload("res://Enemy.tscn")
func _ready():
randomize()
spawn_positions = $spawnpositions.get_children()
func spawn_enemy():
var index = randi() % spawn_positions.size()
var enemy = Enemy.instance()
enemy.global_position = spawn_positions[index].global_position
add_child(enemy)
func _on_Timer_timeout():
spawn_enemy()
Do you get any errors?
Can you explain how the spawn function is not working? Whether it’s errors, nothing happening, etc? It’s hard to guess what could be the issue.
One thing that could, potentially, be worth checking is to make sure all the node references (using either get_node
or $
) use the exact same names as the nodes, as that is one of the only differences I noticed in the code pictures. I think Godot will print an error to the console though if it cannot find the node.
Well, If I wouldnt type this spawn script then my enemy (according to tutorial) should be just going down for eternity. With the script, it should spawn enemies in different locations. Actually, I wrote the script, and it's doing literally nothing, my enemy is still going down and no other enemies are spawning....
@Iohanan said: Well, If I wouldnt type this spawn script then my enemy (according to tutorial) should be just going down for eternity. With the script, it should spawn enemies in different locations. Actually, I wrote the script, and it's doing literally nothing, my enemy is still going down and no other enemies are spawning....
Where is the spawn_enemy
function being called? In the code shown so far, there isn’t anywhere that calls it and that might be why nothing is spawning. What you might want to do is add something like print(“spawn called”)
to the spawn_enemy
function to see if it’s being called from anywhere.
If the function isn’t being called anywhere, you could add some code to the _process
function to spawn enemy’s every few seconds for testing. Something like this:
# how many enemies to spawn per second
var enemy_spawn_rate = 1.5
var enemy_spawn_timer = 0
func _process(delta):
enemy_spawn_timer += delta
if (enemy_spawn_timer >= enemy_spawn_rate):
enemy_spawn_timer -= enemy_spawn_rate
spawn_enemy()
This method isn’t probably the most ideal, as it will just spawn enemies forever, which likely isn’t what you always want to do, but for testing the spawn_enemy
function it should work.
Judging from your images, your problem is that you didn't connect the timeout signal of your timer to your "_on_Timer_timeout" function (you can see that little arrow icon to the left of the function declaration line in the code from the example that's missing in yours). In the editor select the timer node go to the "Node" tab next to "Inspector", double click the "timeout()" signal and then write your function name in the "Receiver Method" box (should be filled already since you're using the default name).