[Solved] Walking with arrow keys

  • #1, by Simon_ASATuesday, 10. January 2017, 15:34 4 years ago
    Hi everyone,

    SimonS posted a script here to walk with arrow keys (keyboard or gamepad).

    This is the script:

    local mouse_x = 0
    local mouse_y = 0
    local charmove_x = 0
    local charmove_y = 0

    function keyboardHandler(eventType, character, keycode, modifiers)
    if eventType==eEvtKeyUp then
    print('key up: ' .. keycode)
    keycode = keycode % 1073741824
    if keycode == 80 then -- left
    charmove_x = 0
    elseif keycode == 79 then -- right
    charmove_x = 0
    elseif keycode == 81 then -- down
    charmove_y = 0
    elseif keycode == 82 then -- up
    charmove_y = 0
    end
    createEvent('eEvtControllerAxisCharacterMove', {x=charmove_x, y=charmove_y},25)

    elseif eventType==eEvtKeyDown then
    print('key pressed: ' .. keycode)
    keycode = keycode % 1073741824
    if keycode == 80 then -- left
    charmove_x = -100
    elseif keycode == 79 then -- right
    charmove_x = 100
    elseif keycode == 81 then -- down
    charmove_y = 100
    elseif keycode == 82 then -- up
    charmove_y = -100
    end
    createEvent('eEvtControllerAxisCharacterMove', {x=charmove_x, y=charmove_y},25)
    elseif eventType==eEvtControllerKeyUp then
    print('controller up: ' .. keycode)
    if keycode == 1000001 then --controller key A up
    createEvent('eEvtMouseLeftButtonDown')
    createEvent('eEvtMouseLeftButtonUp')
    -- createEvent('eEvtKeyDown',{x=0,y=0},eKeyEscape,0)
    end
    elseif eventType==eEvtControllerKeyDown then
    print('controller down: ' .. keycode)
    if keycode == 1000001 then --controller key A down
    end
    elseif eventType==eEvtControllerAxis then
    if string.match(character, 'RIGHTX') then
    mouse_x = keycode
    createEvent('eEvtControllerAxisMouseMove', {x=mouse_x, y=mouse_y}, 19, 9)
    elseif string.match(character, 'RIGHTY') then
    mouse_y = keycode
    createEvent('eEvtControllerAxisMouseMove', {x=mouse_x, y=mouse_y}, 19, 9)
    elseif string.match(character, 'LEFTX') then
    charmove_x = keycode
    createEvent('eEvtControllerAxisCharacterMove', {x=charmove_x, y=charmove_y}, 25)
    elseif string.match(character, 'LEFTY') then
    charmove_y = keycode
    createEvent('eEvtControllerAxisCharacterMove', {x=charmove_x, y=charmove_y}, 25)
    end
    end
    return false
    end

    registerEventHandler('keyEvent', 'keyboardHandler')
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    Anyone tried it? Did it work for you?
    Unfortunately there's no info with the script to explain how to use it.

    For me it doesn't work: when I use the arrows, it will change my character orientation (he will look in the corresponding direction) but he won't move.
    (I just copy/pasted as a definition script)

    ***
    In the meantime I am using another script on each arrow key (left, right, etc. separately) like this:
    game.CurrentCharacter.Destination = { x = game.CurrentCharacter.Position.x, y = (game.CurrentCharacter.Position.y + 200) }
    1

    But the result is far from great and I don't like it, because the character will follow the way points in the scene, which I don't want.

    ***
    Hope someone can save me! I'd love to use keyboard gameplay. Thanks in advance.

    Great Poster

    321 Posts


  • #2, by bananeisafreeTuesday, 10. January 2017, 16:23 4 years ago
    The script works fine with me. Though the characters behaves in a kind of ectic manner when it comes to border collision or pressing buttons in a too quick succession.

    (dumb question : are you sure your char speed is not set to 0 ?)

    Forum Fan

    120 Posts

  • #3, by afrlmeTuesday, 10. January 2017, 16:27 4 years ago
    If you include walk paths inside of the border area then it will automatically calculate the position based on the path to a degree. If you are only creating the paths for scaling then add a path on y-axis outside of your way border - actually recommended to do this anyway.

    I think game pad / keyboard walking support should be better in the next update, or at least if I remember correctly, SimonS said he would rework/come up with a better script. Ideally some built in stuff in the editor would be better, but I guess scripting is still better option as it allows you to tailor for various game pad types, makes & models.

    Imperator

    6846 Posts

  • #4, by Simon_ASATuesday, 10. January 2017, 18:39 4 years ago
    The script works fine with me.

    (dumb question : are you sure your char speed is not set to 0 ?)
    Strange, it definetely doesn't work for me. The speed is 450
    How did you use the script? Definition script? Is that all, or did you write other scripts elsewhere?

    Thanks a lot to both of you!

    Great Poster

    321 Posts

  • #5, by bananeisafreeTuesday, 10. January 2017, 18:57 4 years ago
    I litteraly copy paste the script as a definition script on one of the ved I was working on.

    But as Lee stated, you need to setup a way system (even a crude one) to make the script work.

    When I removed it for test, the character was not able to move. So if you did not setup one, it very well might be the problem !

    Forum Fan

    120 Posts

  • #6, by Simon_ASAWednesday, 11. January 2017, 11:28 4 years ago
    Thank you bananeisafree (lol! "banane is a fruit", with the French accent I just understood)

    In fact there is already a way system... I'm using the script in my prototype of ATOS
    (can be downloaded here: http://gamejolt.com/games/atos/217101)

    Everything was set properly I think. I have checked the script, my scene, the log, the way system, but nothing looks wrong.
    There might be a conflict with another definition script eventually (shaders?) so I'll make more tests and try to disable the other ones.

    I think game pad / keyboard walking support should be better in the next update
    Yes yes, I'm waiting... My last project is finished since May 2016 and I've been looking forward from a long time to try the new version grin

    Great Poster

    321 Posts

  • #7, by bananeisafreeWednesday, 11. January 2017, 13:42 4 years ago
    You are litteraly the first one to figure it out by himself ! (And I've used that nickname for over 16 years !)

    I downloaded the ATOS prototype to see if we were talking about the same thing, and indeed, everything seems to be in order ... 

    If you use other scripts, perhaps you should change the name of each variable in the keyboard script. (all the charmove_ , mouse_ , etc ...) for they may be interfering with other variables you use in other scripts and even perhaps the name of the function (remember to change its name into the register EvenHandler too)

    Though again, I'm merely a neophyte when it comes to scripting ... so everything I say should be taken with a pinch of salt.

    Forum Fan

    120 Posts

  • #8, by Simon_ASAWednesday, 11. January 2017, 19:22 4 years ago
    I tried all I can, but nothing works... The names of the variables are correct roll
    I have no luck with Visionaire, I always face some strange situations like that.

    If SimonS comes around here, I hope he has an idea. I summon you, ô god SimonS! Or I'll get in touch someday with PM eventually...

    Great Poster

    321 Posts

  • #9, by SimonSWednesday, 11. January 2017, 19:38 4 years ago
    Script works for me, even in the 4.2.5 version. Speaking of which there's currently a bug if there are less than 2 shapes in the waysystem if I remember correctly.

    Thread Captain

    1380 Posts

  • #10, by sebastianWednesday, 11. January 2017, 20:40 4 years ago
    do you see omething in the log?
    this line:   print('key pressed: ' .. keycode) should print the keycode when pressing a key...

    Thread Captain

    2337 Posts

  • #11, by Simon_ASAThursday, 12. January 2017, 11:11 4 years ago
    there's currently a bug if there are less than 2 shapes in the waysystem if I remember correctly.
    Thanks Simon! What do you mean by 2 shapes? I have 1 shape for the way borders and 1 for the way points.
    I'll make some tests by creating more shapes in these systems. We'll see if it helps!

    do you see omething in the log?

    this line: print('key pressed: ' .. keycode) should print the keycode when pressing a key...
    No, you're right!! As I noticed before, there's nothing in my log. I thought it was good news, but in fact it might prove there's an issue. I have to find it...
    I try to do everything clean and fix the mistakes in the log as often as possible, so if the log is empty it's difficult to find the problem........

    Thanks to both of you! Let me know if I can help too smile

    Great Poster

    321 Posts

Write post