Jump to content

onClientRender


Z4Zy

Recommended Posts

So, I just created below code.

Client Side Code :-

local screenW, screenH = guiGetScreenSize()

local marker = createMarker( 0, 0, 2, "cylinder", 2, 255, 255, 0, 255 )

function drawAlert()
    dxDrawText("You Hit The Marker", (screenW - 340) / 2, (screenH - 53) / 2, ((screenW - 340) / 2) + 340, ( (screenH - 53) / 2) + 53, tocolor(255, 0, 0, 255), 3.00, "default", "left", "top", false, false, false, false, false)
    dxDrawLine(296, 358, 719, 358, tocolor(32, 222, 55, 255), 1, false)
    dxDrawLine(296, 411, 719, 411, tocolor(32, 222, 55, 255), 1, false)
end

function onMarkerHit()
    addEventHandler("onClientRender", root, drawAlert)
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

function onMarkerLeave()
    removeEventHandler("onClientRender", root, drawAlert)
end
addEventHandler("onClientMarkerLeave", marker, onMarkerLeave)

 

        The problem is that, since onClientRender event use root,  when one player hit the marker, alert [ drawAlert function ] will draw on all players' screen !

How to draw that alert [ drawAlert function ] only on marker hit player's screen ??

Link to comment
function onMarkerHit(hitElement)
    if hitElement and getElementType(hitElement) == "player" and hitElement == localPlayer then
        addEventHandler("onClientRender", root, drawAlert)
    end
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

function onMarkerLeave(hitElement)
    if hitElement == localPlayer then
        removeEventHandler("onClientRender", root, drawAlert)
    end
end
addEventHandler("onClientMarkerLeave", marker, onMarkerLeave)

 

  • Thanks 1
Link to comment
9 minutes ago, #َxLysandeR said:

function onMarkerHit(hitElement)
    if hitElement and getElementType(hitElement) == "player" and hitElement == localPlayer then
        addEventHandler("onClientRender", root, drawAlert)
    end
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

function onMarkerLeave(hitElement)
    if hitElement == localPlayer then
        removeEventHandler("onClientRender", root, drawAlert)
    end
end
addEventHandler("onClientMarkerLeave", marker, onMarkerLeave)

 

Are you sure that this trick will work ? because if the hitElement was the localPlayer, drawAlert function will render to root element means all players ?!?!

Link to comment
7 minutes ago, DeadthStrock said:

Are you sure that this trick will work ? because if the hitElement was the localPlayer, drawAlert function will render to root element means all players ?!?!

it's not a trick ..

+ yes iam sure 

+ seems that you didn't understand addEventHandler right so i suggest  to read this page one time again

https://wiki.multitheftauto.com/wiki/AddEventHandler

Edited by #َxLysandeR
Link to comment
  • 4 weeks later...
On 23/08/2018 at 13:50, DeadthStrock said:

Are you sure that this trick will work ? because if the hitElement was the localPlayer, drawAlert function will render to root element means all players ?!?!

No it doesn't. It's on the client side, meaning that root is the client's root element.

Link to comment
49 minutes ago, MIKI785 said:

No it doesn't. It's on the client side, meaning that root is the client's root element.

 

And this is also client side :-

local screenW, screenH = guiGetScreenSize()

local marker = createMarker( 0, 0, 2, "cylinder", 2, 255, 255, 0, 255 )

function drawAlert()
    dxDrawText("You Hit The Marker", (screenW - 340) / 2, (screenH - 53) / 2, ((screenW - 340) / 2) + 340, ( (screenH - 53) / 2) + 53, tocolor(255, 0, 0, 255), 3.00, "default", "left", "top", false, false, false, false, false)
    dxDrawLine(296, 358, 719, 358, tocolor(32, 222, 55, 255), 1, false)
    dxDrawLine(296, 411, 719, 411, tocolor(32, 222, 55, 255), 1, false)
end

function onMarkerHit()
    addEventHandler("onClientRender", root, drawAlert)
end
addEventHandler("onClientMarkerHit", marker, onMarkerHit)

function onMarkerLeave()
    removeEventHandler("onClientRender", root, drawAlert)
end
addEventHandler("onClientMarkerLeave", marker, onMarkerLeave)

Why is client's root element changed in this code that leads render to everyone ?

Link to comment
1 hour ago, MIKI785 said:

Because onClientMarkerHit is triggered for all players and you're not checking if it's the localPlayer.

 

so .. when checking that the "hitelement" is localplayer, "onClientRender" event gets the root element as localPlayer isn't it ?

Link to comment

You still don't get it.. root element never changes. Root is root.. all other elements are underneath this element in the element tree (see here: https://wiki.multitheftauto.com/wiki/Element_tree), hence the name root.

What happens here is that onClientMarkerHit gets triggered on all clients, doesn't matter which player hit the marker. If you check if the hitelement is the localPlayer then the onClientRender event will be handled only for the player that hit the marker.

  • Thanks 1
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...