2D Toolkit Forum

2D Toolkit => Support => Topic started by: drkucho on May 07, 2014, 10:50:22 pm

Title: Script Execution order and Animation Triggers
Post by: drkucho on May 07, 2014, 10:50:22 pm
Hello !

i have set my game like this:

one script called CC ( character controller) that controls each sprite, thinking what to do on collisions , update x,y position , etc , this script also set the next script AT as delegate for tk2d Animation Triggers
then the second script called AT (Animation Triggers) that handles all tk2d animation triggers, i have set a text command system, so the frameInfo is the command, and then frameInt and frameFloat are values for this commands

there is one command , the "X" command, which means "move the sprite on the X coordinate" using frameInt value
i do this kind of movement for special cases, as there are some animations that go better if they move one or two pixels only on certain frames

so AT script its calle with the tk2d anim frame triggers, it identifies the command X , and it does not change X position directly but sets a variable of CC script called CC.frameIncX with the frameInt value
then CC script at the end of Update() , among other things, changes the sprite/gameObject transform.position by summing frameIncX (that has been set by AT)

the modification of transform.position needs to be done at the end of CC because there a some more complex stuff going on on this script, including more ways to move the sprite that are all combined to work together,  so it is better to modify the transform.position only once here, by summing all the other movements in just one increment variable and then change transform.position at once.

it works, but there is a problem, the sprite frame gets displayed on screen but the frameIncX value is not summed on this unity "screen frame", the x position is updated on the next frame, so you see the sprite glitching its position ,if the increment is just one pixel you don't appreciate it , but i have a situation i need to jump 4 pixels so that frame of the sprite gets displayed 4 pixels shifted for one unity frame time, and then goes back to its good position on the next one

i have tried to change the script execution order by moving CC and AT right after tk2dSpriteAnimator, tk2dSprite scripts,  and also before them, but no luck , no matter what i do it doesnt change anything apparently

basically i need all tk2d sprite animation scripts + my CC and AT scripts to be executed all before unity updates the screen

hope i was clear enough and you have solution
Title: Re: Script Execution order and Animation Triggers
Post by: unikronsoftware on May 08, 2014, 04:16:11 pm
The easiest (but not necessarily the most desirable) workaround to this is to have another script that runs after AT, so CC sets the value on the frameIncX on that script and that script is guaranteed to run after that.

A neater way of doing this is to have a singleton behaviour (SB) that runs after animation lateupdate. The AT sets frameIncX on the CC script, then appends the CC to a global list of things that need this call. SB.LateUpdate runs through the list and calls the appropriate function to update positon.
Title: Re: Script Execution order and Animation Triggers
Post by: drkucho on May 08, 2014, 05:38:32 pm
The easiest (but not necessarily the most desirable) workaround to this is to have another script that runs after AT, so CC sets the value on the frameIncX on that script and that script is guaranteed to run after that.

..to run after…what? after CC or AT? , i don't understand... how would that help? in the way i see it the problem is i need to place the command that updates position before unity updates the screen and in the same unity frame that the tk2d scripts are being run, but seems that the tk2d script that prints the sprite frame happens on the precious unity screen update and my code in the next screen update , is this really happening this way? if so… how to change it?
Title: Re: Script Execution order and Animation Triggers
Post by: drkucho on May 08, 2014, 05:49:16 pm
it does happen like that, i am testing going frame by frame now with the unity "frame" button and i see the sprite frame is updated in one frame, but won't move, then i click again to see the next frame and  the sprite position is updated

so my AT , my CC or both are being run not in the same unity screen update than the tk2dSpriteAnimator ( i guess ) … why? :-\
Title: Re: Script Execution order and Animation Triggers
Post by: drkucho on May 08, 2014, 06:47:25 pm
i think i found what could be the reason, been looking inside tk2dSpriteAnimator.cs and i found this

   public virtual void LateUpdate()
   {
      UpdateAnimation(Time.deltaTime);
   }

... thinking fast it makes sense this is the cause of the glitch cause my position update is made during Update() so the new sprite frame with the X increment inside the frame trigger is processed after i update the position , and this is wrong, should be BEFORE , in the way it is now, the X increment is set , but its not processed until the next frame!

is there any reason why the animation needs to be updated on LateUpdate ?
Title: Re: Script Execution order and Animation Triggers
Post by: unikronsoftware on May 08, 2014, 06:53:14 pm
You can change it from LateUpdate to Update if you want, it may help in your issue here. It was a good place for what we used it for, but might not be for you. The whole sequencing thing in Unity is a mess, you're just shifting the issue - wait till you need to react to moving the sprite after the animation trigger has fired :) As I mentioned in the previous post you can build a simple system to get almost explicit control, I would prefer that over most other options.
Title: Re: Script Execution order and Animation Triggers
Post by: drkucho on May 08, 2014, 08:25:47 pm
thanks , i didn't want to change it without your ok to avoid things going crazy

and well, I'm afraid i did not understand the workarounds you proposed  ???
Title: Re: Script Execution order and Animation Triggers
Post by: drkucho on May 08, 2014, 09:17:11 pm
i changed it and it works, so happy,  thanks again for the support