WASD/Arrow movement?

  • #1, by HasuakSunday, 21. December 2014, 03:34 10 years ago
    Is such a thing possible? I'm thinking of making my game a sidescroller since depth and multiple angles for each characters are giving me a serious headache.

    Newbie

    26 Posts


  • #2, by afrlmeSunday, 21. December 2014, 13:16 10 years ago
    Yes, technically this is possible. SimonS previously posted a method for this in an old thread, as to where that thread is in vs forum, I have no clue - probably lost in time or LSpace somewhere! razz

    Basic gist of it was that on key down you keep updating character destination to the end coordinate of the current scene & then on key release you stop the character. Was something along those lines. Actually since vs 4.0 I believe that this can now be refined as it's possible to use lua to get/return the scene/height of the current scene with the new getSize() function.

    Imperator

    7285 Posts

  • #3, by SimonSSunday, 21. December 2014, 14:40 10 years ago
    Since we have a keyhandler now in lua, you can do it purely with lua:
    function switch(c, cases)
      local f
      if (c) then
        f = cases[c] or cases.default
      else
        f = cases.missing or cases.default
      end
      if f then
        if type(f)=="function" then
          return f()
        else
          error("case "..tostring(c).." not a function")
        end
      end
    end
    
    keymask = tonumber("40000000",16)
    eKeyRight = keymask + 79
    eKeyLeft = keymask + 80
    eKeyDown = keymask + 81
    eKeyUp = keymask + 82
    target_pos = {0, 0}
      
    registerEventHandler("keyEvent", "keyHandler")
    function keyHandler(type, char, keycode, mod)
      local oldtarget = {target_pos[1], target_pos[2]}
      if type == eEvtKeyDown then
        switch(keycode, {
          [eKeyRight] = function() target_pos[1] = 1 end, 
          [eKeyLeft] = function() target_pos[1] = -1 end, 
          [eKeyUp] = function() target_pos[2] = 1 end, 
          [eKeyDown] = function() target_pos[2] = -1 end, 
        })
      elseif type == eEvtKeyUp then
        switch(keycode, {
          [eKeyRight] = function() target_pos[1] = 0 end, 
          [eKeyLeft] = function() target_pos[1] = 0 end, 
          [eKeyUp] = function() target_pos[2] = 0 end, 
          [eKeyDown] = function() target_pos[2] = 0 end, 
        })
      end
      if target_pos[1] ~= oldtarget[1] or target_pos[2] ~= oldtarget[2] then
        local newtarget = {}
        if target_pos[1] == 0 then
          newtarget[1] = game.CurrentCharacter.Position.x
        elseif target_pos[1] == -1 then
          newtarget[1] = 0
        else
          newtarget[1] = 1900
        end
    
        if target_pos[2] == 0 then
          newtarget[2] = game.CurrentCharacter.Position.y
        elseif target_pos[2] == -1 then
          newtarget[2] = 1000
        else
          newtarget[2] = 0
        end
        game.CurrentCharacter.Destination = {x = newtarget[1] , y = newtarget[2]}
      end
      return true 
    end
    


    It only works well if the waysystem is rather big, also the character won't walk straight right or but use the waypoints. So not quite optimal, but fits as demonstration.

    Thread Captain

    1582 Posts

  • #4, by afrlmeSunday, 21. December 2014, 15:16 10 years ago
    hmm... I really wish you had added some -- comments to that script.

    You seem to be updating characters position as opposed to updating destination? Thing with updating the position is that it resets character animation to idle on each update no?

    Imperator

    7285 Posts

  • #5, by HasuakSunday, 21. December 2014, 16:58 10 years ago
    Whooooa. I got no understanding nor skills in LUA, but I appreciate the effort. Kinda silly that Visionaire got all sorts of cool stuff on the LUA's side but I can't access them through normal means in events and such.

    Newbie

    26 Posts

  • #6, by afrlmeSunday, 21. December 2014, 18:47 10 years ago
    The script is plug & play as far as I'm aware - I've not tested it mind. Create a new script in the script section & paste the script into it, then see what happens. I think Simon is using arrow keys instead of WASD in this script though.

    P.S: I don't think there will be any general support for character movement via keyboard added to VS any time soon, well at least not until controller support is added.

    Imperator

    7285 Posts