Am i going about making Enemies right?

So I’m trying to make smart enemies for my players to fight against, and I’m trying to think of a better way of doing it.
My way of doing this is making a state machine, i have a set amount of states the enemy can enter, for example Idle state or Attack state, transitioning from one state to another happens in a while true do loop, but i don’t think this is the most efficient way of doing this. Any tips?

What happens during this “transition” in the while loop maybe you can provide a piece of code that shows what happens during this transition to better understand?

I’m actually in the process of doing a similar thing myself - making Actors (name from TES) NPCs that replicate the way Morrowind (The Elder Scrolls) NPCs worked.

Yes, I believe it is efficient to use a main “loop” for the NPC that can switch between states. I call my states “packages” and then there’s the “aggressive” mode.

During this loop, I first check if the NPC is set to be aggressive. If so, find a target and engage it.

After that (and if they are not aggressive) handle the more idle packages such as being “Idle,” “Wander,” or “Follow” a target.

Right now only Wander is implemented.

image

Here’s an example of one of them “wandering” while the other is set to aggressive.

And here’s how I’m structuring my objects/modules

image

Let me know if you have other questions

3 Likes

Do you control all NPCs with this one Script?

I do, yes. Otherwise the project won’t scale and if I wanted to change a bunch of NPCs I’d have to do it individually.

If I have specific monster type mechanics (Like a jumping frog who attacks an NPC named “Aqua” or “Megumin” for hypothetical reasons) then I can just make that my “combat” module (shown on line 29 in my first screenshot) that implements the same method names.

I organize all my “actors” into one folder.

image

Then initiate them in a script.

image

1 Like

Thx for all the info you have given me!
This will definitely help me out!

1 Like