#Hope someone can help me. I recently plan to refactor my Player and various NPCs.

19 messages · Page 1 of 1 (latest)

neat edge
#

The image shows my previous implementation approach. Even though it's in Chinese, I'll roughly describe it."

First, here are two screenshots of the node tree. Under the Node2D node are the specific animations of the AnimationSprite2D nodes. Under the Node, there’s a bunch of specific states such as Run, Idle, Attack, etc. Their parent node is an overall script that centrally manages the logic.

The remaining three images show my personal implementation of this comprehensive logic management script. One image shows that I created a large number of boolean variables to check various conditions — for example: whether on the ground, whether the movement key is pressed, whether the attack key is pressed, whether in the air, etc. Another image shows the judgment/check functions I wrote specifically for these variables. Most of them are just simple one-line expressions, basically combining those boolean variables in different ways. The last image shows that these functions are executed once every frame (_process)."

Finally, the last image has text written on it — it's the Idle script. Most of the other state-specific scripts follow a similar pattern.

My main implementation logic is as follows: each state script gets the boolean judgment variables from the parent node, initializes them in _ready(), and then follows this unified structure in its every-frame function:

If the condition for Idle is not satisfied, then set the Idle animation's visible = false and return immediately. (Almost all my state scripts start their per-frame function with this same check: if the condition is not met, hide the animation and return.)

If the condition is satisfied, then set the animation visible = true, call play(), and apply some fine-tuning and additional settings depending on the specific state.

(The rest of the states follow basically the same structure as this Idle example.)

#

Back then, I was lazy and didn’t put in the effort to properly learn state machines. When I tried to apply them, I ran into a lot of bugs, which killed my motivation. As a result, I ended up not using them.

But now, my current approach can no longer hold up. It has turned into a massive pile of spaghetti code that I can’t continue developing. I know it’s time to pay the price for my past laziness and refactor this part of the code.

After reading my project description, I hope someone can recommend a suitable state machine solution for my project. This time, I will seriously study and face it properly."

cyan pebble
#

I had a video tutorial that explains state machine like you were an idiot. BUT it's in spanish

neat edge
waxen oak
# neat edge well, after all this time of learning, I'm no longer concerned about technical d...

Is there a need to run so many function each frame like this from your physics process? That seems odd. That's raising a flag that maybe something may have been misconceived or misunderstood. I can't make head's or tails what they are, because I don't read Chinese (yet), and my attempts at getting AI to help me have been... Well, less than helpful. I put your screenshots into Deepsink to see if it could tell me more, but it just recognizes you have 17 function calls in the physics process and agrees that likely points to some optimization/logic problems if not now, then down the line. I don't normally use AI, but had no choice, because I can't read Chinese, myself. I can tell all of those functions end in something that means 'check' or 'test' but that's about it. There's a good chance these properties can be listened for via signals rather than polled every physics frame (very inefficient).

I'd recommend learning to use signals combined with an enum-based state machine with explicit entry/exit for handling state. Ask questions like: How many 'things' can my character be doing at once? If hte answer is one, this becomes much simpler. If it's more than one, you have some states that are dependent on other states at hte same time. That's where things can get hairy. This looks like it's for a platformer. So, I recommend playing one, and taking careful notes for how each action is allowed to happen based on inputs. You don't have to use these for your game, but the exercise is useful to gain an understanding of the exact amount of state control you actually need, versus trying to design it all at once and have each thing tested each frame.

#

Also, based on Deepsink, you are trying to evaluate a Vector2 as a bool in a couple places where you will find it parses, but will cause erroneous behavior. I am taking it at its word, since I can't read it.

#

I don't know how it figures stuff out, as I'm not that familiar with it, but that's all I've got.

neat edge
neat edge
sage gyro
#

Actually I feel like all of this can be handled by AnimationTree and I recommend you to check the godot docs and relevant tutorials

#

Mostly all the states transition are handled by code and how does the state interact with each other. It is all hidden, not visualised.

Also no idea gdscript can handle chinese code. This is wild

neat edge
neat edge
neat edge
# sage gyro Actually I feel like all of this can be handled by AnimationTree and I recommend...

yeah,animation tree.That was my problem. At the beginning, I also found adding animations using "animationplay" was quite troublesome, so I discovered "animationsprite2d" and haven't used "ap" since then. I really should relearn and confront this thing now. However, considering the "asp2d" that I have been using for such a long time, it seems that my animation requirements weren't that complex after all.

sage gyro
#

https://youtu.be/iElHZhOxGYA?si=rnyz_eFJsiK0nsiU

i think in you case, you can try to use animation tree and refactor 1 by 1

In this video we go over the basics of Animation Trees in Godot 4. We rebuild a top down animation set with idle, run, and attack animations from simple to complex.

Animation Trees are unbelievably useful, and I use them all the time and would highly recommend using them yourself.

Discord - https://discord.gg/skPc32bUfA

Resources:
Character...

▶ Play video
#

There is code approach Let me go find it

neat edge
sage gyro
#

There is also a resource-based approach for state machine. May make your game run smoother