OnElementDestroy: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: {{Server event}} __NOTOC__ This event is triggered when an element gets destroyed by destroyElement or when the creator resource is stopping. It is also triggered when a parent elemen...)
 
No edit summary
Line 23: Line 23:
   end
   end
end)
end)
</syntaxhighlight>
This example outputs a message every time an element is destroyed saying which resource destroyed the element
<syntaxhighlight lang="lua">
function getSourceResourceOfElementDestruction()
local sourceResourceRoot = getElementParent(getElementParent(source)) -- Get the resource root element of the element
-- Now to get the resource from the resource root element
local theResource = false
for ind, res in pairs(getResources()) do -- Loop all resources
if (getResourceRootElement(res) == sourceResourceRoot) then -- Compare each resources root element to what we've got
theResource = res -- We've found the source
break -- End the loop
end
end
if (theResource) then -- Did we find it?
local resourceName = getResourceName(theResource) -- Get the resources name
outputChatBox(resourceName.." destroyed a "..getElementType(source)) -- Output the resource name along with element type
end
end
addEventHandler("onElementDestroy", root, getSourceResourceOfElementDestruction) -- All elements
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:22, 1 April 2012

This event is triggered when an element gets destroyed by destroyElement or when the creator resource is stopping. It is also triggered when a parent element of this element is destroyed.

Parameters

None

Source

The source of this event is the element that is being destroyed.

Example

This example sends a message to the vehicle occupants when it's being destroyed.

addEventHandler("onElementDestroy", getRootElement(), function ()
  if getElementType(source) == "vehicle" then
    local nPassengers = getVehicleMaxPassengers(source)
    for i=0,nPassengers-1 do
      local occupant = getVehicleOccupant(source, i)
      if occupant then
        outputChatBox("The vehicle that you were in has been destroyed by the script", occupant)
      end
    end
  end
end)

This example outputs a message every time an element is destroyed saying which resource destroyed the element

function getSourceResourceOfElementDestruction()
	local sourceResourceRoot = getElementParent(getElementParent(source)) -- Get the resource root element of the element
	-- Now to get the resource from the resource root element
	local theResource = false
	for ind, res in pairs(getResources()) do -- Loop all resources
		if (getResourceRootElement(res) == sourceResourceRoot) then -- Compare each resources root element to what we've got
			theResource = res -- We've found the source
			break -- End the loop
		end
	end
	if (theResource) then -- Did we find it?
		local resourceName = getResourceName(theResource) -- Get the resources name
		outputChatBox(resourceName.." destroyed a "..getElementType(source)) -- Output the resource name along with element type
	end
end
addEventHandler("onElementDestroy", root, getSourceResourceOfElementDestruction) -- All elements

See Also

Element events


Event functions