Global script for "mouse enters/leaves area"

  • #20, by afrlmeWednesday, 18. February 2015, 13:28 9 years ago
    The script works? It could probably have been a lot more compact / global than you have written it, but if it works, then it works. wink

    Imperator

    7278 Posts


  • #21, by LebosteinWednesday, 18. February 2015, 13:49 9 years ago
    I don't know what you mean. I see no way to make it smaller to handle 5 different cursors. It would be smaller if I could check, which cursor is the active cursor... Ok, you could make a loop that checks all cursors:
    local exitcode = getObject("Game.GameCurrentCharacter.CharacterValues[exitcode]")
    local cursors = {}
    cursors[0] = "obj" -- marker for normal objects
    cursors[1] = "exit_l" -- marker for left exits
    cursors[2] = "exit_r" -- marker for right exits
    cursors[3] = "exit_u" -- marker for up exits
    cursors[4] = "exit_d" -- marker for down exits
    
    function setCursor(n)
        if exitcode.Int == n then return end -- change cursor only if it is needed
        startAction("Game.GameCurrentCharacter.CharacterActions[cursor_" .. cursors[n] .. "]")
        exitcode.Int = n
    end
    
    function autoCursor()
        if game.CurrentObject:getId().tableId == eObjects then
            for n = 0, 4 do if string.find(game.CurrentObject:getName(), cursors[n]) then setCursor(n) end end
        else
            setCursor(0)
        end
    end
    
    registerEventHandler("mouseEvent", "autoCursor")
    

    Key Killer

    621 Posts

  • #22, by afrlmeWednesday, 18. February 2015, 15:17 9 years ago
    Indeed. You could also use a single cursor & add all the animation frames for each of the different cursors to the active animation part & then use Lua script to force which frames should be played / looped.

    I guess there are multiple ways to approach this.

    P.S: your mouse event handler function isn't quite right. In your example it is executing the code regardless of which event is triggered. Please see example 2 of this page. How you have written the registerEventHandler function is fine because if you don't include any flag events then it will automatically declare all events available.

    P.P.S: technically Lua index tables start with an index value of 1, but you have forced it to start with 0. Also because you are using index values there was no reason to write each cursor name on a new line.

    local t = { "obj", "exit_left", "exit_right", "exit_up", "exit_down" }
    
    function setCursor(n)
     startAction(Actions["cursor_" .. n])
    end
    
    function onMouseEvent(eventType, mousePosition)
     if eventType == eEvtMouseMove and game.CurrentObject:getId().tableId == eObjects then
      for i = 1, #t do
       if string.find( game.CurrentObject:getName(), t[i] ) and Values["exitcode"].Int ~= i then setCursor(i); break end
      end
     end
    end
    
    registerEventHandler("mouseEvent", "onMouseEvent")
    

    ...untested. There's no reason to directly access said values, as you should only need 1 instance of them, which you could create anywhere you like.

    Imperator

    7278 Posts