Jump to content

[TUT] addEventHandler on a group of elements. (Small tutorial)


IIYAMA

Useful  

32 members have voted

  1. 1. Is this tutorial useful?

    • Yes, useful. Didn't know this.
    • Somethings are unclear to me. (Only counts if you ask for clarity in a reply)
      0
    • I am not a scripter, but I like to vote.
      0
    • I already knew this method.


Recommended Posts

  • Moderators

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

Tre.png

 

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. :cheers:

 

 

Edited by IIYAMA
  • Like 7
  • Thanks 8
  • Haha 2
Link to post
  • Moderators
58 minutes ago, Khadeer143 said:

I need to start from bottom can you give Me a desperate classes for me?

I am sorry,  I do not have time for personal / private coaching. If you start from bottom you have to start with LUA and not with MTA scripting.

There are enough basic tutorials around.

 

  • Thanks 1
Link to post

I know some basics scripting I can script some small resources like speakers ,login panel, etc.. But when it comes to higher level of scripting I can't script for example scripting turfs,group system,level systems base on GUI functions etc.. I can't script like this resources 

Link to post
  • Moderators

Once you understand the basics of variables, functions, debugging, tables and loops, the only thing that is your way is your mental model and your creativity. Which is something that has to grow over time.

 

 


The fastest way to get at an higher level is to know exactly what your code does, which is achieve able by debugging (or lots of reading).

 

So for example this code looks complex: (useless code btw)

thisTable = {"randomStuff", [20] = "randomStuff", "randomStuff", [30] = "randomStuff"}
function functionName (parameter1, parameter2)
	local newTable = {}
	for i=1, 30 do
		newTable[#newTable + 1] = (parameter1[1] or "nothing here") .. " | " .. math.random(1000) .. " | " .. parameter2
	end
	return newTable
end


local result = functionName (thisTable, "text")

 

 

Adding debug lines:

thisTable = {"randomStuff", [20] = "randomStuff", "randomStuff", [30] = "randomStuff"}
iprint("thisTable:", thisTable) -- show table structure

function functionName (parameter1, parameter2)
  
	iprint("parameter1:", parameter1, ",parameter2:", parameter2)
  
	local newTable = {}
	iprint("newTable:", newTable)	

	local countLoops = 0
	for i=1, 30 do
    
		iprint('What does >(parameter1[1] or "nothing here")< do? ', (parameter1[1] or "nothing here"))

		newTable[#newTable + 1] = (parameter1[1] or "nothing here") .. " | " .. math.random(1000) .. " | " .. parameter2
    
		countLoops = countLoops + 1
    
	end
	iprint("Loop has executed:", countLoops, "times.")
 
	return newTable
end

iprint("functionName:", functionName, ", type value:", type(functionName))

local result = functionName (thisTable, "text")
iprint(result) -- show end result

 

But after debugging: (You have to do that yourself, post result please. Which is automatic your motivation towards me.)

 

 

 

 

  • Like 1
  • Thanks 1
Link to post
  • 9 months later...
On 02/10/2017 at 08:59, IIYAMA said:

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

Tre.png

 

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

 

 


 

Not recommended to change the parent of players

It is not recommended to change the parent of the players, because some resources are making use of this. (example: race)

 


 

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. :cheers:

 

 

this is just like bacon,  AWESOME. I was missing some group system in LUA. Hey, if I was going to call the group in a diff resource I should use getElementsByType or retrieve it with getElementById?

and how is the right way to remove a element from a group?

 

local charGroup = createElement('groupCharacter', 'character_group')
setElementParent(charGroup, getElementById('account_group'))

addEvent('server_onCharacterLogin', function()
	setElementParent(source, charGroup)
end)

addEvent('server_onCharacterLogout', function()
	setElementParent(source, nil)
end)

My code seems correct?
 

Edited by Skream
  • Thanks 1
Link to post
  • Moderators
15 hours ago, Skream said:

this is just like bacon,  AWESOME. I was missing some group system in LUA. Hey, if I was going to call the group in a diff resource I should use getElementsByType or retrieve it with getElementById?

and how is the right way to remove a element from a group?

 


local charGroup = createElement('groupCharacter', 'character_group')
setElementParent(charGroup, getElementById('account_group'))

addEvent('server_onCharacterLogin', function()
	setElementParent(source, charGroup)
end)

addEvent('server_onCharacterLogout', function()
	setElementParent(source, nil)
end)

My code seems correct?
 

 

 

Nope it is not correct. If you have ran the code you would receive an warning at line 4 and 8.

Syntax addEvent:

bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] )   

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

 

Syntax addEventHandler:

bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] )    

https://wiki.multitheftauto.com/wiki/AddEventHandler

 

Correct use:

addEvent("example", true)
addEventHandler("example", root, 
function ()
    
end)

 

 

 

 

Re-attach to the dynamic root element, the original element. You can't attach elements to the void. Then you can better destroy them.

https://wiki.multitheftauto.com/wiki/GetResourceDynamicElementRoot

 


Selectors, please read the documentation carefully:

https://wiki.multitheftauto.com/wiki/GetElementChildren

https://wiki.multitheftauto.com/wiki/GetElementChild

https://wiki.multitheftauto.com/wiki/GetElementsByType

https://wiki.multitheftauto.com/wiki/GetElementByIndex

 


https://wiki.multitheftauto.com/wiki/GetElementByID

ID's SHOULD BE UNIQUE!

Link to post
On 18/07/2018 at 08:12, IIYAMA said:

 

 

Nope it is not correct. If you have ran the code you would receive an warning at line 4 and 8.

Syntax addEvent:


bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] )   

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

 

Syntax addEventHandler:


bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] )    

https://wiki.multitheftauto.com/wiki/AddEventHandler

 

Correct use:


addEvent("example", true)
addEventHandler("example", root, 
function ()
    
end)

 

 

 

 

Re-attach to the dynamic root element, the original element. You can't attach elements to the void. Then you can better destroy them.

https://wiki.multitheftauto.com/wiki/GetResourceDynamicElementRoot

 


Selectors, please read the documentation carefully:

https://wiki.multitheftauto.com/wiki/GetElementChildren

https://wiki.multitheftauto.com/wiki/GetElementChild

https://wiki.multitheftauto.com/wiki/GetElementsByType

https://wiki.multitheftauto.com/wiki/GetElementByIndex

 


https://wiki.multitheftauto.com/wiki/GetElementByID

ID's SHOULD BE UNIQUE!

understood, thanks mate. Oh, sorry for the addEvent I wasn't paying attention to it. 

  • Like 1
Link to post
  • qaisjp featured, pinned and unfeatured this topic
  • qaisjp unpinned this topic
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...