SetGarageOpen: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Improved example)
 
(7 intermediate revisions by 2 users 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. 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>
</section>
<section name="Server" class="server" show="true">
This example opens each garage door when a player enters a collision shape near it, and closes it when they leave:
<syntaxhighlight lang="lua">
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)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

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