SetGarageOpen: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Improved example)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
This function opens or closes the specified garage door in the world.
This function opens or closes the specified garage door in the world.
{{Note|setGarageOpen does not work with ID 32 (Pay 'n' Spray near Royal Casino). This garage has been disabled by Rockstar Games due to floor collision issues (see TheJizzy's video "BETA Leftovers and Glitches" at 12:12 timestamp). You can remove the door by using [[removeWorldModel]] and recreating it for later with [[moveObject]].}}
{{Note|setGarageOpen does not work with ID 32 (Pay 'n' Spray near Royal Casino). This garage has been disabled by Rockstar Games due to floor collision issues (see TheJizzy's video "BETA Leftovers and Glitches" at 12:12 timestamp). You can remove the door by using [[removeWorldModel]] and recreating it for later with [[moveObject]].}}
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 19: Line 21:
This example opens a garage door when a player enters a collision shape near it, and closes it when they leave:
This example opens a garage door when a player enters a collision shape near it, and closes it when they leave:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
GARAGE_ID = 25
local GARAGE_ID = 25


-- create a collision shape and attach event handlers to it when the resource starts
-- create a collision shape and attach event handlers to it when the resource starts
addEventHandler("onResourceStart", getResourceRootElement(),
addEventHandler("onResourceStart", resourceRoot, function (resource)
function (resource)
local garageCube = createColCuboid(1337, 194, 28, 6, 10, 4)
local garageCube = createColCuboid(1337, 194, 28, 6, 10, 4)
addEventHandler("onColShapeHit", garageCube, onGarageCubeHit)
addEventHandler("onColShapeHit", garageCube, onGarageCubeHit)
Line 31: Line 32:
-- open the door when someone enters the garage's collision shape
-- open the door when someone enters the garage's collision shape
function onGarageCubeHit(hitElement, matchingDimension)
function onGarageCubeHit(hitElement, matchingDimension)
if (getElementType(hitElement) == "player") then
if getElementType(hitElement) ~= "player" then
-- check to make sure the door is closed
return
if (not isGarageOpen(GARAGE_ID)) then
-- open the door
setGarageOpen(GARAGE_ID, true)
end
end
end
-- check to make sure the door is closed
-- open if they are closed
setGarageOpen(GARAGE_ID, not isGarageOpen(GARAGE_ID))
end
end


-- close the door when someone leaves the garage's collision shape
-- close the door when someone leaves the garage's collision shape
function onGarageCubeLeave(leaveElement, matchingDimension)
function onGarageCubeLeave(leaveElement, matchingDimension)
if (getElementType(leaveElement) == "player") then
if getElementType(leaveElement) ~= "player" then
-- check to make sure the door is open
return
if (isGarageOpen(GARAGE_ID)) then
-- close the door
setGarageOpen(GARAGE_ID, false)
end
end
end
-- check to make sure the door is open
-- close if they are opened
setGarageOpen(GARAGE_ID, not isGarageOpen(GARAGE_ID))
end
end
</syntaxhighlight>
</syntaxhighlight>
Line 57: Line 56:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local garages = {
local garages = {
    [0]  = Vector3(1643.43, -1520.3, 14.3438),
{ 1643.43, -1520.3, 14.3438, },
    [1]  = nil, -- LSPD Police Impound Garage (not working)
{ 1877.41, -2096.51, 14.0391, },
    [2]  = Vector3(1877.41, -2096.51, 14.0391),
     { 1843.37, -1856.32, 13.875, },
     [3]  = Vector3(1843.37, -1856.32, 13.875),
     { 1798.69, -2146.73, 14, },
     [4]  = Vector3(1798.69, -2146.73, 14),
     { 1698.91, -2088.74, 14.1406, },
     [5]  = Vector3(1698.91, -2088.74, 14.1406),
     { 2741.07, -2004.78, 14.875, },
     [6]  = Vector3(2741.07, -2004.78, 14.875),
     { 2644.86, -2039.23, 14.0391, },
     [7]  = Vector3(2644.86, -2039.23, 14.0391),
     { 2071.48, -1831.42, 14.5625, },
     [8]  = Vector3(2071.48, -1831.42, 14.5625),
     { 2505.52, -1690.99, 14.3281, },
     [9]  = Vector3(2505.52, -1690.99, 14.3281),
     { 1041.35, -1025.93, 32.6719, },
     [10] = Vector3(1041.35, -1025.93, 32.6719),
     { 1024.98, -1029.35, 33.1953, },
     [11] = Vector3(1024.98, -1029.35, 33.1953),
     { 488.28, -1734.7, 12.3906, },
     [12] = Vector3(488.28, -1734.7, 12.3906),
     { 322.4141, -1769.0312, 5.25, },
     [13] = Vector3(322.4141, -1769.0312, 5.25),
     { 1353.48, -626.63, 109.82, },
     [14] = Vector3(1353.48, -626.63, 109.82),
     { -2716.35, 217.48, 5.3828, },
     [15] = Vector3(-2716.35, 217.48, 5.3828),
     { -2730.47, 72.32, 5.3516, },
     [16] = Vector3(-2730.47, 72.32, 5.3516),
     { -2454.12, -123.06, 26.9844, },
     [17] = Vector3(-2454.12, -123.06, 26.9844),
     { -1935.86, 239.53, 35.3516, },
     [18] = Vector3(-1935.86, 239.53, 35.3516),
     { -1904.53, 277.9, 42.9531, },
     [19] = Vector3(-1904.53, 277.9, 42.9531),
     { -2102.93, -16.05, 36.4844, },
     [20] = Vector3(-2102.93, -16.05, 36.4844),
     { -2026.91, 129.41, 30.4531, },
     [21] = Vector3(-2026.91, 129.41, 30.4531),
     { -2038.93, 178.81, 29.9375, },
     [22] = Vector3(-2038.93, 178.81, 29.9375),
     { -2162.03, 654.66, 53.375, },
     [23] = Vector3(-2162.03, 654.66, 53.375),
     { -1786.81, 1209.42, 25.8359, },
     [24] = Vector3(-1786.81, 1209.42, 25.8359),
     { -2105.2, 896.93, 77.4453, },
     [25] = Vector3(-2105.2, 896.93, 77.4453),
     { -2425.73, 1027.99, 52.2812, },
    [26] = nil, -- SFPD Police Impound Garage (not working)
     { -2696.01, 821.45, 50.8516, },
     [27] = Vector3(-2425.73, 1027.99, 52.2812),
     { 1586.26, 1222.7, 19.75, },
     [28] = Vector3(-2696.01, 821.45, 50.8516),
     { 2609.52, 1438.37, 11.5938, },
    [29] = nil, -- LVPD Police Impound Garage (not working)
     { 2386.66, 1043.6, 11.5938, },
     [30] = Vector3(1586.26, 1222.7, 19.75),
     { 2449.55, 698.08, 11.6797, },
     [31] = Vector3(2609.52, 1438.37, 11.5938),
     { 1968.74, 2162.49, 12.0938, },
    [32] = nil, -- Pay 'n' Spray (Royal Casino) (not working)
     { 1408.64, 1902.69, 11.6797, },
     [33] = Vector3(2386.66, 1043.6, 11.5938),
     { 1278.7, 2529.81, 11.3203, },
     [34] = Vector3(2449.55, 698.08, 11.6797),
     { 929.55, 2012.06, 11.6797, },
     [36] = Vector3(1968.74, 2162.49, 12.0938),
     { -1420.55, 2591.16, 57.7422, },
     [37] = Vector3(1408.64, 1902.69, 11.6797),
     { -100, 1111.41, 21.6406, },
     [38] = Vector3(1278.7, 2529.81, 11.3203),
     { -360.77, 1194.26, 20.5938, },
     [39] = Vector3(929.55, 2012.06, 11.6797),
     { 429.98, 2546.52, 17.3516, },
     [40] = Vector3(-1420.55, 2591.16, 57.7422),
     { -389.59, 2227.91, 42.9219, },
     [41] = Vector3(-100, 1111.41, 21.6406),
     { 397.48, 2476.63, 19.5156, },
     [42] = Vector3(-360.77, 1194.26, 20.5938),
    { 412.12, 2476.63, 19.5156, },
     [43] = Vector3(429.98, 2546.52, 17.3516),
     { -2113.04, -2460.62, 30.9141, },
     [44] = Vector3(-389.59, 2227.91, 42.9219),
     { 720.02, -462.52, 16.8594, },
     [45] = {
     { 2231.24, 168.73, 27.7734, },
        Vector3(397.48, 2476.63, 19.5156),
     { 786.01, -492.87, 17.632, },
        Vector3(412.12, 2476.63, 19.5156)
    },
     [46] = Vector3(-2113.04, -2460.62, 30.9141),
     [47] = Vector3(720.02, -462.52, 16.8594),
     [48] = Vector3(2231.24, 168.73, 27.7734),
     [49] = Vector3(786.01, -492.87, 17.6328)
}
}


local function createGarageColShape(pos, ID)
local function createGarageColShape(x, y, z, ID)
     local col = createColSphere(pos.x, pos.y, pos.z, 7)
     local col = createColSphere(x, y, z, 7)
      
      
     addEventHandler("onColShapeHit", col, function(hitElement, matchingDimension)
     addEventHandler("onColShapeHit", col, function(hitElement, matchingDimension)
         if getElementType(hitElement) == "player" then
         if getElementType(hitElement) ~= "player" then
             if not isGarageOpen(ID) then
             return
                setGarageOpen(ID, true)
            end
         end
         end
        setGarageOpen(ID, not isGarageOpen(ID))
     end)
     end)
      
      
     addEventHandler("onColShapeLeave", col, function(leaveElement, matchingDimension)
     addEventHandler("onColShapeLeave", col, function(leaveElement, matchingDimension)
         if getElementType(leaveElement) == "player" then
         if getElementType(hitElement) ~= "player" then
             if isGarageOpen(ID) then
             return
                setGarageOpen(ID, false)
            end
         end
         end
        setGarageOpen(ID, not isGarageOpen(ID))
     end)
     end)
end
end


addEventHandler("onResourceStart", getResourceRootElement(), function(res)
addEventHandler("onResourceStart", resourceRoot, function(res)
     for ID, pos in pairs(garages) do
     for ID, pos in ipairs(garages) do
         if pos then
         createGarageColShape(pos[1], pos[2], pos[3], ID)
            if (pos and type(pos) == "table") then
                for _, subPos in ipairs(pos) do
                    createGarageColShape(subPos, ID)
                end
            else
                createGarageColShape(pos, ID)
            end
        end
     end
     end
end)
end)

Latest revision as of 08:05, 23 July 2024

This function opens or closes the specified garage door in the world.

[[{{{image}}}|link=|]] Note: setGarageOpen does not work with ID 32 (Pay 'n' Spray near Royal Casino). This garage has been disabled by Rockstar Games due to floor collision issues (see TheJizzy's video "BETA Leftovers and Glitches" at 12:12 timestamp). You can remove the door by using removeWorldModel and recreating it for later with moveObject.

Syntax

bool setGarageOpen ( int garageID, bool open )

Required Arguments

  • garageID: The garage ID that represents the garage door being opened or closed.
  • isOpen: A boolean indicating whether or not to open the door.

Returns

Returns true if successful, false if an invalid garage id was given.

Example

Click to collapse [-]
Server

This example opens a garage door when a player enters a collision shape near it, and closes it when they leave:

local GARAGE_ID = 25

-- create a collision shape and attach event handlers to it when the resource starts
addEventHandler("onResourceStart", resourceRoot, function (resource)
	local garageCube = createColCuboid(1337, 194, 28, 6, 10, 4)
	addEventHandler("onColShapeHit", garageCube, onGarageCubeHit)
	addEventHandler("onColShapeLeave", garageCube, onGarageCubeLeave)
end)

-- open the door when someone enters the garage's collision shape
function onGarageCubeHit(hitElement, matchingDimension)
	if getElementType(hitElement) ~= "player" then
		return
	end
	-- check to make sure the door is closed
	-- open if they are closed
	setGarageOpen(GARAGE_ID, not isGarageOpen(GARAGE_ID))
end

-- close the door when someone leaves the garage's collision shape
function onGarageCubeLeave(leaveElement, matchingDimension)
	if getElementType(leaveElement) ~= "player" then
		return
	end
	-- check to make sure the door is open
	-- close if they are opened
	setGarageOpen(GARAGE_ID, not isGarageOpen(GARAGE_ID))
end
Click to collapse [-]
Server

This example opens each garage door when a player enters a collision shape near it, and closes it when they leave:

local garages = {
	{ 1643.43, -1520.3, 14.3438, },
	{ 1877.41, -2096.51, 14.0391, },
    { 1843.37, -1856.32, 13.875, },
    { 1798.69, -2146.73, 14, },
    { 1698.91, -2088.74, 14.1406, },
    { 2741.07, -2004.78, 14.875, },
    { 2644.86, -2039.23, 14.0391, },
    { 2071.48, -1831.42, 14.5625, },
    { 2505.52, -1690.99, 14.3281, },
    { 1041.35, -1025.93, 32.6719, },
    { 1024.98, -1029.35, 33.1953, },
    { 488.28, -1734.7, 12.3906, },
    { 322.4141, -1769.0312, 5.25, },
    { 1353.48, -626.63, 109.82, },
    { -2716.35, 217.48, 5.3828, },
    { -2730.47, 72.32, 5.3516, },
    { -2454.12, -123.06, 26.9844, },
    { -1935.86, 239.53, 35.3516, },
    { -1904.53, 277.9, 42.9531, },
    { -2102.93, -16.05, 36.4844, },
    { -2026.91, 129.41, 30.4531, },
    { -2038.93, 178.81, 29.9375, },
    { -2162.03, 654.66, 53.375, },
    { -1786.81, 1209.42, 25.8359, },
    { -2105.2, 896.93, 77.4453, },
    { -2425.73, 1027.99, 52.2812, },
    { -2696.01, 821.45, 50.8516, },
    { 1586.26, 1222.7, 19.75, },
    { 2609.52, 1438.37, 11.5938, },
    { 2386.66, 1043.6, 11.5938, },
    { 2449.55, 698.08, 11.6797, },
    { 1968.74, 2162.49, 12.0938, },
    { 1408.64, 1902.69, 11.6797, },
    { 1278.7, 2529.81, 11.3203, },
    { 929.55, 2012.06, 11.6797, },
    { -1420.55, 2591.16, 57.7422, },
    { -100, 1111.41, 21.6406, },
    { -360.77, 1194.26, 20.5938, },
    { 429.98, 2546.52, 17.3516, },
    { -389.59, 2227.91, 42.9219, },
    { 397.48, 2476.63, 19.5156, },
    { 412.12, 2476.63, 19.5156, },
    { -2113.04, -2460.62, 30.9141, },
    { 720.02, -462.52, 16.8594, },
    { 2231.24, 168.73, 27.7734, },
    { 786.01, -492.87, 17.632, },
}

local function createGarageColShape(x, y, z, ID)
    local col = createColSphere(x, y, z, 7)
    
    addEventHandler("onColShapeHit", col, function(hitElement, matchingDimension)
        if getElementType(hitElement) ~= "player" then
            return
        end
        setGarageOpen(ID, not isGarageOpen(ID))
    end)
    
    addEventHandler("onColShapeLeave", col, function(leaveElement, matchingDimension)
        if getElementType(hitElement) ~= "player" then
            return
        end
        setGarageOpen(ID, not isGarageOpen(ID))
    end)
end

addEventHandler("onResourceStart", resourceRoot, function(res)
    for ID, pos in ipairs(garages) do
        createGarageColShape(pos[1], pos[2], pos[3], ID)
    end
end)

See Also