Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateMachine has delay which is caused by transition from Start to the first state needs one frame #91215

Closed
Watersilver opened this issue Apr 26, 2024 · 8 comments · Fixed by #94372

Comments

@Watersilver
Copy link

Tested versions

  • Reproducible in: 4.2.2.stable

System information

Windows 10

Issue description

Animation in state machine that is configured to play only once and then transition to another animation lags for a few ticks on its final frame.

When I set callback_mode_process to manual and make multiple animation tree advance calls though lag gets reduced and starting at five times there is not lag at all.

Expected: When animation ends instantaneously move to next animation without lag.

Steps to reproduce

When you run the MRP the output prints the animation, its frame and how many ticks the frame lasted.

When you turn around until the run_turn animation transitions to the run animation you should see something like: player run turn | frame: 7 | lasted: 6 This is the final frame of the player run turn animation and the correct non laggy version.

Select the player character scene and change the anim_tree_advance_steps property to select how many times to call the animation tree advance method.

If you set it to 1 the behavior is the same as if we didn't use the manual animation tree advance method and callback_mode_process was set to idle or physics.

now if you turn around again to transition from run_turn -> run you will probably feel the delay between the transition but if not you should see something like: player run turn | frame: 7 | lasted: 8 = Two ticks of lag for the final frame of the player run turn animation.

I get these results consistently. 6 for non laggy version 8 for laggy but it might be machine dependent.

Minimal reproduction project (MRP)

AnimTreeIssues.zip

@TokageItLab
Copy link
Member

TokageItLab commented Apr 27, 2024

The end of the State is not delayed, but it seems that the start of the next State is delayed by processing one frame to transition from the Start state to the first state. I assume that we can eliminate this delay if the transition is not broken.

The MRP in follow indicates that a total of 10 frames of animation progressing at 1 FPS, but if there are two layers in the StateMachine, it will take 12 frames to end.

state_frame.zip

@TokageItLab TokageItLab added this to the 4.x milestone Apr 27, 2024
@TokageItLab TokageItLab changed the title Animation tree state machine switch_mode: at_end transition 2 frames lag StateMachine has delay which is caused by transition from Start to the first state needs one frame Apr 27, 2024
@elXill

This comment was marked as off-topic.

@TokageItLab

This comment was marked as off-topic.

@TokageItLab
Copy link
Member

TokageItLab commented Jul 13, 2024

@Watersilver I sent #94304 so please check it out, as there should appear to be no longer a clear delay that we can visually notice.

However, for some reason the numbers that MRP prints are the same as before. It might be worth taking a video, or slowing down the Process speed by setting a limit on FPS in ProjectSetting to see if there is a difference when comparing the two (before or after PR). If there is no difference there, probably MRP is including something in the if-conditions for printing that has nothing to do with visuals.

And then, If there is no problem in practicality, I think this issue can be closed.

@Watersilver
Copy link
Author

I will check it out today and let you know

@Watersilver
Copy link
Author

Ok so I gave it a look and the issue seems to still be there. It's probably caused by something other than what you fixed. To be honest, I can't even see the delay in the MRP anymore. I had spent so much time looking at it when I opened the issue that it was easily apparent to me but now I can't really see it.

I started wondering if I gaslit myself into believing it in the first place so I made a better MRP that compares a state machine animation with an animated sprite 2d animation and it can easily be seen that they become out of sync:

TestAnimTreeIssue.zip

The right side animation uses a state machine and the left one just an animated sprite 2d. I understand that there's no guarantee that they should give me the same result and I've already made bad assumptions about how the state machine works in the past.

However there is something that makes me believe that they'd give me the same result if there was no issue. Again, like in the first MRP, I set the animation tree callback process mode to manual and I call advance from within the process and I can see that the more times I call advance the closer they get synced.

Some new stuff I noticed with the side by side comparison:

  • it doesn't ever get 100% synced like I thought before.
  • it gets desynced even where there is a normal transition between animations. Previously I thought that the culprit was the at_end switch mode. It's not that, or at least it's not just that.

To use the new MRP:

  • press the right key to run the turn->run animation
  • you can check the no_turn checkbox for both animations to transition from idle animation to run without the transitional turn animation (to confirm that they still get desynced)
  • you can change the steps exported variable of the animation tree node to control how many times advance runs when callback_mode_process is set to manual

Also I checked the code that prints the numbers and I can't find a mistake. I think the numbers printed are correct but the delay is not easily noticable. Basically the code that prints them is an accumulator that increases every tick and becomes zero and prints the result when the animation changes. I've run it in both _physics_process and _process with the same results.

I've moved away from animation trees for my project so I don't really care if you close the issue.

The problem I can see with this kind of stuff is that there seems to be some uncertainty about how exactly the state machine runs animations and it's unfit for syncing with other animations. I would only use it for stuff where details aren't too important.

If that's what the animation tree tool is supposed to be used for that's alright. If on the other hand it's meant to be used for detailed animations it should give consistent results no matter how many times advance is called and there should be docs detailing its behaviour.

Also, do we know what happens in the following scenario: We have one animation with 100 frames and a state machine with 50 animations of 2 frames which are ordered to transition at_end to the next until they reach the last. Does it stay in sync? If it doesn't then the animation tree is not a good tool to create and combine different animations because while in the original example the delay cannot be really seen, the error is systematic and it would add up after 50 transitions causing a clear delay. (I don't have time to check this right now, however I might make it depending on your reply.)

@TokageItLab
Copy link
Member

TokageItLab commented Jul 14, 2024

Ah, I understand perfectly. The following project is a simple 9 second synchronization of 3 chained animations and 1 continuous animation, and I can confirm that the problem is still there after fix #94304. As I checked the StateMachine state, I think it comes from the fact that the 0th frame is being processed after the transition.

state_frame.zip

This is the same problem as the recommendation to use advance(0) if you do not want to process the 0th frame when AnimationMixer playback started. We may need to add some option to AnimationNode to handle this in each Node.

@TokageItLab
Copy link
Member

Alright, I sent #94372.

As I wrote there, although there could still be problems depending on fps and animation length due to floating point precision errors, but it should solve the main problem of this issue, and I think we can close this issue.

@fire fire moved this to Work in progress in Animation Team Issue Triage Nov 10, 2024
@github-project-automation github-project-automation bot moved this from Work in progress to Done in Animation Team Issue Triage Nov 13, 2024
@AThousandShips AThousandShips modified the milestones: 4.x, 4.4 Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
4 participants