CreateSearchLight: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Added information about searchlight limit)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Client function}}
{{Client function}}
{{New feature/item|3.0160|1.6|7675|This function creates a [[Element/Searchlight|searchlight]]. A [[Element/Searchlight|searchlight]] is a spotlight which looks like the one available in the Police Maverick.}}
{{New feature/item|3.0152|1.5.2||This function creates a [[Element/Searchlight|searchlight]]. A [[Element/Searchlight|searchlight]] is a spotlight which looks like the one available in the Police Maverick.}}
{{Tip|''You should only use this function when you are sure that the searchlight will point upwards or downwards''. Using them horizontally or almost horizontally will generate visual artifacts in the searchlight.}}


==Syntax==  
==Syntax==  
Line 7: Line 8:
searchlight createSearchLight ( float startX, float startY, float startZ, float endX, float endY, float endZ, float startRadius, float endRadius [, bool renderSpot = true ] )
searchlight createSearchLight ( float startX, float startY, float startZ, float endX, float endY, float endZ, float startRadius, float endRadius [, bool renderSpot = true ] )
</syntaxhighlight>
</syntaxhighlight>
{{OOP||[[Element/Searchlight|SearchLight]].create}}
{{OOP||[[Element/Searchlight|SearchLight]]}}


===Required Arguments===
===Required Arguments===
Line 24: Line 25:


===Returns===
===Returns===
If every argument is correct, this function returns a [[Element/Searchlight|searchlight element]]. Otherwise, it returns ''false''.
If every argument is correct and the limit of 1000 searchlights has not been reached, this function returns a [[Element/Searchlight|searchlight element]]. Otherwise, it returns ''false''.


==Example==
==Example==
This example allows players to wear a helmet lantern, which can be toggled on or off by pressing O or using ''/togglelantern''. It uses createSearchLight to create the illumination effect.
This example allows players to wear a helmet lantern, which can be toggled on or off by pressing O or using ''/togglelantern''. It uses createSearchLight to create the illumination effect.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local helmetLanterns
local helmetLantern


