DestroyElement: Difference between revisions
m (Changed comment above destroyElement ( root ) line) |
mNo edit summary |
||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | {{Server client function}} | ||
This function destroys an [[element]] | This function destroys an [[element]] and all elements withinn it in the hierarchy (its children, the children of those children etc). [[player|Player]] elements cannot be destroyed using this function. A player can only be removed from the hierarchy when they quit or are kicked. The root element also cannot be destroyed, however, passing the root as an argument will wipe all elements from the map, except for the players, which will become direct descendants of the root node. | ||
Players are not the only elements that cannot be deleted. This list also includes | Players are not the only elements that cannot be deleted. This list also includes remote clients and console elements. | ||
==Syntax== | ==Syntax== | ||
Line 16: | Line 16: | ||
Returns ''true'' if the element was destroyed successfully, ''false'' if either the element passed to it was invalid or it could not be destroyed for some other reason. | Returns ''true'' if the element was destroyed successfully, ''false'' if either the element passed to it was invalid or it could not be destroyed for some other reason. | ||
==Example== | ==Example== | ||
'''Example 1:''' This example would destroy every element in the map, with the exception of players and the root element itself. | '''Example 1:''' This example would destroy every element in the map, with the exception of players and the root element itself. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
Line 25: | Line 25: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Example 2:''' This example destroys all | '''Example 2:''' This example destroys all vehicles of the specified model: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function destroyVehiclesOfModel(modelID) | function destroyVehiclesOfModel(modelID) | ||
Line 41: | Line 41: | ||
'''Example 3:''' This example allows creation of claymores, which trigger and explode. When they explode, the colshape and object for the claymore are destroyed. | '''Example 3:''' This example allows creation of claymores, which trigger and explode. When they explode, the colshape and object for the claymore are destroyed. | ||
<syntaxhighlight lang="lua">function createClaymore ( x,y,z, creator ) | <syntaxhighlight lang="lua">function createClaymore ( x,y,z, creator ) | ||
local claymoreObject = createObject ( 1945, x, y, z - 1, 0, 0, 90 ) --create an object which looks like a claymore | local claymoreObject = createObject ( 1945, x, y, z - 1, 0, 0, 90 ) -- create an object which looks like a claymore | ||
local claymoreCol = createColSphere ( x, y, z, 1 ) --create a col sphere with radius 1 | local claymoreCol = createColSphere ( x, y, z, 1 ) -- create a col sphere with radius 1 | ||
setElementData ( claymoreCol, "object", claymoreObject ) --store the object of the claymore | setElementData ( claymoreCol, "object", claymoreObject ) -- store the object of the claymore | ||
setElementData ( claymoreCol, "creatorPlayer", creator ) --store the person who created it | setElementData ( claymoreCol, "creatorPlayer", creator ) -- store the person who created it | ||
addEventHandler ( "onColShapeHit", claymoreCol, claymoreHit ) --add an event handler to the colshape | addEventHandler ( "onColShapeHit", claymoreCol, claymoreHit ) -- add an event handler to the colshape | ||
end | end | ||
function claymoreHit ( | function claymoreHit ( thePlayer, matchingDimension ) | ||
--retrieve the object associated to the claymore, and who created it | -- retrieve the object associated to the claymore, and who created it | ||
local claymoreObject = getElementData ( source, "object" ) | local claymoreObject = getElementData ( source, "object" ) | ||
local claymoreCreator = getElementData ( source, "creatorPlayer" ) | local claymoreCreator = getElementData ( source, "creatorPlayer" ) | ||
--get the position of the claymore | -- get the position of the claymore | ||
local x,y,z = getElementPosition ( source ) | local x,y,z = getElementPosition ( source ) | ||
createExplosion ( x,y,z, 12, claymoreCreator ) --create an explosion, associated to the creator, of a small size at the col's position | createExplosion ( x,y,z, 12, claymoreCreator ) -- create an explosion, associated to the creator, of a small size at the col's position | ||
--remove the event handler for the colshape | -- remove the event handler for the colshape | ||
removeEventHandler ( "onColShapeHit", source, claymoreHit ) | removeEventHandler ( "onColShapeHit", source, claymoreHit ) | ||
--destroy the claymore object, and the col shape so it doesn't trigger again. | -- destroy the claymore object, and the col shape so it doesn't trigger again. | ||
destroyElement ( claymoreObject ) | destroyElement ( claymoreObject ) | ||
destroyElement ( source ) | destroyElement ( source ) |
Revision as of 13:27, 21 August 2007
This function destroys an element and all elements withinn it in the hierarchy (its children, the children of those children etc). Player elements cannot be destroyed using this function. A player can only be removed from the hierarchy when they quit or are kicked. The root element also cannot be destroyed, however, passing the root as an argument will wipe all elements from the map, except for the players, which will become direct descendants of the root node.
Players are not the only elements that cannot be deleted. This list also includes remote clients and console elements.
Syntax
bool destroyElement ( element elementToDestroy )
Required Arguments
- elementToDestroy: The element you wish to destroy.
Returns
Returns true if the element was destroyed successfully, false if either the element passed to it was invalid or it could not be destroyed for some other reason.
Example
Example 1: This example would destroy every element in the map, with the exception of players and the root element itself.
-- Find the root element (the element that contains all others) root = getRootElement () -- Destroy all its children, except players. destroyElement ( root )
Example 2: This example destroys all vehicles of the specified model:
function destroyVehiclesOfModel(modelID) -- get a table of all the vehicles that exist and loop through it local vehicles = getElementsByType("vehicle") for i,v in ipairs(vehicles) do -- if the vehicle's ID is the one provided, destroy it if (getVehicleID(v) == modelID) then destroyElement(v) end end end
Example 3: This example allows creation of claymores, which trigger and explode. When they explode, the colshape and object for the claymore are destroyed.
function createClaymore ( x,y,z, creator ) local claymoreObject = createObject ( 1945, x, y, z - 1, 0, 0, 90 ) -- create an object which looks like a claymore local claymoreCol = createColSphere ( x, y, z, 1 ) -- create a col sphere with radius 1 setElementData ( claymoreCol, "object", claymoreObject ) -- store the object of the claymore setElementData ( claymoreCol, "creatorPlayer", creator ) -- store the person who created it addEventHandler ( "onColShapeHit", claymoreCol, claymoreHit ) -- add an event handler to the colshape end function claymoreHit ( thePlayer, matchingDimension ) -- retrieve the object associated to the claymore, and who created it local claymoreObject = getElementData ( source, "object" ) local claymoreCreator = getElementData ( source, "creatorPlayer" ) -- get the position of the claymore local x,y,z = getElementPosition ( source ) createExplosion ( x,y,z, 12, claymoreCreator ) -- create an explosion, associated to the creator, of a small size at the col's position -- remove the event handler for the colshape removeEventHandler ( "onColShapeHit", source, claymoreHit ) -- destroy the claymore object, and the col shape so it doesn't trigger again. destroyElement ( claymoreObject ) destroyElement ( source ) end
See Also
- attachElements
- createElement
- destroyElement
- detachElements
- getAttachedElements
- getElementAlpha
- getElementAttachedOffsets
- getElementAttachedTo
- getElementByIndex
- getElementByID
- getElementChild
- getElementChildren
- getElementChildrenCount
- getElementCollisionsEnabled
- getElementColShape
- getElementData
- getAllElementData
- hasElementData
- getElementDimension
- getElementHealth
- getElementID
- getElementInterior
- getElementMatrix
- getElementModel
- getElementParent
- getElementPosition
- getElementRotation
- getElementsByType
- getElementsWithinColShape
- getElementsWithinRange
- getElementType
- getElementVelocity
- getLowLODElement
- getRootElement
- isElement
- isElementAttached
- isElementCallPropagationEnabled
- isElementDoubleSided
- isElementFrozen
- isElementInWater
- isElementLowLOD
- isElementWithinColShape
- isElementWithinMarker
- setElementAlpha
- setElementAngularVelocity
- getElementAngularVelocity
- setElementAttachedOffsets
- setElementCallPropagationEnabled
- setElementCollisionsEnabled
- setElementData
- setElementDimension
- setElementDoubleSided
- setElementFrozen
- setElementHealth
- setElementID
- setElementInterior
- setElementModel
- setElementParent
- setElementPosition
- setElementRotation
- setElementVelocity
- setLowLODElement
- getPedContactElement
- getResourceDynamicElementRoot
- getResourceRootElement