Moderators Popular Post IIYAMA Posted October 2, 2017 Moderators Popular Post Share Posted October 2, 2017 (edited) 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. Edited February 21, 2019 by IIYAMA 11 9 2 Link to comment
Storm-Hanma Posted October 7, 2017 Share Posted October 7, 2017 I need to start from bottom can you give Me a desperate classes for me? Link to comment
Moderators IIYAMA Posted October 7, 2017 Author Moderators Share Posted October 7, 2017 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. 1 Link to comment
Storm-Hanma Posted October 7, 2017 Share Posted October 7, 2017 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 comment
Moderators IIYAMA Posted October 7, 2017 Author Moderators Share Posted October 7, 2017 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.) 1 1 Link to comment
Discord Moderators Pirulax Posted July 15, 2018 Discord Moderators Share Posted July 15, 2018 Thanks, never thought about this! You got some +rep from me. 1 Link to comment
Skream Posted July 17, 2018 Share Posted July 17, 2018 (edited) 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 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. 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 July 17, 2018 by Skream 1 Link to comment
Moderators IIYAMA Posted July 18, 2018 Author Moderators Share Posted July 18, 2018 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 comment
Skream Posted July 19, 2018 Share Posted July 19, 2018 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. 1 Link to comment
RughCuttle Posted February 9, 2022 Share Posted February 9, 2022 (edited) eMarkersGroup = createElement("GroupedMarkers") local mPosX, mPosY, mPosZ = -2429.3, -613.3, 131.2 eMarkersGroup = { createMarker(mPosX, mPosY, mPosZ, "cylinder", 1.5, 255, 0, 0, 90), createMarker(mPosX+10, mPosY, mPosZ, "cylinder", 1.5, 255, 0, 0, 90), createMarker(mPosX-10, mPosY, mPosZ, "cylinder", 1.5, 255, 0, 0, 90) } ---It Works Is this way better (performance) than resourceRoot ? Edited February 9, 2022 by RughCuttle Link to comment
RughCuttle Posted February 9, 2022 Share Posted February 9, 2022 Pff... forgot to change the eventhandler element from resourceRoot to this one before making the post. Link to comment
Recommended Posts