ARYDER Posted February 10, 2023 Share Posted February 10, 2023 Please explain what this code means. I have seen this in many resources, such as standart "admin" resource. What is this code for? Example: local _triggerClientEvent = triggerClientEvent _triggerClientEvent(source, eventData.name, eventData.source, unpack(eventData.args)) function triggerClientEvent( triggerFor, name, theElement, ... ) local args = { ... } if type(triggerFor) == 'string' then table.insert(args, 1, theElement) theElement = name name = triggerFor triggerFor = nil end ... ... It is used in different variations and i can't understand any of it. Link to comment
DiSaMe Posted February 10, 2023 Share Posted February 10, 2023 The purpose is to alter the behavior of functions - or so it appears. It's all just assignment to variables. local _triggerClientEvent = triggerClientEvent This line creates a local variable _triggerClientEvent and assigns the value from triggerClientEvent - which usually means that _triggerClientEvent now refers to the MTA-defined function that triggers the client events from the server. function triggerClientEvent( triggerFor, name, theElement, ... ) ... end This one creates a function and assigns it to the global variable triggerClientEvent, meaning that calls to triggerClientEvent from other scripts in the same resource will call this function instead of the original one. This new function probably contains _triggerClientEvent calls, which are calls to the MTA-defined function. Link to comment
Moderators IIYAMA Posted February 10, 2023 Moderators Share Posted February 10, 2023 1 hour ago, sadvely said: What is this code for? It is called a Wrapper function. It is used to add extra functionality too an existing function. For example the setElementPosition function. It's syntax is now: --[[ Syntax: setElementPosition ( element theElement, float x, float y, float z [, bool warp = true ] ) -- https://wiki.multitheftauto.com/wiki/SetElementPosition ]] All arguments except for warp are required. An example wrapper function: local _setElementPosition = setElementPosition -- Re-define original function (so that it is still accessible) function setElementPosition (element, x, y, z, warp) -- Overwrite the existing setElementPosition with your wrapper function (it is overwritten, because the function name is the same) if not x then x = 0 end if not y then y = 0 end if not z then z = 0 end return _setElementPosition(element, x, y, z, warp) -- calling the original function end The x, y, z parameters do not have to be filled in any more. They are by default set to 0. The new syntax would be now: --[[ Syntax: setElementPosition ( element theElement, [ float x = 0 ], [ float y = 0 ], [ float z = 0] [, bool warp = true ] ) -- https://wiki.multitheftauto.com/wiki/SetElementPosition ]] Is this wrapper function useful? Not really. Link to comment
ARYDER Posted September 3, 2023 Author Share Posted September 3, 2023 Hello again! Once again I came across the above code. I can "overwrite" a function in one resource, how can I make this overwritten function work in all resources? Link to comment
DiSaMe Posted September 8, 2023 Share Posted September 8, 2023 This can't be done as far as I know - each resource has its own global environment, which can only be modified from that resource. So you need to override the function in each resource. 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