Animations fail when creating state machine in "Creating an idle and move state"

So, I had my animations working and my sprite was flipping directions but when I did the chapter on “Creating an idle and move state” the implementation caused my animations to stop working. I started over several times and am still having the problem.

When the states are implemented, the project works and I can move the sprite and it even flips directions. But the idle and move animations are not running. So it’s just a flat sprite moving around the screen.

Any clues to start? If not, what would you need to see to help me out.

Based on some of your previous topics, I’m certain you’re able to create the animations themselves without much difficulty. If clicking the play button in the AnimationPlayer works fine and the animations themselves are named, I doubt that’s the problem. More than likely what’s happening is just that the animation’s Play() function isn’t being called when it’s supposed to be. In turn, that would be because a conditional statement somewhere isn’t behaving the way you expect it to.

The first thing I would do in a case like that is control-flow testing. Print statements along the entire processing chain from button-press to end-result (“control flow,” which is why it’s called control-flow testing) can be used not only to confirm that this section of code is being reached, but also to show the values of relevant variables at that moment in time. The results of those tests are used to isolate the problem area and better-refine the exact root cause of the problem. Maybe you’ve already been doing this, but you’d be surprised how many people don’t know about this tactic, so I’ve written it out just in case.

If control-flow testing doesn’t lead you to a solution, it would be great to see the entire control flow that you tested (with script names if you don’t mind, since I know there are multiple different scripts involved here and I’m not very familiar with this project), as well as the results of any control-flow testing you did (ie which print statements correctly/incorrectly fired and what the results of any printed variables were).

1 Like

thanks!

I actually put try/catch blocks and I’m getting this error. Not sure why I would get this. Compile time it’s cool but it doesn’t like it runtime.

I don’t have a whole lot of experience with godot and this seems like something simple that someone with experience would know immediately. Here it is;

Unable to cast object of type ‘Main’ to type ‘Player’. at Godot.Node.GetOwnerT in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs:line 148
at PlayerIdleState._Ready() in D:\Godot Projects\Dungeon RPG\Scripts\Character\Player\PlayerIdleState.cs:line 13

The line of code is:
Player characterNode = GetOwner();

############
Also, i noticed that for some reason my project structure got out of sorts compared to the tutorial. What do you think happened?
This is my view. Look on the left hand side

This is tutorial view:

Could this be a part of the problem?

I’m a C# developer by trade and this stuff is friggin’ baffling…

Thanks a bunch,
Scott

oh one more thing, what ==5001 NEVER happens…Am I supposed to set a node somewhere?

thanks again

Great, this is all very helpful.

Excellent, this is another great debugging tool (which I had completely forgotten about because I’m almost certain they’re not supported in GDScript).

Very cool! That will help with the explanations I’m sure.

I’m pretty sure I see what’s going on in relation to the error message you gave here.

The idea behind the code that happens in IdleState.Ready() is that it’s building a reference to the Player node, and it’s doing that using GetOwner(). That call returns the root of the scene being operated in, and that’s a very important detail. If you look closely at your screenshots, you’ll see that one reason why your scene trees are different is because in the first screenshot (which is of your project), you’re looking at the Main scene, in which the Player is simply instantiated; in the second screenshot (which is of Luis’s project), he has the Player scene open, in which the Player is the root of the scene.

If you’re experienced with C#, I’m sure I don’t need to explain what typecasting is in general. The reason you’re getting this error is because the IdleState script is running in the Main scene, and therefore GetOwner() returns Main, which is a Node3D that is not of class Player (Godot can use class names as object datatypes. Luis talks about this briefly at around the 4m mark in this lecture). Because there is no native way to typecast an ordinary Node3D to this Player datatype, the result is an unhandled type mismatch exception.

Because this lecture is all about migrating existing functionality out of Main and into Player, probably what’s happening is IdleState is running on both scenes and it’s failing on Main, where it isn’t meant to run anymore - keep in mind that both IdleState nodes would be running the same script! Being experienced with C#, I’m assuming you’re familiar with breakpoints too; try stepping through your code to see if this is the case, paying close attention to the script in which each sequential line of the control flow is in.

Either way, if you follow the lecture all the way through to completely finish the functionality migration procedure that Luis demonstrates, there’s actually a very good chance this problem will fix itself.

Someone else had an issue with this, and it is indeed a line that Luis writes himself; however, that’s actually all done in the next lecture. It starts at around 5:30, but I’d rewatch the whole thing anyway so you have the context behind it.

thanks for this info. I’ll take a look

1 Like

oh and, you’ve been amazingly kind with your help. It’s really appreciated. I know sometimes on the boards we can be short but I just wanted you to know I’m very appreciative!

2 Likes

As is this =)

so, I’ve had “half” success. I can change the state machine default to “MoveState” and my character moves. I can change it to Idle state but the animation does not, well, animate.

I’m still getting those cast errors which is weird because you’d think it be able to cast to a CharacterBody3D, right? But it can’t…anyway.

I’m gonna continue on. If someone has an answer or better information, great. If not, please go ahead and close this. If I get more information I’ll pass it along

Well that’s certainly some progress :+1:

It still sounds like there are situations in which the wrong scene’s owner is being targeted due to the PlayerIdleState node being instanced somewhere it shouldn’t be. In your control flow testing, have you tried printing the result of GetOwner() in the PlayerIdleState script? The reason I bring this up is because that result, during the call that breaks the code, is almost certainly not the node it should be. Otherwise the typecast would work.

If you feel that you’ve checked everything and you’re really stumped, all I can personally recommend in addition is to try looking at the commit for this lecture and possibly downloading the finished project as an example. The lecture commit will highlight the additions and deletions directly, allowing you to compare that to your current code, and the example project would show you exactly how each scene’s scene tree needs to be structured.

Good luck, and keep pressing on =)

2 Likes

Hi Scott,

I’ve nothing really to add to @CoreyKnecht’s posts as they have been awesome at helping out here and especially with the site transistion which you can imagine keeps us all incredibly busy.
I did want to touch base however and let you know if you do need a second set of eyes we can have you upload the project and take a look directly.
Also that once the new site is stable the forums will change to a community driven forums and so the teaching assistants will be answering questions on the new platform.

Hope you find a solution to this and if not please do let us know as support will continue here till Friday or may be extended until the new site is stable.

All the best
Marc

1 Like

Interesting, I downloaded the project for that chapter and the same problem (once I fixed a couple of things). It could either show the Idle animation or the Moving animation. By default, at least in that project, it’s the Moving animation.

Knowing that, I’m moving on to the next chapter…Finally!

Thanks for the help. You can close this one now unless anyone else has something they would like to add.

Thanks again! You guys have been great

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms