Controlling animation frame via Lua SOLVED

  • #1, by nenad-asanovicFriday, 29. September 2017, 19:56 7 years ago
    Let me try to explain what I was doing.
    I have "trophy" scene where are some dolls that rotate through 24 frames.
    So I created MANUAL rotation possible and here how I did it (note I have very low knowledge in lua but this works):
    I created "Called by other action" with:
    if value = 1
     Execute script
           getObject("ActiveAnimations[animation_name]"):setValue(VAnimationFirstFrame, 1)
           getObject("ActiveAnimations[animation_name]"):setValue(VAnimationLastFrame, 1)
    End if
    And after that for each frame same (if value 2 first/last frame, 2 etc.
    Added mouse scroll up and down to change value +/-1 and then jump to "Called by other action"

    So, now this works. But i have many trophy's on this scene and I want to make it as simple as it can get.
    My question is: can we somehow via Lua set this to be "Global" script for this scene meaning, can I make  "animation_name" be global in this execute script and somehow call for specific name of each trophy. (as I said I am very basic with lua)
    Here what I meant something like:
    if value/condition=x 
    change "animation_name" to "x"
    so this way there would be only 1 Called by other action" command but would change animation name for current trophy allowing us to control current animation/trophy.

    I apologize if it is a bit awkward explanation, I hope tho someone will understand what I wanted to ask. 

    Thank you in advance.

    Newbie

    59 Posts


  • #2, by afrlmeFriday, 29. September 2017, 22:15 7 years ago
    I assume you want it to rotate the trophy the cursor is over? What you could do is use the new if lua result action part inside of the mouse wheel actions to check if an object is underneath the cursor & if it contains "trophy" inside of the name & if so return true, then you would tell it to update the animation belonging to the object below the cursor by setting the current animation index value plus or minus 1. In theory this should work, but I can't tell you how to write the script or function off the top of my head right now as I'm not in the correct mind space & it's late, sorry.

    You could actually simpify your own method though by having the engine save the object below the cursor when you mouse over a trophy & by giving the animation the same name as the trophy object so that you can do something like...
    local anim = ActiveAnimations[game.SavedObject:getName()]
    
    anim.AnimationFirstFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1
    anim.AnimationLastFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1

    Imperator

    7285 Posts

  • #3, by nenad-asanovicSaturday, 30. September 2017, 00:19 7 years ago
    This works perfectly! Thank you very much. 
    Now I just need to find way to set function when rotate reach last frame to reset to first frame (as it is 360 rotation).

    Newbie

    59 Posts

  • #4, by afrlmeSaturday, 30. September 2017, 00:52 7 years ago
    This works perfectly! Thank you very much. 
    Now I just need to find way to set function when rotate reach last frame to reset to first frame (as it is 360 rotation).


    Wrap it in a query.
    local anim = ActiveAnimations[game.SavedObject:getName()]
    
    if anim.CurrentSpriteIndex < 360 then
     anim.AnimationFirstFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1
     anim.AnimationLastFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1
    else
     anim.AnimationFirstFrame = 1
     anim.AnimationLastFrame = 1
    end

    Just edit the query & frame for the opposite direction.

    P.S: you aren't actually using 360 frames are you?


    Imperator

    7285 Posts

  • #5, by nenad-asanovicSaturday, 30. September 2017, 01:07 7 years ago
    I am crazy perfectionist but not THAT crazy lol. This animations have 24 frame (just enough for human eye to see it as smooth 360 loop rotation) grin. And thanks again I'll try this out later or tomorrow it is getting late here also.

    Newbie

    59 Posts

  • #6, by nenad-asanovicSaturday, 30. September 2017, 01:13 7 years ago
    Yup. Works like charm. Thanx!

    Newbie

    59 Posts

  • #7, by afrlmeSaturday, 30. September 2017, 02:23 7 years ago
    Nice. smile

    Imperator

    7285 Posts

  • #8, by jeffWednesday, 22. August 2018, 21:01 6 years ago

    Wrap it in a query.
    local anim = ActiveAnimations[game.SavedObject:getName()]
    
    if anim.CurrentSpriteIndex < 360 then
     anim.AnimationFirstFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1
     anim.AnimationLastFrame = ActiveAnimations[game.SavedObject:getName()].CurrentSpriteIndex + 1
    else
     anim.AnimationFirstFrame = 1
     anim.AnimationLastFrame = 1
    end

    Just edit the query & frame for the opposite direction.

    So I have tried to execute this code, but I don't understand how to get code executed within VS. This is how I'm going about it. Wondering if you can show me what I'm missing.

    See below. This is linked to an object area above my animation.



    Newbie

    17 Posts

  • #9, by afrlmeThursday, 23. August 2018, 03:10 6 years ago
    hmm... game.SavedObject is the object below the cursor that has manually been saved using the save object action part. Try replacing that with the actual name of the object/animation...
    ActiveAnimations["example"]

    Quick note #1: names are case sensitive.

    Quick note #2: technically the code could have been shortened further - I have no idea why I wrote the currentspriteindex lines out fully when I could have just used anim.CurrentSpriteIndex + 1, seeing as I'd already stored the animation inside of the anim variable.

    Imperator

    7285 Posts

  • #10, by jeffThursday, 23. August 2018, 05:37 6 years ago
    hmm... game.SavedObject is the object below the cursor that has manually been saved using the save object action part. Try replacing that with the actual name of the object/animation...
    ActiveAnimations["example"]

    Quick note #1: names are case sensitive.

    Quick note #2: technically the code could have been shortened further - I have no idea why I wrote the currentspriteindex lines out fully when I could have just used anim.CurrentSpriteIndex + 1, seeing as I'd already stored the animation inside of the anim variable.
    Thanks, I'll try it. Really appreciate your help and patience as I'm learning VS!

    Newbie

    17 Posts

  • #11, by sebastianThursday, 23. August 2018, 08:24 6 years ago
    Just as a hint why your ActiveAnimations string in your example wont work, Jeff:

    You need to out the animation name in the [] brackets. Either as a variable:

    ActiveAnimations[some_lua_var_which_contains_a_string] 

    or string:

    ActiveAnimations["animation_name"] 

    also you canbuse an even shorter method :

    ActiveAnimations.animation_name

    While this is the shortest and most stylish way you cant use variables in the latest method because there is no way to differ if the string after the "." is a string or a variable the engine should look up. So it will search for an animation with that name. 

    The Animation has to run at this moment, otherwise it will not find the object (Thats why its called ActiveAnimations) . 
    When the Animation is set as a default animation of an object or button you wont need to start it manually, of course.. 

    PS:
    The game.savedObject doesn't need to be under the cursor. Its just the saved object regardless of mouse position (but mostly will be set as the object under the cursor, when e. g. opening a verb coin interface). 

    Thread Captain

    2346 Posts