GetElementsWithinRange: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Correct circle to sphere)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Server client function}}
{{Server client function}}
__NOTOC__
__NOTOC__
{{New feature/item|3.0156|1.5.5|13823|This function is used to retrieve a list of all elements of specified type within a range of 3D coordinates.}}
This function is used to retrieve a list of all elements of specified type within a range of 3D coordinates.
{{Deprecated items|3.0160|1.5.9|
* Z argument isn't in use currently, but make your scripts like it is for future compatibility reasons.
|21438}}
{{New feature/item|3.0160|1.5.9|21438|
* Z argument is now being taken into consideration when checking for elements.
}}
{{Note|
{{Note|
* This function doesn't verify whether elements are in the same dimension and interior, additional checks could be implemented manually if they are needed.
* This function checks if elements are in a box, not in a sphere.
* This function checks if elements are in a box, not in a sphere.
* Z argument isn't in use currently, but make your scripts like it is for future compatibility reasons.
* This function doesn't work with elements which are created by createElement.
* This function doesn't work with elements which is created by createElement.}}
}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table getElementsWithinRange ( float x, float y, float z, float range [, string elemType = "" ] )
table getElementsWithinRange ( float x, float y, float z, float range [, string elemType = "", int interior, int dimension ] )
</syntaxhighlight>
</syntaxhighlight>
{{OOP| |[[Element]].getWithinRange}}
{{OOP| |[[Element]].getWithinRange}}
===Required Arguments===
===Required Arguments===
*'''x:''' the x coordinate at which to retrieve elements
*'''x:''' the x coordinate at which to retrieve elements.
*'''y:''' the y coordinate at which to retrieve elements
*'''y:''' the y coordinate at which to retrieve elements.
*'''z:''' the z coordinate at which to retrieve elements
*'''z:''' the z coordinate at which to retrieve elements.
*'''range:''' the range at the coordinates in which to retrieve elements
*'''range:''' the range at the coordinates in which to retrieve elements.


===Optional Arguments===
===Optional Arguments===
*'''elemType:''' The type of element you want a list of. This can be any element type, such as:
*'''elemType:''' The type of element you want a list of. This can be any element type, such as:
**'''"player":''' A player connected to the server
**'''"player":''' A player connected to the server.
**'''"ped":''' A ped
**'''"ped":''' A ped.
**'''"vehicle":''' A vehicle
**'''"vehicle":''' A vehicle.
**'''"object":''' An object
**'''"object":''' An object.
**'''"pickup":''' A pickup
**'''"pickup":''' A pickup.
**'''"marker":''' A marker
**'''"marker":''' A marker.
{{Added feature/item|1.5.9|1.5.8|20797|
*'''interior:''' The [[interior]] you want to limit the search to. If not specified, it can return elements in any interior.
*'''dimension:''' The [[dimension]] you want to limit the search to. If not specified, it can return elements in any dimension.
}}


===Returns===
===Returns===
Line 34: Line 43:
This example allows admins to destroy all vehicles in close proximity.
This example allows admins to destroy all vehicles in close proximity.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function deleteNearbyVehicles(p)
function deleteNearbyVehicles(playerElement)
local acc = getPlayerAccount(p)
local playerAccount = getPlayerAccount(playerElement)
if not isGuestAccount(acc) and isObjectInACLGroup("user."..getAccountName(acc), aclGetGroup("Admin")) then
 
local x, y, z = getElementPosition(p)
if (not playerAccount) then
local nearbyVehicles = getElementsWithinRange(x, y, z, 300, "vehicle")
return false
for i,v in ipairs(nearbyVehicles) do
end
if v and isElement(v) then
 
destroyElement(v)
local guestAccount = isGuestAccount(playerAccount)
end
 
if (guestAccount) then
return false
end
 
local accountName = getAccountName(playerAccount)
local aclObject = "user."..accountName
local adminGroup = aclGetGroup("Admin")
local playerAdmin = isObjectInACLGroup(aclObject, adminGroup)
 
if (not playerAdmin) then
return false
end
 
local playerX, playerY, playerZ = getElementPosition(playerElement)
local playerInterior = getElementInterior(playerElement)
local playerDimension = getElementDimension(playerElement)
local searchRange = 300
local nearbyVehicles = getElementsWithinRange(playerX, playerY, playerZ, searchRange, "vehicle", playerInterior, playerDimension)
 
for vehicleID = 1, #nearbyVehicles do
local vehicleElement = nearbyVehicles[vehicleID]
local validElement = isElement(vehicleElement)
 
if (validElement) then
destroyElement(vehicleElement)
end
end
end
end
end
end
addCommandHandler ("delnearvehs", deleteNearbyVehicles)
addCommandHandler("deletenearbyvehs", deleteNearbyVehicles)
</syntaxhighlight>
</syntaxhighlight>


Line 52: Line 86:
This example retrieves a table of players within range of the 3D coordinates and prints their name to the chat.
This example retrieves a table of players within range of the 3D coordinates and prints their name to the chat.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local players = getElementsWithinRange( 0, 0, 3, 20, "player" )   -- get all player elements within 20 units of 0, 0, 3
local playersTable = getElementsWithinRange(0, 0, 3, 20, "player") -- get all player elements within 20 units of 0, 0, 3
for _, player in pairs( players ) do                               -- use a generic for loop to step through each player
 
    outputChatBox( getPlayerName( player ) .. " is within range" ) -- print their name to the chat
for _, playerElement in pairs(playersTable) do -- use a generic for loop to step through each player
local playerName = getPlayerName(playerElement) -- get player name
 
outputChatBox(playerName.." is within range") -- print their name to the chat
end
end
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 15:38, 3 May 2024

This function is used to retrieve a list of all elements of specified type within a range of 3D coordinates.

BEFORE VERSION 1.5.9 r21438:
  • Z argument isn't in use currently, but make your scripts like it is for future compatibility reasons.
  • Z argument is now being taken into consideration when checking for elements.
[[{{{image}}}|link=|]] Note:
  • This function checks if elements are in a box, not in a sphere.
  • This function doesn't work with elements which are created by createElement.

Syntax

table getElementsWithinRange ( float x, float y, float z, float range [, string elemType = "", int interior, int dimension ] )

OOP Syntax Help! I don't understand this!

Method: Element.getWithinRange(...)


Required Arguments

  • x: the x coordinate at which to retrieve elements.
  • y: the y coordinate at which to retrieve elements.
  • z: the z coordinate at which to retrieve elements.
  • range: the range at the coordinates in which to retrieve elements.

Optional Arguments

  • elemType: The type of element you want a list of. This can be any element type, such as:
    • "player": A player connected to the server.
    • "ped": A ped.
    • "vehicle": A vehicle.
    • "object": An object.
    • "pickup": A pickup.
    • "marker": A marker.
  • interior: The interior you want to limit the search to. If not specified, it can return elements in any interior.
  • dimension: The dimension you want to limit the search to. If not specified, it can return elements in any dimension.

Returns

Returns a table containing all the elements of the specified type within range. Returns an empty table if there are no elements within range. Returns false if the arguments are invalid.

Example

This example allows admins to destroy all vehicles in close proximity.

function deleteNearbyVehicles(playerElement)
	local playerAccount = getPlayerAccount(playerElement)

	if (not playerAccount) then
		return false
	end

	local guestAccount = isGuestAccount(playerAccount)

	if (guestAccount) then
		return false
	end

	local accountName = getAccountName(playerAccount)
	local aclObject = "user."..accountName
	local adminGroup = aclGetGroup("Admin")
	local playerAdmin = isObjectInACLGroup(aclObject, adminGroup)

	if (not playerAdmin) then
		return false
	end

	local playerX, playerY, playerZ = getElementPosition(playerElement)
	local playerInterior = getElementInterior(playerElement)
	local playerDimension = getElementDimension(playerElement)
	local searchRange = 300
	local nearbyVehicles = getElementsWithinRange(playerX, playerY, playerZ, searchRange, "vehicle", playerInterior, playerDimension)

	for vehicleID = 1, #nearbyVehicles do
		local vehicleElement = nearbyVehicles[vehicleID]
		local validElement = isElement(vehicleElement)

		if (validElement) then
			destroyElement(vehicleElement)
		end
	end
end
addCommandHandler("deletenearbyvehs", deleteNearbyVehicles)

Example

This example retrieves a table of players within range of the 3D coordinates and prints their name to the chat.

local playersTable = getElementsWithinRange(0, 0, 3, 20, "player") -- get all player elements within 20 units of 0, 0, 3

for _, playerElement in pairs(playersTable) do -- use a generic for loop to step through each player
	local playerName = getPlayerName(playerElement) -- get player name

	outputChatBox(playerName.." is within range") -- print their name to the chat
end

See Also

Shared