Jump to content

Is there any way to do this?


Dzsozi (h03)

Recommended Posts

Hello community! Today I was searching MTA Wiki to find answer to my question which is: Is this possible to synchronize the head turning effect between players? I've visited 'setPedLookAt' wiki page, and it tells me this:

This example makes the local player look at where the camera points at. If you want to sync this effect with other players you can use triggerLatentServerEvent and triggerLatentClientEvent functions.

So I've visited triggerLatentServer and ClientEvent functions' wiki page, but there's no example and I don't know how could I use them and sync setPedLookAt between players. Could someone help me in this? How should I set other players' head look at the position where they are looking. Just like in SA-MP. I hope you understand me and someone could help me! Thank you!

P.S.: SA-MP sucks, but it's the only example that I can say to be understandable. lol

Link to comment
  • MTA Team

Not tested.

Edit: It might be better to update it only for those players, which are streamed in.

CLIENTSIDE:

  
-- To avoid flooding the server with massive head rotation updates 
-- you shouldn't drop it below 50 milliseconds 
local UPDATE_MS = 100 
  
-- Systemtick, when the last update occured 
local g_nLastUpdate = 0 
  
-- Last rotation sent to other players 
local g_fLastX, g_fLastY, g_fLastZ = 0, 0, 0 
  
-- Screen resolution of the player 
local g_nScreenWidth, g_nScreenHeight = guiGetScreenSize() 
  
function updateLookAt() 
    -- Get the current tick and world position 
    local tick = getTickCount() 
    local fX, fY, fZ = getWorldFromScreenPosition(g_nScreenWidth / 2, g_nScreenHeight / 2, 10) 
  
    -- Let our player look at that point 
    setPedLookAt(localPlayer, fX, fY, fZ, -1, 0) 
  
    -- Update if the last update has passed the min. time 
    if tick >= (g_nLastUpdate + UPDATE_MS) then 
        g_nLastUpdate = tick 
  
        -- Only update the head rotation when the actually changed it 
        -- (to avoid flood from AFK players) 
        if fX ~= g_fLastX or fY ~= g_fLastY or fZ ~= g_fLastZ then 
            g_fLastX, g_fLastY, g_fLastZ = fX, fY, fZ 
            triggerLatentServerEvent(".updateHeadRotation", resourceRoot, localPlayer, fX, fY, fZ) 
        end 
    end 
end 
addEventHandler("onClientPreRender", root, updateLookAt) 
  
addEvent(".updateHeadRotation", true) 
addEventHandler(".updateHeadRotation", resourceRoot, 
    function (player, x, y, z) 
        -- Prevent applying invalid data 
        if not isElement(player) or type(x) ~= "number" or type(y) ~= "number" or type(z) ~= "number" then 
            return 
        end 
  
        setPedLookAt(player, x, y, z, -1, 0) 
    end 
) 
  

SERVERSIDE:

  
addEvent(".updateHeadRotation", true) 
addEventHandler(".updateHeadRotation", resourceRoot, 
    function (player, x, y, z) 
        -- Prevent sending invalid data 
        if not isElement(player) or type(x) ~= "number" or type(y) ~= "number" or type(z) ~= "number" then 
            return 
        end 
  
        -- Get every player on this server 
        local players = getElementsByType("player") 
  
        -- Abort if there is only 1 player 
        if #players == 1 then 
            return 
        end 
  
        -- Cache the original player's dimension 
        local dim = getElementDimension(player) 
  
        -- Loop through each player 
        for i = 1, #players do 
            -- Ignore our player, who sends his head rotation 
            if player ~= players[i] then 
                -- Check if the player is in the right dimension 
                if getElementDimension(players[i]) == dim then 
                    -- Send the head rotation to that player 
                    triggerLatentClientEvent(players[i], ".updateHeadRotation", resourceRoot, player, x, y, z) 
                end 
            end 
        end 
    end 
) 
  

Link to comment
  • MTA Team

Going to test the code on my own.

Anyway, I can't tell right now if it's mine code or the function itself.

setPedLookAt: Avoid calling setPedLookAt every frame as this can cause bugs like being invincible to burning.

Edit: The function is broken and the best result was that the other player's head was switching between left and straight. Changing the player skin broke the function for your own player.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...