Jump to content

Create an event that be can used to all resources


WiBox

Recommended Posts

Greetings. I've had some circumstances so I had that idea to activate an function by using a "addEventHandler", what I'm looking for is to create an Event Handler that can be used on any function like "onPlayerLogin", "onClientPlayerWeaponFire"...

Is there a way to create such thing?

Link to comment

Can you give me an example? I don't want to trigger the function using triggerServerEvent or triggerClientEvent, I want to trigger a function from another resource using addEventHandler..

like

addEventHandler("onClientGUIClick", apply, applySetting, false)
addEventHandler("onClientGUIClick", apply, onApplying, false)

function applySetting()
    setSetting(slcSet, guiGetText(settingsEdit))
end

in another resource:

function set()
    if ( exports.settings:getSetting("setHaze") ) then
        setHaze( exports.settings:getSetting("setHaze") )
    end
end
addEventHandler("onSettingsChange", root, set)

--[[ So What I need is when the function applySetting is trigger from onClientGUIClick I need to trigger
" addEventHandler("onSettingsChange", root, set) "
I need something like this so I don't have to use exports on each resource and add it inside the applySetting function...]]

 

Link to comment
  • Moderators

I don't quite understand what you want, but:

 

-- CLIENT SIDE
-- FIRST RESOURCE

function applySetting()
    setSetting(slcSet, guiGetText(settingsEdit))
    triggerEvent("onSettingsChange", localPlayer) -- Trigger the event, whats in a another client sided resource
end
addEventHandler("onClientGUIClick", apply, applySetting, false)
addEventHandler("onClientGUIClick", apply, onApplying, false) -- (?)
-- CLIENT SIDE (?)
-- SECOND RESOURCE

function set()
    if ( exports.settings:getSetting("setHaze") ) then
        setHaze( exports.settings:getSetting("setHaze") )
    end
end
addEvent("onSettingsChange", true) -- register our custom event
addEventHandler("onSettingsChange", root, set)

 

Edited by stPatrick
Link to comment

I know I can do it that way, but what I'm asking is: I want to something like the server events and client event, I can trigger them anywhere using addEventHandler only, example, so I use "addEventHandler("onPlayerLogin", ...) not triggerEvent("onPlayerLogin", ...). So what I'm looking for I want to create a function that when it function an event handler named 'onSettingsChange' will activate..

function logins()
  outputChatBox("Welcome....", source)
end
addEventHandler("onPlayerJoin", root, logins)

Here once the player join, the event handler functions right? I want to create like it but when the settings[A custom settings window I created..] change the event handler will function...

If it still unclear, I want to create a function that I can trigger in another resource using addEventHandler

Edited by WiBox
Link to comment

You need this functions:

https://wiki.multitheftauto.com/wiki/AddEvent

and

https://wiki.multitheftauto.com/wiki/TriggerEvent or https://wiki.multitheftauto.com/wiki/TriggerServerEvent or https://wiki.multitheftauto.com/wiki/TriggerClientEvent

Example:

function myFunction()

--code here

end

addEvent("myCustomEvent", true)

addEventHandler("myCustomEvent", root, myFunction)

Then trigger this event using one of the trigger functions, for example:

triggerServerEvent("myCustomEvent", root)

 

"root" means it will be triggered for all elements and all resources, see wiki for more details https://wiki.multitheftauto.com/wiki/Element_tree

That's it.

P.S. not 100% sure thought, lazy to check

P.P.S. also it can be something like that:

myResource = getResourceFromName("myResourceName")
myResourceRoot = getResourceRootElement(myResource)
triggerServerEvent("myCustomEvent", myResourceRoot)

So you call this event only for 1 resource, instead of telling all other resource about this event and creating additional load on CPU etc

Edited by AleksCore
P.P.S.
Link to comment

Dude that's not what I need I want to like

function applySetting() -- when this function activate, all 'addEventHandler('onSettingsChange" .. will activate
    setSetting(slcSet, guiGetText(settingsEdit))
end
addEventHandler("onClientGUIClick", apply, applySetting, false)
-------------------------------------------------------------------------------------------------------------
--[[ of using triggerEvent I want to use 'addEventHandler' so when it apply 
settings all the eventhandler with "onSettingsChange" shall activate]]
-- CLIENT SIDE (?)
-- SECOND RESOURCE

function set()
    if ( exports.settings:getSetting("setHaze") ) then
        setHaze( exports.settings:getSetting("setHaze") )
    end
end
addEventHandler("onSettingsChange", root, set)

I gave an example like replacing onPlayerLogin with onSettingsChange, when a player logins all event handler with onPlayerLogin will activate.. I want it like that when a function activate all the handlers will activate as well...

Example, in 

addEventHandler("onWeaponFire", ...) How does it activate? when a random weapon has fire.. of course it's a function so it checks if a random weapon has fire, when a random weapon fire, all addEventHandler('onWeaponFire"..) in all resources will activate. Isn't that right? I want to do the same thing with onSettingsChange

Edited by WiBox
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...