IsCursorShowing: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Adding new and more lifelike examples, clarifying the grammar, simplifying the page for beginners.)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
This function is used to determine whether or not a player's cursor is showing.
{{Note|This function only handles the cursor state set by the [[showCursor]] function, ignoring it if the console, chatbox, or menu is open.}}
{{Note|This retrieves the cursor state that has been set using [[showCursor]], and thus doesn't take into account the cursor shown while the chatbox, menu or console are open. Also, keep in mind that while the client is aware of cursor states set from the server, the server doesn't know about cursor states set from the client.}}
{{Note|If you use this function on the server side, keep in mind that it only detects the [[showCursor]] function executed on the server side and does not detect the function executed on the client side.}}
 
==Purpose==
This function determines the state of a player's cursor.
 
==Syntax==  
==Syntax==  
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool isCursorShowing ( player thePlayer )
bool isCursorShowing ( player playerElement )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''thePlayer:''' The [[player]] you want to get cursor state of.
*'''playerElement:''' The [[player]] from whom we want to retrieve the cursor state.
</section>
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
bool isCursorShowing ( )
</syntaxhighlight>
</section>


===Returns===
===Returns===
Returns ''true'' if the player's cursor is showing, ''false'' if it isn't or if invalid parameters were passed.
Returns ''true'' if the player's cursor is visible, and ''false'' if it is not.
 
==Example==
<section name="Server (Simple)" class="server" show="true">
This example creates a function to set the state of the player's cursor using the [[showCursor]] function.
<syntaxhighlight lang="lua">
function toggleCursor(playerElement)
    if playerElement and isElement(playerElement) then -- Check whether the given element exists
        local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
        local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
 
        showCursor(playerElement, cursorStateOpposite) -- Setting the new cursor state
    end
end
</syntaxhighlight>
</section>
</section>


<section name="Client" class="client" show="true">
<section name="Server (Complex)" class="server" show="true">
This example creates a function that gets the state of the player's cursor and outputs it to the chatbox using the [[outputChatBox]] function.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool isCursorShowing ( )
function outputCursor(playerElement)
    if playerElement and isElement(playerElement) then -- Check whether the given element exists
        local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
        local cursorStateText = cursorState and "visible" or "hidden" -- Calculate the context from the boolean variable.
 
        outputChatBox("Your cursor is " .. cursorStateText .. ".", playerElement) -- Output the text in the chatbox according to the cursor state.
    end
end
</syntaxhighlight>
</syntaxhighlight>
===Returns===
Returns ''true'' if the player's cursor is showing, ''false'' if it isn't.
</section>
</section>


==Example==
<section name="Client (Simple)" class="client" show="true">
<section name="Example" class="server" show="true">
This example creates a function to set the state of the player's cursor using the [[showCursor]] function.
This serverside function toggles a player's cursor state.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function toggleCursor ( thePlayer )
function toggleCursor()
local currentState = isCursorShowing ( thePlayer ) -- get the current cursor state as a boolean
    local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
local oppositeState = not currentState              -- get the new state as its logical opposite
    local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
showCursor ( thePlayer, oppositeState )             -- set it as the new cursor state
 
    showCursor(cursorStateOpposite) -- Setting the new cursor state
end
</syntaxhighlight>
If you are already advanced in scripting, using this code is recommended, as it is much more compact:
<syntaxhighlight lang="lua">
function toggleCursor()
    showCursor(not isCursorShowing())
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
<section name="Clientside Example" class="client" show="true">
 
With little of tweaking this can also be used clientside
<section name="Client (Complex)" class="client" show="true">
This example creates a function that allows the player to change the state of the cursor using the [[showCursor]] and [[bindKey]] functions.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function toggleCursor ()
function toggleCursor()
        local currentState = isCursorShowing () -- get the current cursor state as a boolean
    local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
        local oppositeState = not currentState              -- get the new state as its logical opposite
    local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
        showCursor ( oppositeState )             -- set it as the new cursor state
 
    showCursor(cursorStateOpposite) -- Setting the new cursor state
end
end
bindKey("m", "down", toggleCursor) -- Assigning our toggleCursor function to the 'm' key press.
</syntaxhighlight>
</syntaxhighlight>
And a more compact version
If you are already advanced in scripting, using this code is recommended, as it is much more compact:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
 
bindKey("m", "down",
bindKey ("b", "down",                           -- binds B key to toggle cursor state
    function()
        function()
        showCursor(not isCursorShowing())
                showCursor( not isCursorShowing() )
    end
        end)
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 19:47, 24 October 2023

[[{{{image}}}|link=|]] Note: This function only handles the cursor state set by the showCursor function, ignoring it if the console, chatbox, or menu is open.
[[{{{image}}}|link=|]] Note: If you use this function on the server side, keep in mind that it only detects the showCursor function executed on the server side and does not detect the function executed on the client side.

Purpose

This function determines the state of a player's cursor.

Syntax

Click to collapse [-]
Server
bool isCursorShowing ( player playerElement )

Required Arguments

  • playerElement: The player from whom we want to retrieve the cursor state.
Click to collapse [-]
Client
bool isCursorShowing ( )

Returns

Returns true if the player's cursor is visible, and false if it is not.

Example

Click to collapse [-]
Server (Simple)

This example creates a function to set the state of the player's cursor using the showCursor function.

function toggleCursor(playerElement)
    if playerElement and isElement(playerElement) then -- Check whether the given element exists
        local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
        local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state

        showCursor(playerElement, cursorStateOpposite) -- Setting the new cursor state
    end
end
Click to collapse [-]
Server (Complex)

This example creates a function that gets the state of the player's cursor and outputs it to the chatbox using the outputChatBox function.

function outputCursor(playerElement)
    if playerElement and isElement(playerElement) then -- Check whether the given element exists
        local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
        local cursorStateText = cursorState and "visible" or "hidden" -- Calculate the context from the boolean variable.

        outputChatBox("Your cursor is " .. cursorStateText .. ".", playerElement) -- Output the text in the chatbox according to the cursor state.
    end
end
Click to collapse [-]
Client (Simple)

This example creates a function to set the state of the player's cursor using the showCursor function.

function toggleCursor()
    local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
    local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state

    showCursor(cursorStateOpposite) -- Setting the new cursor state
end

If you are already advanced in scripting, using this code is recommended, as it is much more compact:

function toggleCursor()
    showCursor(not isCursorShowing())
end
Click to collapse [-]
Client (Complex)

This example creates a function that allows the player to change the state of the cursor using the showCursor and bindKey functions.

function toggleCursor()
    local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
    local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state

    showCursor(cursorStateOpposite) -- Setting the new cursor state
end

bindKey("m", "down", toggleCursor) -- Assigning our toggleCursor function to the 'm' key press.

If you are already advanced in scripting, using this code is recommended, as it is much more compact:

bindKey("m", "down",
    function()
        showCursor(not isCursorShowing())
    end
)

See Also