CreateColRectangle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(29 intermediate revisions by 14 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
This function creates a collision rectangle. This is a shape that has a position and a width and a depth. See [http://en.wikipedia.org/wiki/Rectangle Rectangle] for a definition of a rectangle.
{{Server client function}}
 
This function creates a collision rectangle. This is a shape that has a position and a width and a depth. See [http://en.wikipedia.org/wiki/Rectangle Rectangle] for a definition of a rectangle. XY marks on the south west corner of the colshape.
{{VisualizeColshape}}
{{Note|Attaching a rectangle colshape to another element may give unexpected results as the origin is not at the rectangle centre. Try using a [[createColCircle|collision circle]] for attaching instead}}
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
colshape createColRectangle ( float fX, float fY, float fZ, float fWidth, float fDepth )
colshape createColRectangle ( float fX, float fY, float fWidth, float fHeight )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP| |ColShape.Rectangle||}}
===Required Arguments===  
===Required Arguments===  
*'''fX:''' The collision rectangle's center point's X axis position
*'''fX:''' The X position of the collision rectangle's west side.
*'''fY:''' The collision rectangle's center point's Y axis position
*'''fY:''' The Y position of the collision rectangle's south side.
*'''fZ:''' The collision rectangle's center point's Z axis position
*'''fWidth:''' The collision rectangle's width.
*'''fWidth:''' The collision rectangle's width
*'''fHeight:''' The collision rectangle's height.
*'''fDepth:''' The collision rectangle's depth


===Returns===
===Returns===
Line 18: Line 19:


==Example==  
==Example==  
<section name="Server" class="server" show="true">
This example displays a chat message when a player enters the colshape and allows the colshape to be created using a console function ''set_zone''.
This example displays a chat message when a player enters the colshape and allows the colshape to be created using a console function ''set_zone''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
theZone = false
local theZone
 
function shapeHit(thePlayer)
    outputChatBox(getPlayerName(thePlayer).. " is in the zone!")
end


addCommandHandler ( "set_zone", "setZone" ) -- add a console function called set_zone that will trigger the function setZone
function setZone(playerSource, commandName, fX, fY, fWidth, fHeight)
function setZone ( playerSource, commandName, fX, fY, fZ )
     local fX, fY, fWidth, fHeight = tonumber(fX), tonumber(fY), tonumber(fWidth), tonumber(fHeight)
     if ( fZ and fY and fX ) then -- check we've got all 3 args we need
    if (not fX) or (not fY) or (not fWidth) or (not fHeight) then
        local tempCol = createColRectangle ( fX, fY, fZ, 10.0, 10.0 ) -- create a col
        outputChatBox("Syntax: /"..commandName.." [X] [Y] [Width] [Height]", playerSource)
        if ( tempCol == false ) then -- did the col get created successfully?
    else
            outputConsole ( "Syntax is: set_zone <X> <Y> <Z>" ) -- inform the user what the valid syntax is
        if (theZone ~= nil) then
        else
            destroyElement(theZone)
            if ( theZone ~= false ) then -- did we already have a zone?
                destroyElement ( theZone ) -- if so, destroy it
            end
            theZone = tempCol -- and store the new zone we've made
            outputChatBox ( "Zone has moved!" ) -- and tell everyone
         end
         end
        local tempCol = createColRectangle(fX, fY, fWidth, fHeight)
        addEventHandler("onColShapeHit", tempCol, shapeHit)
        outputChatBox("Zone has "..(theZone ~= nil and "moved" or "created").."!", playerSource)
        theZone = tempCol
     end
     end
end
end
 
addCommandHandler("set_zone", setZone, false, false)
addEventHandler ( "onColShapeHit", getRootElement(), "shapeHit" ) -- add a handler for the onColShapeHit event
function shapeHit ( thePlayer )
    outputChatBox ( getClientName ( thePlayer ) .. " is in the zone!" ) -- display a message in everyone's chat box
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Collision shape functions}}
{{Collision shape functions}}
[[hu:createColRectangle]]

Latest revision as of 09:18, 6 November 2023

This function creates a collision rectangle. This is a shape that has a position and a width and a depth. See Rectangle for a definition of a rectangle. XY marks on the south west corner of the colshape.

[[{{{image}}}|link=|]] Tip: To visualize a colshape when writing scripts, use the client console command showcol
[[{{{image}}}|link=|]] Note: Attaching a rectangle colshape to another element may give unexpected results as the origin is not at the rectangle centre. Try using a collision circle for attaching instead

Syntax

colshape createColRectangle ( float fX, float fY, float fWidth, float fHeight )

OOP Syntax Help! I don't understand this!

Method: ColShape.Rectangle(...)


Required Arguments

  • fX: The X position of the collision rectangle's west side.
  • fY: The Y position of the collision rectangle's south side.
  • fWidth: The collision rectangle's width.
  • fHeight: The collision rectangle's height.

Returns

Returns a colshape element if successful, false if invalid arguments were passed to the function.

Example

Click to collapse [-]
Server

This example displays a chat message when a player enters the colshape and allows the colshape to be created using a console function set_zone.

local theZone

function shapeHit(thePlayer)
    outputChatBox(getPlayerName(thePlayer).. " is in the zone!")
end

function setZone(playerSource, commandName, fX, fY, fWidth, fHeight)
    local fX, fY, fWidth, fHeight = tonumber(fX), tonumber(fY), tonumber(fWidth), tonumber(fHeight)
    if (not fX) or (not fY) or (not fWidth) or (not fHeight) then
        outputChatBox("Syntax: /"..commandName.." [X] [Y] [Width] [Height]", playerSource)
    else
        if (theZone ~= nil) then
            destroyElement(theZone)
        end
        local tempCol = createColRectangle(fX, fY, fWidth, fHeight)
        addEventHandler("onColShapeHit", tempCol, shapeHit)
        outputChatBox("Zone has "..(theZone ~= nil and "moved" or "created").."!", playerSource)
        theZone = tempCol
    end
end
addCommandHandler("set_zone", setZone, false, false)

See Also