GetCursorPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
 
(21 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{Client function}}
{{Client function}}
__NOTOC__
__NOTOC__
This function gets the current position of the mouse cursor.
This function gets the current position of the mouse cursor. Note that for performance reasons, the world position returned is always 300 units away. If you want the exact world point (similar to [[onClientClick]]), use [[processLineOfSight]] between the camera position and the worldX/Y/Z result of this function. (See example below)


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int int float float float getCursorPosition ( )
float, float, float, float, float getCursorPosition ( )
</syntaxhighlight>
</syntaxhighlight>


===Required Arguments===  
===Returns===
''None''
Returns 5 values: ''cursorX'', ''cursorY'', ''worldX'', ''worldY'', ''worldZ''. The first two values are the 2D '''relative''' screen coordinates of the cursor. The 3 values that follow are the 3D world map coordinates that the cursor points at. If the cursor isn't showing, returns ''false'' as the first value.


===Returns===
===Issues===
Returns 5 values: cursorX, cursorY, worldX, worldY, worldZ. The first two values are the 2D screen coordinates of the cursor in pixels, relative to the upper left corner. The 3 values that follow are the 3D world map coordinates that the cursor points at.
{{Issues|
{{Issue|1166|getCursorPosition() returns false even when cursor is showing due to opened console}}
}}


==Example==
==Example==
This example prints your cursors current world coordinates and relative screen coordinates to chatbox after typing ''cursorpos''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function cursorInfo()
function cursorInfo()
screenx, screeny, worldx, worldy, worldz = getCursorPosition()
  if isCursorShowing() then -- if the cursor is showing
outputChatBox("Cursor screen position: X=" .. screenx .. " Y=" .. screeny)
      local screenx, screeny, worldx, worldy, worldz = getCursorPosition()
outputChatBox("Cursor world position: X=" .. worldx .. " Y=" .. worldy .. " Z="  .. worldz)
 
      outputChatBox( string.format( "Cursor screen position (relative): X=%.4f Y=%.4f", screenx, screeny ) ) -- make the accuracy of floats 4 decimals
      outputChatBox( string.format( "Cursor world position: X=%.4f Y=%.4f Z=%.4f", worldx, worldy, worldz ) ) -- make the accuracy of floats 4 decimals accurate
  else
      outputChatBox( "Your cursor is not showing." )
  end
end
end
addCommandHandler( "cursorpos", cursorInfo )
</syntaxhighlight>
This (untested) example uses [[processLineOfSight]] to calculate the exact world location:
'''Warning, using the script down there will cause high CPU usage.'''
<syntaxhighlight lang="lua">
addEventHandler( "onClientRender", root,
    function()
        if isCursorShowing() then
            local screenx, screeny, worldx, worldy, worldz = getCursorPosition()
            local px, py, pz = getCameraMatrix()
            local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, worldx, worldy, worldz )
            if hit then
                dxDrawText( "Cursor at " .. x .. " " .. y .. " " ..  z, 200, 200 )
                if elementHit then
                    dxDrawText( "Hit element " .. getElementType(elementHit), 200, 220 )
                end
            end
        end
    end
)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Client_input_functions}}
{{Cursor_functions|client}}
[[Category:Needs_Example]]
 
[[hu:getCursorPosition]]

Latest revision as of 15:11, 6 November 2024

This function gets the current position of the mouse cursor. Note that for performance reasons, the world position returned is always 300 units away. If you want the exact world point (similar to onClientClick), use processLineOfSight between the camera position and the worldX/Y/Z result of this function. (See example below)

Syntax

float, float, float, float, float getCursorPosition ( )

Returns

Returns 5 values: cursorX, cursorY, worldX, worldY, worldZ. The first two values are the 2D relative screen coordinates of the cursor. The 3 values that follow are the 3D world map coordinates that the cursor points at. If the cursor isn't showing, returns false as the first value.

Issues

Issue ID Description
#1166 getCursorPosition() returns false even when cursor is showing due to opened console

Example

This example prints your cursors current world coordinates and relative screen coordinates to chatbox after typing cursorpos.

function cursorInfo()
   if isCursorShowing() then -- if the cursor is showing
      local screenx, screeny, worldx, worldy, worldz = getCursorPosition()

      outputChatBox( string.format( "Cursor screen position (relative): X=%.4f Y=%.4f", screenx, screeny ) ) -- make the accuracy of floats 4 decimals
      outputChatBox( string.format( "Cursor world position: X=%.4f Y=%.4f Z=%.4f", worldx, worldy, worldz ) ) -- make the accuracy of floats 4 decimals accurate
   else
      outputChatBox( "Your cursor is not showing." )
   end
end
addCommandHandler( "cursorpos", cursorInfo )


This (untested) example uses processLineOfSight to calculate the exact world location: Warning, using the script down there will cause high CPU usage.

addEventHandler( "onClientRender", root,
    function()
        if isCursorShowing() then
            local screenx, screeny, worldx, worldy, worldz = getCursorPosition()
            local px, py, pz = getCameraMatrix()
            local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, worldx, worldy, worldz )

            if hit then
                dxDrawText( "Cursor at " .. x .. " " .. y .. " " ..  z, 200, 200 )
                if elementHit then
                    dxDrawText( "Hit element " .. getElementType(elementHit), 200, 220 )
                end
            end
        end
    end
)

See Also