GetElementsByType: Difference between revisions
No edit summary |
|||
Line 59: | Line 59: | ||
outputChatBox ( getPlayerName ( thePlayer ) .. " is on foot" ) | outputChatBox ( getPlayerName ( thePlayer ) .. " is on foot" ) | ||
end | end | ||
end | |||
</syntaxhighlight> | |||
This shows how you could create a new element to describe a gas station: | |||
<syntaxhighlight lang="lua"> | |||
function createGasStations(below) | |||
local gasstations = getElementsByType ( "gasstation", below ) -- get a table of all the players in the server | |||
for theKey,theGasStation in ipairs(gasstations) do -- use a generic for loop to step through each player | |||
x = getElementData(theGasStation, "posX") | |||
y = getElementData(theGasStation, "posY") | |||
z = getElementData(theGasStation, "posZ") | |||
setElementParent(createColSphere(x, y, z, 10), theGasStation) | |||
addEventHandler("onColShapeHit", theGasStation, giveGas) | |||
end | |||
end | |||
function mapLoad() | |||
createGasStations(source) -- create gas stations for the map that's just loaded | |||
end | |||
addEventHandler("onResourceStart", getRootElement(), mapLoad) | |||
function giveGas(hittingElement) | |||
local theGasStation = source | |||
if getElementType(hittingElement) == "vehicle": | |||
gas_left = getElementData(theGasStation, "amount") | |||
gas_speed = getElementData(theGasStation, "speed") | |||
if gas_left > 0 then | |||
if gas_left < gas_speed then | |||
gas_to_remove = gas_left | |||
else | |||
gas_to_remove = gas_speed | |||
end | |||
current_vehicle_gas = getElementData(hittingElement, "gas") | |||
current_vehicle_gas = current_vehicle_gas + gas_to_remove | |||
gas_left = gas_left - gas_to_remove | |||
setElementData(hittingElement, "gas", current_vehicle_gas) | |||
setElementData(theGasStation, "amount", gas_left) | |||
else | |||
outputChatBox("Pump is out of gas!") | |||
end | |||
end | |||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 10:39, 1 June 2010
This function is used to retrieve a list of all elements of the specified type. This can be useful, as it disregards where in the element tree it is. It can be used with either the built in types (listed below) or with any custom type used in a .map file. For example, if there is an element of type "flag" (e.g. <flag />) in the .map file, the using "flag" as the type argument would find it.
Syntax
table getElementsByType ( string theType, [ element startat=getRootElement() ] )
table getElementsByType ( string theType, [ element startat=getRootElement(), bool streamedIn=false ] )
Required Arguments
- theType: The type of element you want a list of. This is the same as the tag name in the .map file, so this can be used with a custom element type if desired. Built in types are:
- "player": A player connected to the server
- "ped": A ped
- "water": A water polygon
- "sound": A playing sound
- "vehicle": A vehicle
- "object": An object
- "pickup": A pickup
- "marker": A marker
- "colshape": A collision shape
- "blip": A blip
- "radararea": A radar area
- "team": A team
- "spawnpoint": A spawnpoint
- "remoteclient": A remote client connected to the server
- "console": The server Console
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.
- startat: The element the search should start at. Children of this element are searched, siblings or parents will not be found. By default, this is the root element which should suit most uses.
- streamedIn: If true, function will only return elements that are streamed in.
(PS: lil_Toady is a commie)
Returns
Returns a table containing all the elements of the specified type. Returns an empty table if there are no elements of the specified type. Returns false if the string specified is invalid (or not a string).
Example
This example retrieves a table of the players in the server, and checks whether or not each one is in a vehicle:
local players = getElementsByType ( "player" ) -- get a table of all the players in the server for theKey,thePlayer in ipairs(players) do -- use a generic for loop to step through each player if ( isPlayerInVehicle ( thePlayer ) ) then -- if the player is in a vehicle, announce it outputChatBox ( getPlayerName ( thePlayer ) .. " is in a vehicle" ) else -- if the player isn't in a vehicle, announce that he/she is on foot outputChatBox ( getPlayerName ( thePlayer ) .. " is on foot" ) end end
This shows how you could create a new element to describe a gas station:
function createGasStations(below) local gasstations = getElementsByType ( "gasstation", below ) -- get a table of all the players in the server for theKey,theGasStation in ipairs(gasstations) do -- use a generic for loop to step through each player x = getElementData(theGasStation, "posX") y = getElementData(theGasStation, "posY") z = getElementData(theGasStation, "posZ") setElementParent(createColSphere(x, y, z, 10), theGasStation) addEventHandler("onColShapeHit", theGasStation, giveGas) end end function mapLoad() createGasStations(source) -- create gas stations for the map that's just loaded end addEventHandler("onResourceStart", getRootElement(), mapLoad) function giveGas(hittingElement) local theGasStation = source if getElementType(hittingElement) == "vehicle": gas_left = getElementData(theGasStation, "amount") gas_speed = getElementData(theGasStation, "speed") if gas_left > 0 then if gas_left < gas_speed then gas_to_remove = gas_left else gas_to_remove = gas_speed end current_vehicle_gas = getElementData(hittingElement, "gas") current_vehicle_gas = current_vehicle_gas + gas_to_remove gas_left = gas_left - gas_to_remove setElementData(hittingElement, "gas", current_vehicle_gas) setElementData(theGasStation, "amount", gas_left) else outputChatBox("Pump is out of gas!") end end 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