SetCameraTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server function}}
{{Server client function}}
This function allows you to set a player's camera to follow a specific [[element]]. This could be another player, a vehicle or an object depending on what the camera mode expects.
This function allows you to set a player's camera to follow a specific [[element]]. This could be another player, a vehicle or an object depending on what the camera mode expects.
==Syntax==  
==Syntax==
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setCameraTarget ( player thePlayer, element target )
bool setCameraTarget ( player thePlayer, element target )
</syntaxhighlight>  
</syntaxhighlight>


===Required Arguments===  
===Required Arguments===  
*'''thePlayer:''' The player whose camera you wish to modify.
*'''thePlayer:''' The player whose camera you wish to modify.
*'''target:''' The element that you want the camera to follow.
*'''target:''' The element that you want the camera to follow.
</section>
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
bool setCameraTarget ( element target )
</syntaxhighlight>
===Required Arguments===
*'''target:''' The element that you want the local camera to follow.
</section>


===Returns===
===Returns===
Returns ''true'' if the function was successful, ''false'' otherwise.
Returns ''true'' if the function was successful, ''false'' otherwise.


==Example==  
==Example==
This is an example of how one could implement a spectator function. Using the left and right arrow keys you can view other players. Note that this code isn't complete as it doesn't take into account joining or quitting players.
<section class="client" name="Client script" show="true">
<syntaxhighlight lang="lua">
g_Players = getElementsByType("player")        -- get a list of all players in the server
for i,aPlayer in ipairs(g_Players) do          -- find out what index the local player has in the list
    if aPlayer == getLocalPlayer() then
        g_CurrentSpectated = i
        break
    end
end
 
function spectatePrevious()                    -- decrement the spectate index and spectate the corresponding player
    if g_CurrentSpectated == 1 then
        g_CurrentSpectated = #g_Players
    else
        g_CurrentSpectated = g_CurrentSpectated - 1
    end
    setCameraTarget(g_Players[g_CurrentSpectated])
end
 
function spectateNext()                        -- increment the spectate index and spectate the corresponding player
    if g_CurrentSpectated == #g_Players then
        g_CurrentSpectated = 1
    else
        g_CurrentSpectated = g_CurrentSpectated + 1
    end
    setCameraTarget(g_Players[g_CurrentSpectated])
end
 
-- Bind above functions to arrow keys
bindKey("arrow_l", "down", spectatePrevious)
bindKey("arrow_r", "down", spectateNext)
</syntaxhighlight>
</section>
 


==See Also==
==See Also==

Revision as of 12:34, 30 December 2007

This function allows you to set a player's camera to follow a specific element. This could be another player, a vehicle or an object depending on what the camera mode expects.

Syntax

Click to collapse [-]
Server
bool setCameraTarget ( player thePlayer, element target )

Required Arguments

  • thePlayer: The player whose camera you wish to modify.
  • target: The element that you want the camera to follow.
Click to collapse [-]
Client
bool setCameraTarget ( element target )

Required Arguments

  • target: The element that you want the local camera to follow.

Returns

Returns true if the function was successful, false otherwise.

Example

This is an example of how one could implement a spectator function. Using the left and right arrow keys you can view other players. Note that this code isn't complete as it doesn't take into account joining or quitting players.

Click to collapse [-]
Client script
g_Players = getElementsByType("player")        -- get a list of all players in the server
for i,aPlayer in ipairs(g_Players) do          -- find out what index the local player has in the list
    if aPlayer == getLocalPlayer() then
        g_CurrentSpectated = i
        break
    end
end

function spectatePrevious()                    -- decrement the spectate index and spectate the corresponding player
     if g_CurrentSpectated == 1 then
         g_CurrentSpectated = #g_Players
     else
         g_CurrentSpectated = g_CurrentSpectated - 1
     end
    setCameraTarget(g_Players[g_CurrentSpectated])
end

function spectateNext()                        -- increment the spectate index and spectate the corresponding player
     if g_CurrentSpectated == #g_Players then
         g_CurrentSpectated = 1
     else
         g_CurrentSpectated = g_CurrentSpectated + 1
     end
    setCameraTarget(g_Players[g_CurrentSpectated])
end

-- Bind above functions to arrow keys
bindKey("arrow_l", "down", spectatePrevious)
bindKey("arrow_r", "down", spectateNext)


See Also