Skip to content

Commit 2568b0f

Browse files
authored
Add getElementPosition and setElementPosition (PR #1)
2 parents f0300db + a816ba1 commit 2568b0f

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Create the elegy
2+
local myElegy = createVehicle(562, 1591.596680, -2495.323242, 18.098244)
3+
-- Get the vehicle's position
4+
local x, y, z = getElementPosition(myElegy)
5+
-- Create the samsite
6+
local samsite = createObject(3267, x, y, z + 3)
7+
-- Attach the samsite to the elegy
8+
attachElementToElement(samsite, myElegy, 0, 0, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function randomPlayersToLocation(p)
2+
if not isPlayerStaff(p) then return end
3+
4+
local playersOnline = getElementsByType("player")
5+
local amount = #playersOnline
6+
7+
if amount == 0 then return end
8+
9+
for index = 1,(amount > 5 and 5 or amount) do
10+
local player = playersOnline[index]
11+
setElementPosition(player, getElementPosition(p))
12+
end
13+
end
14+
addCommandHandler("randomtp", randomPlayersToLocation)
15+
addCommandHandler("playershere", randomPlayersToLocation)
16+
17+
-- Utility function
18+
local staffACLs = {
19+
aclGetGroup("Admin"),
20+
aclGetGroup("Moderator")
21+
}
22+
23+
function isPlayerStaff(p)
24+
if isElement(p) and getElementType(p) == "player" and not isGuestAccount(getPlayerAccount(p)) then
25+
local object = getAccountName(getPlayerAccount(p))
26+
27+
for _, group in ipairs(staffACLs) do
28+
if isObjectInACLGroup("user." .. object, group) then
29+
return true
30+
end
31+
end
32+
end
33+
return false
34+
end
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
shared: &shared
2+
name: 'getElementPosition'
3+
oop:
4+
entity: player
5+
method: getPosition
6+
variable: position
7+
pair: 'setElementPosition'
8+
description: |
9+
This function allows you to retrieve the position coordinates of an element. This can be any real world element, including:
10+
- [[player|Players]]
11+
- [[vehicle|Vehicles]]
12+
- [[object|Objects]]
13+
- [[pickup|Pickups]]
14+
- [[marker|Markers]]
15+
- [[collision shape|Collision shapes]]
16+
- [[blip|Blips]]
17+
- [[radar area|Radar areas]]
18+
returns:
19+
description: |
20+
Returns three floats indicating the position of the element, x, y and z respectively.
21+
values:
22+
- type: 'float'
23+
name: 'x'
24+
- type: 'float'
25+
name: 'y'
26+
- type: 'float'
27+
name: 'z'
28+
examples:
29+
- path: 'examples/getElementPosition.lua'
30+
description: |
31+
This example attaches a samsite on the player's vehicle.
32+
33+
server:
34+
<<: *shared
35+
36+
client:
37+
<<: *shared
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
shared:
2+
name: 'setElementPosition'
3+
oop:
4+
entity: player
5+
method: setPosition
6+
variable: position
7+
pair: 'getElementPosition'
8+
description: |
9+
This function sets the position of an element to the specified coordinates.
10+
notes:
11+
- |
12+
Warning: Do not use this function to spawn a [[player]]. It will cause problems with other functions like [[warpPedIntoVehicle]]. Use [[spawnPlayer]] instead.
13+
- |
14+
If you want to put a vehicle or player out of the water or simulate the position-resetting behaviour if CJ goes below the ground too far, then you need to retrieve a recommended coordinate on ground to place the element at. Take a look at this MTA forums post for steps in the right direction.
15+
https://forum.mtasa.com/topic/132891-important-helprespawn-vehicle/?do=findComment&comment=1003198
16+
returns:
17+
description: |
18+
Returns *true* if the function was successful, *false* otherwise.
19+
values:
20+
- type: 'bool'
21+
name: 'result'
22+
examples:
23+
- path: 'examples/setElementPosition.lua'
24+
description: |
25+
This example lets admins teleport 5 random players to themselves
26+
issues:
27+
- id: 539
28+
description: 'Changing player position when they have a jetpack will remove the jetpack and bug when skin is changed'
29+
- id: 529
30+
description: 'Player falls from bike when the bike is teleported using setElementPosition'

0 commit comments

Comments
 (0)