RegisterEventHandler(explained)

  • #1, by afrlmeThursday, 03. January 2013, 23:01 13 years ago
    can anyone make sense of the syntax found in this page here?

    I get the mainLoop events & so on but the mouseEvent syntax makes absolutely no sense to me at all ... it looks like bloody gibberish!!!

    ok so I understand:

    registerEventHandler('mouseEvent', 'function_name')
    but how do I add one of the eventFlags into this; "eEvtMouseWheelDown" for example?
    
    -- * this syntax example is not very clear at all! * --
    registerEventHandler(event, eventHandler [, eventFlags] )
    
    much appreciated, Lee smile

    Imperator

    7289 Posts


  • #2, by afrlmeFriday, 04. January 2013, 01:57 13 years ago
    ignore me bloody post ... I figured it out eventually - although the bloody syntax example was about as much use as a dead donkey!

    keep an eye on this message as I will be editing this post to link a wiki page - with the correct syntax - which will explain in detail about the registerEventHandler along with various examples!

    *** edit: link here! not finished yet mind!

    Imperator

    7289 Posts

  • #3, by afrlmeFriday, 04. January 2013, 15:05 13 years ago
    ok so finally finished ... check link in post above! else if you can't be bothered, then I added the content from the wiki page in the spoiler below smile

    I'd recommend the wiki page mind!

    script type: definition syntax: registerEventHandler('eventName', 'functionName', {integerValue or eventFlagName})

    
    --[[
    mainLoop allows us to continuously loop a "function" used in conjunction with "mainLoop"
    syntax: registerEventHandler('mainLoop', 'function_name')
    --]]

    --[[ global integer variable which we will use to count how many times ... we've printed the message inside of the function below! --]] local index_number = 0 -- integer variable

    -- * this is the function that will be looped * -- function count_me() print('this is message number ' .. index_number) index_number = index_number + 1 -- increments integer variable by 1 each loop end

    -- * loops function "count_me" * -- registerEventHandler('mainLoop', 'count_me')

    
    --[[
    eventFlag names & integer values ...
    these are only for the "mouseEvent" listener loop!
    --]]

    name: eEvtMouseMove | integer value: 1 -- useless! name: eEvtMouseLeftButtonDoubleClick | integer value: 2 -- do not loop this! name: eEvtMouseLeftButtonDown | integer value: 3 name: eEvtMouseLeftButtonUp | integer value: 4 name: eEvtMouseLeftButtonHold | integer value: 5 name: eEvtMouseLeftButtonHolding | integer value: 6 -- useless! name: eEvtMouseRightButtonDoubleClick | integer value: 7 -- do not loop this! name: eEvtMouseRightButtonDown | integer value: 8 name: eEvtMouseRightButtonUp | integer value: 9 name: eEvtMouseMiddleButtonDown | integer value: 10 name: eEvtMouseMiddleButtonUp | integer value: 11 name: eEvtMouseWheelUp | integer value: 12 -- do not loop this! name: eEvtMouseWheelDown | integer value: 13 -- do not loop this!

    
    --[[
    mouseEvent allows us to detect if a button has been pressed or released etc ...
    which we can use to control a function - to some degree but ...
    if we want to loop a function on button hold, then we need to use "mouseEvent" in conjunction with "mainLoop"!
    --]]

    -- * let's create a boolean true/false variable to determine if button is being pressed down or has been released! * -- local buttonPressed = false

    -- * this is the loop which checks if the button is pressed down & decides what to do with said information! * -- function OnMainLoop() if buttonPressed then -- (if buttonPressed == true) is also a valid argument! -- do some action else -- do some other action end end

    -- * if button is pressed down then this function sets "buttonPressed" boolean value to "true"! * -- function OnLeftButtonDown() buttonPressed = true end

    -- * if button is released then this function sets "buttonPressed" boolean value to "false"! * -- function OnLeftButtonReleased() buttonPressed = false end

    -- * let's create the loop & listeners * -- registerEventHandler('mouseEvent', 'OnLeftButtonDown', {eEvtMouseLeftButtonDown}) -- listens for when left mouse button is being pressed down! registerEventHandler('mouseEvent', 'OnLeftButtonReleased', {eEvtMouseLeftButtonUp}) -- listens for when left mouse button has been released! registerEventHandler('mainLoop', 'OnMainLoop') -- this loops anything found inside of the "OnMainLoop" function!

    Quick note in regards to the mouseEvent listener ...

    the only mouseEvent eventFlags that loop naturally without you having to use boolean variables or mainLoop are: eEvtMouseWheelUp & eEvtMouseWheelDown; this will only loop via a laptop touchpad mind, as the mouseWheel on a touchpad, is in fact a button & when you hold said button down it automatically increases/decreases by a fractional click every x amount of time whereas a mouseWheel on an actual mouse registers every fractional turn.

    I have not used the rest of the event listeners found in the registerEventHandler syntax page but I believe you have to use them in conjunction with the "mainLoop" event!

    Imperator

    7289 Posts

  • #4, by afrlmeFriday, 04. January 2013, 16:43 13 years ago
    ok new issue ... it appears we are only allowed to use one mouseEvent per script which is daft! I guess we'll just have to create separate scripts for various mouse events which is a bit of a pain in the ass but such is life! razz

    *** ignore this for now too!

    same issue ... it skips past any mouseEvents until it gets to the last one ...

    for example if I add more than 1 mouse event to a script or even 1 mouse invent per script then ...

    
    registerEventHandler('mouseEvent', 'OnLeftButtonPress', {eEvtMouseLeftButtonDown}) -- skips this one for some unknown reason! :(
    registerEventHandler('mouseEvent', 'OnMwUp', {eEvtMouseWheelUp}) -- skips this one for some unknown reason! :(
    registerEventHandler('mouseEvent', 'OnMwDown', {eEvtMouseWheelDown}) -- only this one works!
    
    :shakebloodyshark: cry

    Imperator

    7289 Posts

  • #5, by DundilTuesday, 02. April 2013, 15:31 13 years ago
    Good post.

    How is the way to remove the handler?

    I've been thinking on a timer object to call functions after a specified time (you mentioned it in a different post) and, once done, I'd like to clean it and stop the timer system when there are no more functions to call.

    Thanks!

    Newbie

    19 Posts

  • #6, by afrlmeTuesday, 02. April 2013, 17:05 13 years ago
    hmm ...

    this post is somewhat old.

    in the new wiki I have added various syntax & examples although I have a few more examples to add for some of the available functions/events.

    we are only allowed to add one of each event per project file in which we can declare various functions.

    for instance we can control the scripts/functions added to a main loop event by checking if a variable or in editor condition or value does or does not equal something that way the function will only work when you allow it to.

    as for the timer script - I figured out a very crude method of using the getTime() command which is actually more of a debug/developer command for getting the time taken to perform an action or something.

    http://wiki.visionaire-tracker.net/index.php?title=Player_Co...
    http://wiki.visionaire-tracker.net/index.php?title=RegisterE...
    http://wiki.visionaire-tracker.net/index.php?title=GetTime

    Imperator

    7289 Posts

  • #7, by DundilWednesday, 03. April 2013, 09:23 13 years ago
    Oh, I was using the os.clock() method. Are there any adventages/disadventages of using os.clock() or getTime()?

    I'm creating a delayCall method with three parameters: a delay time and a function that is called once the time is over and the number of times the function will be called. With it, I have a table with the calls pile and just call the functions when needed.

    Still under development. Let's see if I can successfully finish it wink

    Newbie

    19 Posts

  • #8, by afrlmeWednesday, 03. April 2013, 11:24 13 years ago
    getTime() is more of a developers tool for checking how long it takes to perform some action ...
    you can only create one instance of it & once it's running, you can't stop it; only reset the timer, but if you leave it running then you can use a function to store the current time in ms into local variables & then using an if query to check for when the timer has been running for x time to perform whatever is in the if query - you also need to add a function to a mainLoop event to keep updating a variable containing the current time.

    I have heard of people using os.clock etc to create delay functions but haven't tried yet myself & am interested to see what you come up with wink

    Imperator

    7289 Posts

  • #9, by DundilWednesday, 03. April 2013, 12:59 13 years ago
    Well, this is a first approximation to a delay system. Please remember I'm not a LUA expert grin

    timerInitialized = false
    callsList = {}
    nextEvent = 0
    
    function onMainLoop()
    	if nextEvent > 0 and os.clock() > nextEvent then
    		callsList[1].func()
    		callsList[1].nextCall = os.clock() + callsList[1].delay
    		if callsList[1].times > 0 then
    			callsList[1].times = callsList[1].times - 1
    			if callsList[1].times == 0 then
    				table.remove(callsList, 1)
    			end
    		end
    		setNextEvent()
    	end
    end
    
    -- Delay is in seconds. It is a real positive number
    function delayCall(delay, func, times)
    	if delay <= 0 then
    		return
    	end
    	if not timerInitialized then
    		timerInitialized = true
    		registerEventHandler('mainLoop', 'onMainLoop')
    	end
    	local newCall = {delay = delay, func = func, times = times, nextCall = os.clock() + delay}
    	table.insert(callsList, newCall)
    	setNextEvent()
    end
    
    function setNextEvent()
    	if table.maxn(callsList) == 0 then
    		nextEvent = 0
    	else
    		table.sort(callsList, compareDelays)
    		nextEvent = callsList[1].nextCall
    	end
    end
    
    function compareDelays(callA, callB)
    	if (callA.nextCall > callB.nextCall) then
    		return false
    	else
    		return true
    	end
    end
    


    With this code, I can call a function after a time period (in seconds, but it is a real number). Last param is the number of times I want the function to be called. If zero, it will be called forever (I have to implement a kind of delete for the call). For example. Let's say I have three test functions (testA, testB and testC), that just prints the time they're called at. If at the start of the game I have:

    delayCall(5, testA, 0)
    delayCall(10, testB, 2)
    delayCall(15, testC, 1)
    


    The result in the log is:
    TestA at 6.21
    TestB at 11.202
    TestA at 11.219
    TestC at 16.212
    TestA at 16.228
    TestB at 21.204
    TestA at 21.238
    TestA at 26.247
    TestA at 31.256
    TestA at 36.265
    ...

    So, TestA is being called forever every 5 seconds, TestB just twice, every 10 seconds and TestC just once 15 seconds after.

    Newbie

    19 Posts

  • #10, by DundilWednesday, 03. April 2013, 13:05 13 years ago
    And yes, I know... I should comment the code: bad programmed habits XDDDD

    To explain it a little, the onMainLoop function checks for functions that have to be called.

    delayCall(delay, function, times) creates a new small table, a newCall, with the delay, function and time parameters and a new one: the next time the function has to be called. It stores newCall in the global table callsList.

    callsList is checked every onMainLoop. If there's a function to call, it calls it and calculates the next time the function has to be called. Also, reduces the times parameter (if it is not a forever-call) and deletes it if no more calls are needed.

    Newbie

    19 Posts

  • #11, by afrlmeWednesday, 03. April 2013, 14:13 13 years ago
    oh smile
    it is working currently?

    sounds like a very nice function! could you please add the script to the wiki (listed on the menu) when you have done along with some instructions maybe & a pastebin version? cheers wink

    --*--

    register event handlers should really be added to a separate script & their should only be one of each - I've tried adding multiple ones before & it tends to skip all of them until it reaches the last one created.

    for the mainLoop I add functions from other scripts into it:
    function onMainLoop()
    -- check if a certain condition is met so that it knows if the function should be called or not ...
     if cond or if variable_state = x then
      function_name()
     end
    end
    

    Imperator

    7289 Posts