Arrow-Key-Movement Question

  • #1, by CrossWednesday, 11. March 2015, 17:43 9 years ago
    If i could kindly bother the community with another lua-question?
    I tried to make the character walk with the arrow keys with this script bound to an key-action (put together from pieces by AFRLme). This is for movement to the right:

    local char, pos
    -- * function for getting character position * --
    function setCharPos()
    char = game:getLink(VGameCurrentCharacter) -- get current character
    pos = char:getPoint(VCharacterPosition) -- stores character position into an x,y table
    -- * --
    game.CurrentCharacter.Destination = { x = pos.x + 20, y = pos.y }
    end
    setCharPos()

    It works fine with one exception: after the character has moved the first 20 pixels, he stops for a brief moment. If you keep pressing the key, he will start walking again and never stop again till he reaches the end of the walkarea. I just can't understand why he stops the first time.
    Any ideas?

    Newbie

    92 Posts


  • #2, by afrlmeWednesday, 11. March 2015, 18:25 9 years ago
    You should make it so that it sets the destination to the edge of the screen as opposed to + 20 or whatever. That way it will keep moving smoothly. Then to stop character you add stop character action part to the release key.

    Did you know that in 4.x that you can query the width & height of a sprite? This means that you can return the exact size of each scene, which would give you the right & bottom coordinate values. Left & top would both be 0.

    Here's a quick function for defining the size of the scene into a global variable...

    1. add this as a definition script to the script tab.
    local pos, swh -- empty variables
    
    -- * function which stores scene width & height into the swh variable * --
    function getSceneSize()
     swh = getObject("Game.GameCurrentScene.SceneSprite"):getSprite(VSpriteSprite):getSize()
    end
    
    -- * function which sets character destination (1 = left, 2 = right, 3 = up, 4 = down) * --
    function moveChar(i)
     pos = game.CurrentCharacter.Position
     -- + --
     if i == 1 then game.CurrentCharacter.Destination = { x = 0, y = pos.y }
      elseif i == 2 then game.CurrentCharacter.Destination = { x = swh.x, y = pos.y }
      elseif i == 3 then game.CurrentCharacter.Destination = { x = pos.x, y = 0 }
      elseif i == 4 then game.CurrentCharacter.Destination = { x = pos.x, y = swh.y }
     end
    end
    

    2. to update the current scene size variable, create an execute a script action part containing... (this should only be called once at the beginning of each playable scene)
    getSceneSize()
    

    3. to move character, create an execute a script action part containing...
    moveChar(1) -- replace 1 with direction you want to move in. (1 = left, 2 = right, 3 = up, 4 = down).
    


    I have no idea if it will work or not.

    P.S: I recommend adding a short pause to the key pressed actions - something like 50 to 100ms because key pressed acts like the turbo function on a gamepad, as in it constantly loops through the actions really fast while the key is pressed which is not necessarily a good thing.

    Imperator

    7278 Posts

  • #3, by CrossWednesday, 11. March 2015, 19:13 9 years ago
    Thanks again for the help, AFRLme,
    but unfortunately this doesn't work. The destination set has to be inside of the walk area or the character won't move at all.
    The movement with the +20-script is actually very smooth, unfortunately the character stops just once after the first step.

    Newbie

    92 Posts

  • #4, by afrlmeWednesday, 11. March 2015, 19:20 9 years ago
    Hmm the destination can be whatever you want it to be. The engine should automatically convert destination into the nearest coordinates inside of your way system borders if the walk area is outside.

    It will not auto-calculate if you have not defined any way points though. If there are no way points then the destination value has to be a coordinate inside of the way border polygon.

    In the upcoming release there is a new function which allows you to check if something is inside of a specified polygon.

    Imperator

    7278 Posts

  • #5, by CrossWednesday, 11. March 2015, 19:30 9 years ago
    I checked by simply setting an action "send character to destination". If the destination is outside the walk area, the charcter won't move, no matter how many waypoints there are.

    Newbie

    92 Posts

  • #6, by afrlmeWednesday, 11. March 2015, 19:35 9 years ago
    What happens if you click your mouse outside of a walkable area?

    Imperator

    7278 Posts

  • #7, by CrossWednesday, 11. March 2015, 19:38 9 years ago
    The character doesn't move.
    edit
    Hm, seems to be my problem, since i just tried it again in the (old) demo version and it works.
    I'll try your script again in a different setting.
    Thanks so much for now.

    Newbie

    92 Posts

  • #8, by afrlmeWednesday, 11. March 2015, 20:04 9 years ago
    That's something to do with the paths. They seemed to be more flexible in 3.7.1 to me. In 4.x the character does actually seem to follow the path lines more strictly.

    Just tested & the same happens to me in regards to the movement. It moves a bit then resets itself then carries on moving ok. If I set the destination to move by a larger number (say 100px) & reduce or remove the pause time, then it seems to move a lot smoother. It's very strange. The glitch could be because the animation is not loaded fully on the first cycle?

    There's a problem with using larger offset values too... if the value goes past the way border then the character doesn't move. I wonder if it would work better if you triggered a Lua loop handler for updating the characters destination position while key is pressed? I think the Lua loop handlers loop faster than engine loops, which means you might be able to get away with smaller movement values. Or you could possibly create a grid out of way points. Not sure which would be the better method.

    Imperator

    7278 Posts

  • #9, by CrossWednesday, 11. March 2015, 20:37 9 years ago
    I just tested your whole script in the demo game. It works perfectly fine, smooth movement, character stops at the border of the walk area.

    I had set up my own project with "minimal configuration" to get going quickly, but seem to have to have a look at the actual settings that have been done that way. Maybe i'll figure out what the difference is.

    Newbie

    92 Posts

  • #10, by afrlmeWednesday, 11. March 2015, 20:54 9 years ago
    The only thing I know is that it's down to the paths somehow. It seems daft that it can't pick the closest point to the way system border depending on the destination coordinates without paths. The current path system will probably have to be overhauled at some point as the plan is to eventually make VS more versatile so that other game genres such as platformers & RPG games can be made with it, without having to create painful workaround solutions to the various limitations.

    Imperator

    7278 Posts

  • #11, by JoelWednesday, 11. March 2015, 22:24 9 years ago
    Hey! Sorry to chime in, i dont really have a solution for you but i want to make a Keyboard-Controlled Character myself. It should only move on the X axis and along paths. I took inspiration from the "Cat Lady" Interface. Im wondering, if as an additional feature it would be possible to let the Character run while holding down Shift (with a separate Animation and moving 50 pxs instead of 20 for example). Also, how will you set it up so the Player can interact with Hotspots? Will they get active once the Character moves close? Is this possible?

    Thanks to all! I know the Devs have a lot to do and i appreciate all their hard work but this feature is so often asked about in the Forum, would it be easy to implement in the WYSIWYG Portion of the Software? I think thatd be really neat smile Not like a full blown Jump N Run Functionality but a simple Keyboard Control Setting along one axis would be great already!

    Thanks!

    Forum Fan

    129 Posts