Moving an animation in a loop via LUA

  • #1, z marvelSaturday, 06. July 2013, 13:29 hodinky 12 years ago
    Hey there,
    i really would like to know how i can move an existing animation up and down in a loop via lua. Help very much appreciated! wink

    Zabiják klíčů

    599 Posts


  • #2, z AlexSaturday, 06. July 2013, 16:33 hodinky 12 years ago
    No need for Lua. You can use the action part 'change position of an animation' (e.g. at the first/last frame of the animation). If it has to be Lua then just set the field VAnimationCurrentPosition accordingly.

    Čestný člen

    378 Posts

  • #3, z afrlmeSaturday, 06. July 2013, 18:11 hodinky 12 years ago
    he wants it to move up & down smoothly...
    which means increasing/decreasing position in x direction gradually.

    I've started on a script but for some reason it's being an asswipe & is not adding any errors to the log on why it's deciding not to move the animation.

    maybe it's the current build I'm writing script in - will test in 3.7.1 in a wee bit wink

    Imperátor

    7285 Posts

  • #4, z afrlmeSaturday, 06. July 2013, 20:00 hodinky 12 years ago
    ok it's working now (I started writing off the top of my head inside of sublime so I ended up forgetting a few things like adding value of "x" to the VAnimationCurrentPosition & also forgot to use 2 == on an if query line)

    all working now though.

    script 1: the mainLoop handler & additional setup. (definition script)
    -- init values for table tblMainLoop
    local tblMainLoop = {}
    tblMainLoop["vlf_scene"] = 0
    tblMainLoop["vlf_state"] = 0
    
    -- * let's create a function for updating the if query values for the mainLoop function for the vLoopFunc() function
    function vl_toMainLoop(scene, state)
     tblMainLoop["vlf_scene"] = scene
     tblMainLoop["vlf_state"] = state
    end
    
    -- * let's create the onMainLoop function which handles all of our loop requests * --
    function onMainLoop()
     if tblMainLoop["vlf_state"] ~= 0 and game:getLink(VGameCurrentScene):getName() == tblMainLoop["vlf_scene"] then -- check if state isn't 0 & current scene is set scene else do nothing
      vLoopFunc()
     end
    end
    
    -- * let's create the mainLoop handler * --
    registerEventHandler("mainLoop", "onMainLoop")
    

    script 2: the generic movement script (definition script)
    --[[
    Generic Vertical Loop Script for Marvel
    Written by AFRLme
    -- * --
    label@alternatingfrequencies.com, aim/skype: AFRLme
    --]]
    
    -- * let's create some init variables for the script * --
    local min = 0 -- highest point of y on scene you want animation to move to
    local max = 0 -- lowest point of y on scene you want animation to move to
    local speed = 0 -- set speed of animation movement (add a value between 1-10)
    local state = 0 -- the direction the animation should move to (0=static, 1=up, 2=down)
    local scene = 0 -- add the scene containing the animation 
    local anim = 0 -- link to the animation you want to move
    local current_scene = 0 -- check the current scene name
    
    -- * let's create a function for setting up the script * --
    function vInitSetup(mn, mx, scn, ani, spd, sta)
     min = mn -- set min value position (top)
     max = mx -- set max value position (bottom)
     scene = getObject("Scenes[" .. scn .. "]"):getName() -- set scene
     anim = getObject("ActiveAnimations[" .. ani .. "]") -- set animation
     -- * --
     if spd < 1 then spd = 1 end -- check speed isn't less than 1
     if spd > 10 then spd = 10 end -- check speed isn't more than 10
     speed = spd -- set speed
     -- * --
     if sta < 0 then sta = 0 end -- check state isn't less than 0
     if sta > 2 then sta = 2 end -- check state isn't more than 2
     state = sta -- set movement direction (static, up, down)
     -- * --
     vl_toMainLoop(scene, state) -- send required if query variables to another function we can use in onMainLoop function
     -- * --
     --print("min: " .. mn .. ", max: " .. mx .. ", speed: " .. spd .. ", state: " .. sta .. ", scene: " .. scn .. ", animation: " .. ani)
     --print("scene: " .. scene:getName() .. ", current scene: " .. current_scene .. "!")
    end
    
    -- * let's create a function for adjusting speed animation moves up & down (value between 1 & 10)
    function vSpeedVal(i)
     if i < 1 then i = 1 end -- check i isn't less than 1
     if i > 10 then i = 10 end -- check i isn't more than 10
     speed = i
    end
    
    -- * let's create a function for adjusting direction animation should move to (0=static, 1=up, 2=down) * --
    function vLoopState(i)
     if i < 0 then i = 0 end -- check i isn't less than 0
     if i > 2 then i = 2 end -- check i isn't more than 2
     state = i
    end
    
    -- * let's create the vertical loop function which we will be using to move the animation up & down * --
    function vLoopFunc()
     local tblAnim = {} -- create local table
     tblAnim["_temporary_"] = "" -- set table as temporary
     tblAnim["pos"] = anim:getPoint(VAnimationCurrentPosition)
     -- * check if state is 1 & animation position is more than min pos & if so then make it move * --
     if state == 1 and tblAnim["pos"].y > min then anim:setValue(VAnimationCurrentPosition, {x=tblAnim["pos"].x, y=tblAnim["pos"].y - 1}) end
     if state == 1 and tblAnim["pos"].y <= min then state = 2 end -- check if animation position is less than or equal to min pos & change direction
     -- * check if state is 2 & animation position is less than max pos & if so then make it move * --
     if state == 2 and tblAnim["pos"].y < max then anim:setValue(VAnimationCurrentPosition, {x=tblAnim["pos"].x, y=tblAnim["pos"].y + 1}) end
     if state == 2 and tblAnim["pos"].y >= max then state = 1 end -- check animation position is more than or equal to max pos & change direction
    end
    


    P.S: you are only allowed one of each event handler per project, so if you are already using a script which contains a mainLoop handler then you need to add the first part of the script into wherever the mainLoop function is located & also copy/paste the content inside of the "onMainLoop()" function into whatever your mainLoop function is called & then remove the registerEventHandler line & the onMainLoop part of my script.

    Also before this script will work you need to create an execute a script action part in an at begin of scene action for each scene in which you want to move an animation up/down...
    execute a script: vInitSetup(mn, mx, "scn", "ani", spd, sta)
    

    mn = integer value (highest point of scene you want animation to move up towards)
    mx = integer value (lowest point of scene you want animation to down towards)
    scn = string value ("write_scene_name")
    ani = string value ("write_animation_name")
    spd = integer value (speed animation should move on y axis (number between 1 & 10)
    sta = integer value (direction animation should move: 0=static, 1=up, 2=down)
    -- example
    vInitSetup(300, 600, "1_first_scene", "skully_1", 3, 1)
    

    Imperátor

    7285 Posts

  • #5, z marvelSunday, 07. July 2013, 12:53 hodinky 12 years ago
    Whooo... its working Lee! smile

    This thing gets us loads of new possibilities like moving waves, flying birds, moving trees by wind and so on.

    Thank you so much!

    Zabiják klíčů

    599 Posts

  • #6, z marvelSunday, 07. July 2013, 13:16 hodinky 12 years ago
    You like to test this script? Ive made a small example for you! Maybe we can add some more flexibility together.

    Download it right here:

    Zabiják klíčů

    599 Posts

  • #7, z afrlmeSunday, 07. July 2013, 13:29 hodinky 12 years ago
    will test it later on this afternoon.
    my mum is back the day from her trip to uk.

    also the script I wrote is somewhat simplified for single animation per page...
    a better idea would be to use tables to add/remove animations & there settings so we could control multiple animations on each scene.

    P.S: have you checked out "divo"'s random flight script?
    It's really good for animating bird flight & you could combine it with action areas so that the bird(s) would fly to a random area when you enter action area & come back after x time when you leave the action area.

    P.P.S: because there's no actual pause/delay function command we can use for VS without a lot of arsing about we can only control the speed so much ...
    an alternative method would be to create a called by other action part containing an if else query, a pause action part & a condition or value which we can use to determine how often the mainLoop or a function inside of the mainLoop should be triggered. We would also use jump to x action part to loop it. & call the action part via the Lua script after each time the script has moved the animation by whatever frames you set the speed variable.

    Personally I would really like it if we could set delay & loop values to functions in Lua, like the custom event handler/listener scripts that Dundil has been working on (check vs wiki - above)

    Imperátor

    7285 Posts