Hello, indie devs!
The next thing on the list is having the enemy chase the player. Now, first, the enemy has to determine what the player is. However, the player is not the parent, or the child, of the enemy- so how can we have the enemy determine what the player is?
Assuming you have things set up where the parent is the world, and the children of that are the zombie and the player, you could call the parent by typing in "..".get_node("name_of_node). However, that's a bit sloppy. Instead, you could get the parent, and from there, get the target, as I did here.
Now, what is the rest of this stuff?
The function hostile needs delta to work, but it won't be able to access it on its own, so instead we give it a parameter, and then in _process, pass delta as that argument.
Now, this enemy will chase the player no matter what while set like this- if you don't have the states changed and only have hostile, it will chase the player forever. How do we fix it so that the player is only chased when it is in range, and how do we stop the change_state function?
For the trigger, we could add an area2D, and check if the body entering it is the player, and then switch over to the hostile function.
As for the change_state(), it goes off based on a timer, so you could simply add the name of the timer responsible for state changing, and after that type .stop().
Now, this enemy doesn't move around obstacles, but doing so would be beyond the scope of this tutorial. However, I encourage you to research ways to make the enemies travel in this manner- for a hint, search for information on path finding. For the purposes of this tut, all you're trying to learn is how to build very basic mechanics. Besides, zombies are usually pretty stupid- it wouldn't surprise me if they decide to slam into walls.
Now let's see if you can make your enemies chase the player. I will see you in the next one.