Enemy architecture question

Godot Version

I believe it's 4.6

Question

hi I need help making a tutorial more complicated than it needs to be. I followed a tut for making a tower defense game. In the tutorial there is only one enemy. It's a pathfollow3d node with a mesh, a collisionshape3d and an animation. So I made five more enemies, made them all children of that pathfollow3d and I just don’t know how to say “if I am a zombie then when I die run the zombie death animation". I don’t want to get an AI to do it for me. I want to learn. So my first question is am I doing this a super dumb way? Should they each have a separate pathfollow? I’m sure there is an easy way to do this.

I applaud this.

Can you show us the code you are using for your enemies. Put it between ``` on the line above and below like this:

```gd
# Put your code here
```

And it’ll look like this:

# Put your code here

This is a complex problem. The way I usually deal with it is to use a state machine. The idea is that your zombie can be in one of several different states, and as different things happen, they will change to a different state and behave differently.

You can make a simple state machine by making an enum for your different states and keeping track of it in your node:

class_name Zombie
extends Node3D

enum States { IDLE, FOLLOW_PATH, REACT_TO_HIT, DYING, DEAD }
var current_state:States = States.IDLE

Here the zombie starts IDLE, which means just standing around. Every _process() call you can check the current_state to figure out what to do. The state can change based on game events - eg, if the zomibe takes a hit, it will change to the REACT_TO_HIT state. If the hp is equal to zero, after reacting to the hit is done, it will switch to the DYING state.

There are lots of useful online articles that go into more depth about state machines.

This is a tough problem. Don’t feel too bad if you’re finding it tricky.


Edit: To identify your zombie character, the best way I’ve found to do it in Godot is create a special node that has useful info like that in it. For example:

class_name EnemyInfo
extends Node

enum EnemyType { ZOMBIE, WEREWOLF, GHOST }
@export var enemy_type:EnemyType

You can create one of these nodes that lists the enemy type (and other info you might find useful) and add it as a child to your player body. Then when you need to check if your character is a zombie or something else, just look through all the children of the node and see if any of the child nodes are of the EnemyInfo type. If there is one, you can then get the enemy_type from it.

But if every type of enemy is a child of this pathfollow 3d node how do I differentiate between them? Maybe the projectile that hits this enemy can tell my pathfollow node script what type of enemy it hit? Is there a way to say “if the mesh that is currently visible is monster type a", then do the thing?

Right now the code is just a note that says #why can’t iake this work?! Lol, I deleted everything that didn’t work as it failed. When I get to my computer I can show you the rest of the code if you think it will help.

Make an enemy scene.

1 Like

Show us the code and a screen shot of your editor with the enemy and the enemy scene tree.

It is an enemy scene but it’s a pathfollow node with the different enemies parented to it. My hope was that they could all share the same script that way but it’s causing me problems