Finite State Machines with Generics in C# Godot for Artificial Intelligence [Andromedoom Dev Log 6]
Polysthetic
๐พWelcome to the Andromedoom dev logs! In episode 6, I'm showing you how I implemented finite state machines in C# for programming the alien AI in my main game. Note in my prototype that I had built the alien AI with a bunch of if statements, which quickly grew untenable. This is also a brief example of how to use generics in C# and how to animate the direction of an object using Godot's in built tweens. The dev log was omitted today as I had been heavily refactoring my main project. I hope you enjoy this video series and stick around for the game's development! ๐
โฑ๏ธ Timestamps: 0:00 Intro 1:03 Setting up the scene and updating collision layers (you can skip this section) 2:53 Creating the abstract classes in a sub-namespace 4:07 Finite state machine class 7:08 State class 8:14 Why we need C# generics 9:19 Method to add and change states 12:38 Initialising the FSM within the alien class 13:26 Defining the movement state and executing code within the FSM 17:10 Changing between states 18:40 Defining the dropping state 20:33 Defining the turning state 26:25 Turning smoothly with tweens 33:21 Landing the alien 35:47 Passing information between states 40:30 Conclusion
๐ป Addenda:
- The functions for "SetCurrentState" and "GetCurrentState" may be better implemented with a "CurrentState" property, with their respective logic within the get and set methods.
- The forward direction on an object is actually -Z, so use -Transform.basis.z. This is why LookAt points towards -Z.
- It may be more efficient to check if the tween is null and generate it if so. That way, the tween is never removed and is just reused.
- Using partial classes has caused my Godot engine to crash, so use caution. It may be prudent to extract your state classes to generic scripts that can be reused by multiple other objects.
๐ถ Music tracks from:
- Chau Sara - Lightsaber Bar
- Provided by Lofi Girl
- Listen: https://fanlink.to/LightsaberBar
You can also visit me at: WEBSITE ๐ https://polysthetic.com THREADLESS ๐ถ๏ธ https://polysthetic.threadless.com DRIBBBLE ๐ https://dribbble.com/polysthetic YOUTUBE ๐ฅ https://youtube.com/c/polysthetic AUDIOMACK ๐ผ https://audiomack.com/polysthetic ... https://www.youtube.com/watch?v=9j7oh-b3Lak
187517323 Bytes