GDscript spawner

90 Views Asked by At

So i just started working in godot 4 and i have to make a project for school. i chose to make a tower defense game like perfect tower 2, but now im having a problem. i have been watching a lot of tutorial since i dont have much time to learn the language, and i cant seem to find one about spawners that works. i have tried 10 tutorials until now. now i have a character body (a cube) inside the pathfollow3d which is inside the path3d. i have a group called "spawn" constituted of 4 markers3d where i want them to spawn, the said node3d (spawnlocation), the timer connected and inside the spawn location i have this code:

extends Node3D

var enemy = preload("res://enemieshit/enemiepathfollow.tscn")

func _on_spawn_timer_timeout():
    var enemy_instantiate = enemy.instantiate()
    add_child(enemy_instantiate)
    enemy_instantiate.position = $SpawnLocation.position
    
    var nodes = get_tree().get_nodes_in_group("spawn")
    var node = nodes[randi() % nodes.size()]
    var position = node.position
    $SpawnLocation.position = position

as i said i tried 10 tutorials and none of them worked and from i have seen in my research the are a lot of people who understand about this language and can help me. ( I apologize for the english im not good at it )

1

There are 1 best solutions below

0
On

For everyone that might have the same problem as me i found one that worked and is really simple, but until now i can only make it for each one separately here is the link: [1]: https://www.youtube.com/watch?v=9ZWM1CDNPm8&t=682s

In godot 4 its a little different so i will put my code here


extends Path3D
var timer = 0
var spawntime = 2
var enemy = preload("your pathfollow3d scene")

func _process(delta):
    timer += delta
    
    if (timer > spawntime):
        var newenemy = enemy.instantiate()
        add_child(newenemy)
        timer = 0

this is the script for the path3d

extends PathFollow3D

@export var movespeed = 6

func _process(delta):
    set_progress(get_progress() + movespeed * delta)

this is for the pathfollow3d.

One more thing, be careful and check if both the pathfollow3d scene itself and the one you use in the main scene have the script because i did not and wasnt working beucause of that

follow the video and i hope i was of help.