addEventHandler + group elements
I noticed that some people like to add 10000000000000000 addEventHandlers for each element, while you probably only need 1 addEventHandler.
Using ONE addEventHandler on a group of elements?
Answer:
local group = createElement("groupMyCutePeds") -- Create a custom element and save it in to the variable <group>.
-- Create 3 peds.
local ped1 = createPed(120, 5540.6654, 1020.55122, 1240.545)
local ped2 = createPed(120, 5541.6654, 1021.55122, 1240.545)
local ped3 = createPed(120, 5542.6654, 1022.55122, 1240.545)
-- Set the parent of the 3 peds.
setElementParent(ped1, group)
setElementParent(ped2, group)
setElementParent(ped3, group)
-- Add an addEventHandler and use the <group> as <attachedTo> element.
addEventHandler("onPedWasted", group, -- "onPedWasted" = serverside. "onClientPedWasted" = clientside.
function ()
outputChatBox("One of my cute peds just died. ;'( No exceptions!")
end)
Code is untested, but the method is tested.
Syntax for functions in example
createElement syntax
element createElement ( string elementType, [ string elementID = nil ] )
setElementParent syntax
bool setElementParent ( element theElement, element parent )
addEventHandler syntax
bool addEventHandler ( string eventName, element attachedTo, function handlerFunction, [ bool getPropagated = true, string priority = "normal" ] )
DO NOT disable getPropagated
getPropagated: A boolean representing whether the handler will be triggered if the event was propagated down or up the element tree (starting from the source), and not triggered directly on attachedTo (that is, handlers attached with this argument set to false will only be triggered if source == this).
If you disable this, children of the <group> element are not included.
Make use of the element tree
Element tree
For applying addEventHandlers to elements created by the resource:
Use: resourceRoot / getResourceRootElement
For applying addEventHandlers to elements created by scripts of the resource:
Use: getResourceDynamicElementRoot
For applying addEventHandlers to elements created by maps of the resource:
Use: getResourceMapRootElement
I hope your code will be without...
print(10^10^10^10) -- Print here: https://www.lua.org/cgi-bin/demo
...addEventHandlers in the future.