Update already created monster's path ? (tower defense)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

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.add_child(mob_instance)” 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)


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)
#add the mob to the entities container
if mobs_remaining > 0:
	mobs_remaining -= 1
	mobs_remaining = 0

Thanks for your time

Not sure, if this counts as an “answer”, so i use just a comment…

I think you can handle this with groups:


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

whiteshampoo | 2020-05-07 12:55

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 !

quizzcode | 2020-05-07 16:03

:bust_in_silhouette: Reply From: quizzcode

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 = get_tree().get_nodes_in_group(“enemies”)
for child in enemies:
var path = $Nav.get_simple_path(child.position, $end_position.position)

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 :slight_smile: