+1 vote

Good morning !
Bit new to godot and python, following many tutorials and testing things.

I was able to make my own version of a tutorial about tower defense.
Instead of having a pre-made path, i decided to use the towers to create the maze.

And so far so good ! Monsters adapt everytime i put a tower.
But. If a monster has spawn by the time i put a tower down, it will walk through it and ignore it. (Monsters spawn after i place the tower down will take the tower in consideration.

I think this is an easy fix.
Every-time I put down a tower, I need to refresh ALL enemy's path at once.
Problem is, i have no clue on how to update multiple "$entities.addchild(mobinstance)" at once.
I tried to google some solution, but i honestly don't even know what look for...

Any tutorial or idea on how i could take care of this ?

Here are 2 pieces of codes that matter :

When I place down a tower :

func build_tower():
#Need to add "If path_available" function to the IF
if can_build and not in_menu:
    Map.set_cellv(current_tile, 16)
    var new_tower = current_tower.instance()
    new_tower.global_position = Map.map_to_world(current_tile)
    $TowerContainer.add_child(new_tower)
    #UPDDATE PATH OF ALL CHILD MONSTERS HERE

When I spawn a monster and create available path :

func _on_mob_spawn_timer_timeout():

var     mob_instance = mob.instance()
#define STARTING psition and DESTINATIOnn
mob_instance.position = $start_position.position
mob_instance.destination = $end_position.position
#Define the path it will follow :

var path = $Nav.get_simple_path($start_position.position, $end_position.position)
mob_instance.set_path(path)
#add the mob to the entities container
if mobs_remaining > 0:
    mobs_remaining -= 1
    $mob_spawn_timer.start(.5)
    $entities.add_child(mob_instance)
else:
    mobs_remaining = 0

Thanks for your time

in Engine by (184 points)

Not sure, if this counts as an "answer", so i use just a comment...

I think you can handle this with groups:

https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_continued.html#groups

Just put all enemies in a group, and after placing a tower, loop/iterate over that group and update the path.

uhhhhh interesting !
I will give it a try. But from quickly reading the doc, it look like this is exactly what i needed !
Will get back to you.
Thanks !

1 Answer

0 votes
Best answer

Alright !
After doing some trial and errors, here is the solution :

When you spawn enemies, add them to the group "enemies".

And then, every time you place a tower run this :

var enemies = gettree().getnodesingroup("enemies")
for child in enemies:
var path = $Nav.getsimplepath(child.position, $endposition.position)
child.set
path(path)

make sure that in the FOR, as a starting position you use child.position.
Or the enemies will go back to start point.

Anyways, I hope it will help someone out there :)

by (184 points)
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.