Sign in to follow this  
DeadthStrock

onClientRender

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 ??

Share this post


Link to post
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

Share this post


Link to post
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 ?!?!

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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 ?

Share this post


Link to post

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

Share this post


Link to post
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 ?

Share this post


Link to post

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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.