WiBox Posted June 29, 2018 Share Posted June 29, 2018 How to check if the function a () -- code.. end addEventHandler("onPlayerCommand", root, a) already handled? Link to comment
WiBox Posted June 29, 2018 Author Share Posted June 29, 2018 (edited) If the Event Handler is already handled*, and if there isn't a way hope someone say that it's not possible.. Edited June 29, 2018 by SSKE Link to comment
Addlibs Posted June 29, 2018 Share Posted June 29, 2018 (edited) Possible through getEventHandlers. The following is (part) of the example usage from the wiki: function isEventHandlerAdded( sEventName, pElementAttachedTo, func ) if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo ) if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then for i, v in ipairs( aAttachedFunctions ) do if v == func then return true end end end end return false end Edited June 29, 2018 by MrTasty Link to comment
WiBox Posted July 1, 2018 Author Share Posted July 1, 2018 I'll make it simple for you to understand, in this code: Timer10 = setTimer( function () for _,plrs in ipairs(getElementsByType("player")) do if ( getElementDimension(plrs) == 0 ) then addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end end end, 1000, 0) I want to add after line 8, if ( addEventHandler line 7 and addEventHandler line are already handled ) then killTimer(Timer10) elseif ( addEventHandler line 7 and 8 are not handled ) then resetTimer(Timer10) end Can you make it for me? Or at least give me a example I would be grateful and thanks for your help! Link to comment
Addlibs Posted July 1, 2018 Share Posted July 1, 2018 (edited) Timer10 = setTimer( function () for _,plrs in ipairs(getElementsByType("player")) do if ( getElementDimension(plrs) == 0 ) then addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end end end, 1000, 0) I don't really understand why would you put these handlers (especially the event handlers attached to root) inside a loop? Just add the handlers once at initialisation. On the server, command and event (attached to root) handlers are bound to all players, even those who join later on, and on the client, well, they're bound when the script starts so pretty much on every client. Also, you really do not have to use a timer for the dimension check, you can have the dimension checked inside the usethemedkit, healOthers, giveMedKit, and usemedkitanother functions. Edited July 1, 2018 by MrTasty Link to comment
WiBox Posted July 1, 2018 Author Share Posted July 1, 2018 The thing is, I want if the player left a specific dimension, He just can go back to dimension 0, I want when he go back to the main dimension those 4 get added.. Because I'll remove them with a command and I want if the player left the dimension so those 4 get added.. function removePlayerMedkits(player) for _,plrs in ipairs(getElementsByType("player")) do if ( getElementDimension (plrs) == 336 ) then removeCommandHandler("usemedkit") removeCommandHandler("acceptmedkit") removeEventHandler("meds.giveMedKit", root, giveMedKit) removeEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end if ( not addCommandHandler("usemedkit", usethemedkit) ) then addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end end end addCommandHandler("empm", removePlayerMedkits) Now look, I use this command to disable medkits, I want if the players left from dimension 336 the 4 of: addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother get added automatically. Any idea's? Link to comment
WiBox Posted July 1, 2018 Author Share Posted July 1, 2018 So? Is there a way I can make that? Or it's a impossible thing? Link to comment
Addlibs Posted July 1, 2018 Share Posted July 1, 2018 (edited) function removePlayerMedkits(player) for _,plrs in ipairs(getElementsByType("player")) do if ( getElementDimension (plrs) == 336 ) then removeCommandHandler("usemedkit") removeCommandHandler("acceptmedkit") removeEventHandler("meds.giveMedKit", root, giveMedKit) removeEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end -- [...] end end addCommandHandler("empm", removePlayerMedkits) You do realise that this code, specifically lines 4 and 5, completely remove the handling of the command for ANY player (if server side) or the client (if client side), and lines 6 and 7 remove the handling of events for ANY player (if server side) or the client (if client side), if the last person of the loop is in dimension 336 (as every other iteration is simply overruled by the next). You don't want to remove the handler - you want the handler to determine how the command/event should be handled - whether you want it to do anything or to ignore the command/event in case the source player or the target player is in the wrong dimension. Note: the following is written assuming this is server-side Use the handler to determine whether to do anything or to ignore the command: function usethemedkit(sourcePlayer, command, ...) -- /usemedkit if getElementDimension(sourcePlayer) == 336 then return false -- abort function here, player is in wrong dimension end -- [...] continue doing whatever this function was meant to do end function healOthers(sourcePlayer, command, ...) -- /acceptmedkit if getElementDimension(sourcePlayer) == 336 then return false -- abort here, player is in wrong dimension end -- here assumes there's a table pendingMedRequests that stores the user as key and healer as value (e.g. pendingMedRequests[whoToHeal] = healedByWho) local healer = pendingMedRequests[sourcePlayer] if not healer then return false -- no awaiting medkit request to accept end if getElementDimension(healer) == 336 then return false -- abort here, healer is in wrong dimension end -- [...] continue doing whatever this function was meant to do end -- and so on And add these only once and keep them: addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) Edited July 1, 2018 by MrTasty Link to comment
WiBox Posted July 2, 2018 Author Share Posted July 2, 2018 Dude.. Look the med kit should work on any other dimensions, I just want to pause those 4: removeCommandHandler("usemedkit", usethemedkit) removeCommandHandler("acceptmedkit", healOthers) removeEventHandler("meds.giveMedKit", root, giveMedKit) removeEventHandler("meds.useMedKitOnOther", root, usemedkitanother) And I already made that, but the thing is they can leave the dimension by using /leaved So if they left or go back to the main dimension I want those 4 get auto added.. So I tried to loop them and make if one of them already handled of line 3 and 4 so the timer will be destroyed.. I want with a command so in dimension 336 the med kit get enable and disable, I maybe use med kits in dimension 336.. Can you just give me a way as I said? And yeah I'm making it for all player join the dimension 336.. But the thing is I just need a way if he left dimension 336 the med kits command and event handlers will be added automatically.. So I tried using setTimer so it will check every second but the spam in the 2 lines: addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) but the thing is I want to check if 1 of those 2 lines are already handled so I killTimer(Timer10) ... Link to comment
WiBox Posted July 2, 2018 Author Share Posted July 2, 2018 (edited) Then what about if I can make a function which example: function isEventHandled() if ( addEventHandler("meds.useMedKitOnOther", root, usemedkitanother ) ) then return true elseif ( removeEventHandler("meds.useMedKitOnOther", root, usemedkitanother) ) then return false end end Timer10 = setTimer( function() for _,plrs in ipairs (getElementsByType("player")) do if ( getElementDimension(plrs) == 0 ) then if ( isEventHandled == true ) then killTimer(Timer10) elseif ( isEventHandled == false ) then resetTimer(Timer10) end addCommandHandler("usemedkit", usethemedkit) addCommandHandler("acceptmedkit", healOthers) addEventHandler("meds.giveMedKit", root, giveMedKit) addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) end end end, 1000, 0) Does something like that work? I mean use a function for check if addEventHandler added or removed, It didn't work for me.. Because ofc I did something wrong in function isEventHandled, I'm still new at coding and I'm using /debugscript 3 but there's only two thing always get spammed eventhandler... already handled line 20 and 21.. My only problem is to check if the Event is already handled so is there a way like I said so it work? I don't want to remove using med kits from dimension 336 I just want to stop and add it using a command and I made it, when he go back to another dimension I need that the med kits get automatically added and I made it I just want to do something so I check if addEventHandler already handled then killTimer(Timer10) .. Edited July 2, 2018 by SSKE Link to comment
Moderators IIYAMA Posted July 2, 2018 Moderators Share Posted July 2, 2018 (edited) Solve it from the source, since there is no native event. local setElementDimension_ = setElementDimension function setElementDimension(theElement, dimension) local previousDimension = getElementDimension(theElement) local result = setElementDimension_(theElement, dimension) if result then triggerEvent("onElementDimensionChange", theElement, dimension, previousDimension) end return result end (add this on the resources where you control the dimensions.) Edited July 2, 2018 by IIYAMA Link to comment
WiBox Posted July 3, 2018 Author Share Posted July 3, 2018 Mmm what about use onPlayerCommand? As: function Test(command,plr) if ( command =="..." or command =="..." ) then if ( did player change dimension ) then addCommandHandler... command and event handlers end end end addEventHandler("onPlayerCommand", root, Test) The thing is.. I got a CommandHandler which back the player to his last location before he use that command.. And once he write that command he instantly go back to his last location before he use that command.. I'm afraid that he goes back faster than the command will determinate if the player was at dimension 336 and for the 2 other command they can work. Is there a EventHandler which check if the player changed dimension? Or I can create one? as triggerEvent or triggerClientEvent (using the code on server side.) which check if the player changed dimension. On 7/2/2018 at 13:07, IIYAMA said: local setElementDimension_ = setElementDimension function setElementDimension(theElement, dimension) local previousDimension = getElementDimension(theElement) local result = setElementDimension_(theElement, dimension) if result then triggerEvent("onElementDimensionChange", theElement, dimension, previousDimension) end return result end Now I don't know if that work or if I can add it in that Test function. Because I didn't understand quite good what it do.. as I see the function it set a element dimension... But I didn't understand what role it do and with what it will help me.. But the thing is I got interested on that triggerEvent onElementDimensionChange, if I can use that so the command handler which I already told what my problem with it can work with that event.. But I need help so I know how to make and add it in the Test function.. Thanks for your help . And sorry for my useless English dictation.. Link to comment
Moderators IIYAMA Posted July 3, 2018 Moderators Share Posted July 3, 2018 (edited) Well, lets first get back to basics. function () -- Yes We Can! end A function = something you can do. function shout() outputChatBox("Yes We Can!") end A function name = describes in short what the function does. `shout` setElementDimension We can set an element it's dimension, with `setElementDimension` local setElementDimension_ = setElementDimension We can now also set it with: `setElementDimension_` There are two variables `setElementDimension` and `setElementDimension_` that contain the same function reference. So why did I created another variable? setElementDimension_ Well, to make a wrapper. If you have for example a hamburger which you want to eat. Normally you can eat it directly, but now I wrap it up with some paper. Which means in order to execute the function `eat my hamburger` I also have to do the function `unwrap my hamburger`. I can case of a wrapper, you can execute both functions with just one function. Because it has to do more than it is already doing and you can't rewrite the native function. local setElementDimension_ = setElementDimension Re-save (wrapper function) function setElementDimension(theElement, dimension) -- ... end Overwrite the native function reference (only in the same resource) With these two steps I made sure that if I call the function setElementDimension, the wrapper function will be used instead. local setElementDimension_ = setElementDimension function setElementDimension(theElement, dimension) local result = setElementDimension_(theElement, dimension) return result end The next thing is to make sure that the wrapper function does at least the same as the native function. local setElementDimension_ = setElementDimension function setElementDimension(theElement, dimension) local previousDimension = getElementDimension(theElement) -- custom behaviour local result = setElementDimension_(theElement, dimension) -- custom behaviour if result then triggerEvent("onElementDimensionChange", theElement, dimension, previousDimension) end -- return result end Now you can customize your wrapper function. New behaviour: When ever you call the function setElementDimension in your script. The wrapper function will do the same thing + trigger the event onElementDimensionChange for it. With triggerEvent you can make a new custom event. Edited July 3, 2018 by IIYAMA 1 Link to comment
WiBox Posted July 3, 2018 Author Share Posted July 3, 2018 (edited) Wow at least there's someone here which know how to explain something clearly, thanks! Edited July 3, 2018 by SSKE 1 Link to comment
JeViCo Posted July 4, 2018 Share Posted July 4, 2018 this also can determine your event: function respFunction(res, evname, evsource, evcl, file, line) if res == getThisResource() then -- check resource print("found!") end end addEventHandler("onClientResourceStart",resourceRoot,function() addDebugHook("preEvent",fix,{"onPlayerCommand"}) end) you can also attach it to file name and line in your script https://wiki.multitheftauto.com/wiki/AddDebugHook Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now