SakeTami
Megan Fox
Megan Fox

patreon


A bit more on AnimNotifyStates

I casually mentioned how these are the hub of the "attack-defining Montage that does all the things" approach in that last post, but I wanted to talk specifically about the two ways you can do this.

There's basically two approaches: either the AnimNotifyState proactively reaches out and sets variables in the thing managing the skeletal mesh it's running on, OR, it registers itself when it starts and unregisters itself when it stops, and there's some good reasons to do both.

Now, before we even talk about those, there's the third way of using AnimNotifyStates, and that's for when they don't need arguments. All you care about is using that AnimNotifyState like a flag. This is handy for things like, say, BlockNormalMovement.

All it does is what it says on the tin, so the only unique code is giving it a user-friendly name for when you're placing it in the Montage. Then in your normal movement logic, where you'd typically apply whatever the player is doing with their gamepad to make your guy run around? Well, you just check for any blocks like this,

In my case, there's a lot of things that might keep you from moving around normally, and that's probably the case for you too. So it lives in a function that's getting called before I let any movement application by the player through. But you get the point, right? That node right there is all you need for this type of AnimNotifyState. For all you know, there's 3 different instances of BlockNormalMovement running at the same time in different animations all overlapping, but, you don't really care- it's just a flag. Is anything saying BlockNormalMovement? Yes? Ok, cool!

Moving on, the next most involved way of using these is the proactive reaching-into-our-owner-and-setting-stuff approach. That looks like this:

Really it's super simple. Most of this logic is just what it takes to reach back and get a reference to the thing playing the montage that contains this AnimNotifyState. That circled node is the only bit of C++ you need here, that same thing I mentioned last time- it just exposes your actual AnimNotifyState class, so that you can get at any arguments you set in the Montage on that state.

The ending of the state is even simpler:

Don't even need the arguments, just get the End call, ok, cool, reach in and cancel whatever you set previous.

Finally, there's the more advanced method. Full disclosure, I'm writing this because I just realized, you know what? Everything should use this method. It's because if you have overlapping states, the poking-arguments-into-things approach can fail, since if you have two overlapped states, one might end midway through when another is still running, but you don't have any concept of "stack" here, just arguments you've poked in. So the one state ends, it reaches in and unsets whatever it set, and oops, that undercuts the other state which was actually still running.

So we do this instead:

Almost identical boilerplate, only difference is this establishes a kind of stack. Multiple AnimNotifyStates of the same class can be running in parallel, all registered, and whatever needs to reach into those and grab arguments can just do whatever it deems best if there's multiples running at a time. It'll vary based on context. The main thing is that this approach happily supports as many stacked states running at once as you care about. You don't care about race conditions, or long delta-time frames that accidentally run the prior state's End after what should have been the next state's Begin, none of that.

Just works, you know? You could even standardize this if you really wanted to, and make your AnimNotifyState used a shared parent class, and have a single registry, and externalize the Add/Remove of states into it as functions so there's more of a clean interface layer and blah blah. I'm not doing any of that. I can count the number of state types I need on both hands, and this boiler plate takes 30 seconds to copy in, and that results in a cleaner interface on the other end too. But you could! I'm not your mom. Do whatever it is in you to do, heh.

Anyways! That was a noisy month, sorry, just wanted to give you a proper catch-up for that couple of months I missed. Should be back to more normal schedule again, now.


More Creators