local function updateHelmetLantern()
local function updateHelmetLantern()
    -- We need to do it this way because there isn't a setSearchLightStart/EndPosition for now
     -- Calculate light properties
     -- Calculate light properties
     local helmetPos, playerMatrix = Vector3(getPedBonePosition(localPlayer, 6)), getElementMatrix(localPlayer)
     local helmetPos, playerMatrix = Vector3(getPedBonePosition(localPlayer, 6)), getElementMatrix(localPlayer)
     local targetPos = Vector3(playerMatrix[4][1] + playerMatrix[2][1] * 3, playerMatrix[4][2] + playerMatrix[2][2] * 3, playerMatrix[4][3] + playerMatrix[2][3] * 3)
     local targetPos = Vector3(playerMatrix[4][1] + playerMatrix[2][1] * 3, playerMatrix[4][2] + playerMatrix[2][2] * 3, playerMatrix[4][3] + playerMatrix[2][3] * 3)
     -- Finally, create the updated searchlight
     -- If the searchlight we use for the effect doesn't exist, create it
     helmetLanterns[#helmetLanterns + 1] = createSearchLight(helmetPos, targetPos, 0, 15)
    -- If it is already created, then simply update its start and end positions
     -- We need to keep two searchlights, so they have time to render visible and look nice
     if not helmetLantern then
    if #helmetLanterns > 2 then
        helmetLantern = createSearchLight(helmetPos, targetPos, 0, 15)
         destroyElement(helmetLanterns[1])
     else
         table.remove(helmetLanterns, 1)
         setSearchLightStartPosition(helmetLantern, helmetPos)
         setSearchLightEndPosition(helmetLantern, targetPos)
     end
     end
end
end


local function manageHelmetLantern()
local function manageHelmetLantern()
     local helmetLanternOff = not helmetLanterns
     local helmetLanternOff = not helmetLantern
     playSoundFrontEnd(helmetLanternOff and 37 or 38)
     playSoundFrontEnd(helmetLanternOff and 37 or 38)
     if helmetLanternOff then
     if helmetLanternOff then
         -- Let updateHelmetLantern take care of creating and updating the effect
         -- Let updateHelmetLantern take care of creating and updating the effect
        helmetLanterns = {}
         addEventHandler("onClientPreRender", root, updateHelmetLantern)
         addEventHandler("onClientPreRender", root, updateHelmetLantern)
     else
     else
         -- Stop updateHelmetLantern doing its job and clear variables
         -- Stop updateHelmetLantern doing its job and clear variables
         removeEventHandler("onClientPreRender", root, updateHelmetLantern)
         removeEventHandler("onClientPreRender", root, updateHelmetLantern)
         for _, helmetLantern in ipairs(helmetLanterns) do
         destroyElement(helmetLantern)
            destroyElement(helmetLantern)
         helmetLantern = nil
         end
        helmetLanterns = nil
     end
     end
end
end

Latest revision as of 09:39, 4 June 2016

This function creates a searchlight. A searchlight is a spotlight which looks like the one available in the Police Maverick.

[[{{{image}}}|link=|]] Tip: You should only use this function when you are sure that the searchlight will point upwards or downwards. Using them horizontally or almost horizontally will generate visual artifacts in the searchlight.

Syntax

searchlight createSearchLight ( float startX, float startY, float startZ, float endX, float endY, float endZ, float startRadius, float endRadius [, bool renderSpot = true ] )

OOP Syntax Help! I don't understand this!

Method: SearchLight(...)


Required Arguments

  • startX: the X coordinate where the searchlight light cone will start.
  • startY: the Y coordinate where the searchlight light cone will start.
  • startZ: the Z coordinate where the searchlight light cone will start.
  • endX: the X coordinate of the direction where the searchlight will point to.
  • endY: the Y coordinate of the direction where the searchlight will point to.
  • endZ: the Z coordinate of the direction where the searchlight will point to.
  • startRadius: the radius of the searchlight's light cone in its beginning.
  • endRadius: the radius of the searchlight's light cone in its end.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • renderSpot: if true, the searchlight will lighten the surface where it ends.

Returns

If every argument is correct and the limit of 1000 searchlights has not been reached, this function returns a searchlight element. Otherwise, it returns false.

Example

This example allows players to wear a helmet lantern, which can be toggled on or off by pressing O or using /togglelantern. It uses createSearchLight to create the illumination effect.

local helmetLantern

local function updateHelmetLantern()
    -- Calculate light properties
    local helmetPos, playerMatrix = Vector3(getPedBonePosition(localPlayer, 6)), getElementMatrix(localPlayer)
    local targetPos = Vector3(playerMatrix[4][1] + playerMatrix[2][1] * 3, playerMatrix[4][2] + playerMatrix[2][2] * 3, playerMatrix[4][3] + playerMatrix[2][3] * 3)
    -- If the searchlight we use for the effect doesn't exist, create it
    -- If it is already created, then simply update its start and end positions
    if not helmetLantern then
        helmetLantern = createSearchLight(helmetPos, targetPos, 0, 15)
    else
        setSearchLightStartPosition(helmetLantern, helmetPos)
        setSearchLightEndPosition(helmetLantern, targetPos)
    end
end

local function manageHelmetLantern()
    local helmetLanternOff = not helmetLantern
    playSoundFrontEnd(helmetLanternOff and 37 or 38)
    if helmetLanternOff then
        -- Let updateHelmetLantern take care of creating and updating the effect
        addEventHandler("onClientPreRender", root, updateHelmetLantern)
    else
        -- Stop updateHelmetLantern doing its job and clear variables
        removeEventHandler("onClientPreRender", root, updateHelmetLantern)
        destroyElement(helmetLantern)
        helmetLantern = nil
    end
end

-- Allow the player to turn the helmet lantern or on off by using /togglelantern or pressing O
addCommandHandler("togglelantern", manageHelmetLantern)
bindKey("o", "down", manageHelmetLantern)

See also