Jump to content

_functionName = functionName


ARYDER

Recommended Posts

Posted

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.

Posted

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.

  • Moderators
Posted
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. ?‍♂️

 

  • 6 months later...
Posted

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?

Posted

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.

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