Toggle Script When Not in Cutscene?

  • #1, by ygmantellTuesday, 03. October 2017, 20:48 3 years ago
    I apologize if the title doesn't make any sense, but I'm using the script pasted below to have the character move with the arrow keys.  My problem is that it still takes input and the character can move even during a cutscene or when the cursor is hidden.  
    Is there a way to change the script to have it only work when the cursor is shown and not in a cutscene?  
    I'm not too great at scripting with VS, so any help would be most appreciated.

    - On an unrelated note, if there is a way to have it so the character can move with the arrow keys and WASD, That would be great.
    Thanks so much!

    The script was originally found here: 
    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

    Great Poster

    274 Posts


  • #2, by afrlmeWednesday, 04. October 2017, 00:13 3 years ago
    It's kind of complicated. I simplified my thinking & thought I saw an obvious solution, but game.HideCursor still returns false when it's hidden unless manually hidden. If Simon could have that field updated whenever a non-background display/narration text is active, or when a begin/end cutscene wrapper or a hide/show cursor wrapper is used then it would make things much easier.

    You could manually sort it out by using a condition or Lua variable which you update each time a text is started/ends or when a cutscene or hide/show cursor wrapper is used, but it unfortunately means more work for you as you'll have to manually create the actions each time you need it yourself. Sorry.

    Imperator

    6842 Posts

  • #3, by ygmantellWednesday, 04. October 2017, 00:18 3 years ago
    Got it, no problem!  For this game, it's not so important.

    But, and I apologize if this should be in a thread of its own, but is there a way to add input to the script, so it uses the arrow keys, and WASD?

    Thanks so much, I really appreciate your help!

    Great Poster

    274 Posts

  • #4, by afrlmeWednesday, 04. October 2017, 00:23 3 years ago
    Yeah unfortunately I don't remember the keycodes/names, so you might be better off creating a new test ved & including this script (below) which will print off the names/codes of any keys you press to the log file which you can use to figure out what you need.
    function keyboardHandler(eventType, character, keycode, modifiers)
      if eventType==eEvtKeyUp then
        print('key up: ' .. keycode)
        -- test for '0' with character parameter
        if character == '0' then print('0 released') end
        -- another option to test '0' key
        if keycode == 48 then print('0 released') end
      elseif eventType==eEvtKeyDown then
        print('key down: ' .. keycode)
      elseif eventType==eEvtKeyTextInput then
        -- this will also show more 'complex' unicode characters when multiple keys are used to generate a single character (e.g. Chinese characters)
        print('input: ' .. character)
      end
      if keycode == eKeyEscape then
        -- event will not be handled by engine. this means that also cutscenes can't be skipped with Escape key
        return true
      end
      return false -- key event will also be handled by engine
    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

    Sorry, been quite a while since I messed with the keyevent handler & I doubt I still have the Lua file David gave me which listed the keycodes, names & modifiers.

    Imperator

    6842 Posts

  • #5, by ygmantellWednesday, 04. October 2017, 00:45 3 years ago
    Got it, thank you.  I could be wrong, but arent KeyCodes universal for Lua? Like, just googlable? Or are they specific to VS?

    I apologize, but my scripting knowledge for VS is next to none (as you may know from my other posts...)

    Adding WASD input be just changing this: (for example)

    if keycode == 80 then -- left 
       charmove_x = 0
    1
    2

    To this: (XYZ, being whatever keycode A is)

    if keycode == 80 OR XYZ then -- left 
       charmove_x = 0
    1
    2
    3

    ?

    Thanks so much!

    Great Poster

    274 Posts

  • #6, by ygmantellWednesday, 04. October 2017, 17:21 3 years ago
    Okay, so I tried it, adding in the correct keycodes found with the script you provided, but when add them in, now every key, WASD and arrow keys, just make the player move left.  Any ideas?  A snippet of the edited code is below.
    Thanks so much!

    EDIT: And the log is correctly identifying the keys by their code

    function keyboardHandler(eventType, character, keycode, modifiers)
     if eventType==eEvtKeyUp then
      print('key up: ' .. keycode)
      keycode = keycode % 1073741824
      if keycode == 80 or 97 then -- left
       charmove_x = 0
      elseif keycode == 79 or 100 then -- right
       charmove_x = 0
      elseif keycode == 81 or 115 then -- down
       charmove_y = 0
      elseif keycode == 82 or 119 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 or 97 then -- left
       charmove_x = -100
      elseif keycode == 79 or 100 then -- right
       charmove_x = 100
      elseif keycode == 81 or 115 then -- down
       charmove_y = 100
      elseif keycode == 82 or 119 then -- up
       charmove_y = -100
      end
    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

    Great Poster

    274 Posts

  • #7, by sebastianWednesday, 04. October 2017, 18:22 3 years ago
    look into your log. therl script should print rhe keycode you just pressed. 

    Thread Captain

    2337 Posts

  • #8, by ygmantellWednesday, 04. October 2017, 18:29 3 years ago
    Yes, I understand that, but for some reason the character moves left no matter which key I press.

    Great Poster

    274 Posts

  • #9, by sebastianWednesday, 04. October 2017, 18:35 3 years ago
    ah, sorry. Misunderstood the situation.
    Lets gimme a hint:

    Is "if keycode == 80 or 97 then... " correct lua? wink 

    Thread Captain

    2337 Posts

  • #10, by ygmantellWednesday, 04. October 2017, 18:38 3 years ago
    Is it not?  From what I know of Lua, the Or operator should work fine in that instance...?  But, I could be wrong as my scripting knowlege is not up to par smile 

    Thank you for your help!

    Great Poster

    274 Posts

  • #11, by sebastianWednesday, 04. October 2017, 21:38 3 years ago
    "if keycode==80"  is allright, but whats with the statement "97" ? it stands for itself. Its like saying "if keycode ==80 or true" . So it will be always be triggering the first if branch..
    make it
    "keycode==80 or keycode==97". 

    Thread Captain

    2337 Posts

Write post