Jump to content

How to use client events on player's join


Recommended Posts

Hello everyone!

I have some server code:

function onJoinLogin()
    local isClientReady = getElementData(source, "client.isready")
    local timer = true
    while not isClientReady or not timer do
        if timer then
            outputChatBox("Loading on client, wait...", source)
            isClientReady = getElementData(source, "client.isready")
            if not isClientReady then
                timer = false
                setTimer(function ()
                timer = true
                end, 100, 1)
            end
        end
    end
    triggerClientEvent("setCameraForSpawn", source)
    triggerClientEvent("toggleHUD", source)
    setElementData(source, "player.isLogined", false)
    setElementData(source, "player.isActiveChar", false)
    outputChatBox("Login or reg, please", source)
end
addEventHandler("onPlayerJoin", root, onJoinLogin)

And client code:

local logoPath = "logo2.png"
local screenWidth, screenHeight = guiGetScreenSize()
local logoPNG = dxCreateTexture(logoPath)

function createLogo()
    if logoPNG then
        dxDrawImage(screenWidth/10 * 9, screenHeight/6 * 5, 128, 128, logoPNG)
    else
        outputDebugString("Err logo!")
    end
end
addEventHandler("onClientRender", root, createLogo)

addEvent("toggleHUD", true)
addEventHandler("toggleHUD", root, function ()
    outputDebugString('Toggle HUD!')
    setPlayerHudComponentVisible("radar", not isPlayerHudComponentVisible("radar"))
end)

addEvent("setCameraForSpawn", true)
addEventHandler("setCameraForSpawn", root, function ()
    outputDebugString('Set camera!')
    fadeCamera(true)
    setCameraMatrix(-2656.638, 373.576, 35.809, -2706.585, 376.137, 4.969)
end)

addEventHandler("onClientResourceStart", root, function ()
    setElementData(root, "client.isready", true)
end    
)

The problem is that client code workes on all players on server, when someone new joining. I tried to change "root" in client code on "localPlayer", "getLocalPlayer()", added "onClientResourceStart" and "set/getElementData()", but it's still don't work. And plus for first time for player camera don't work too, just a black screen. How can I fix it?

p.s. New in Lua, but C++ programmer, I think I know some things about programming, but not here :(

readed this topics:
https://forum.multitheftauto.com/topic/118393-event-is-not-added-a-clientside-mta/

https://forum.multitheftauto.com/topic/88306-event-is-not-added-to-clientside/

https://forum.multitheftauto.com/topic/63280-server-triggered-clientside-event-but-event-is-not-added/

 

Link to comment
14 hours ago, IIYAMA said:

I recommend to replace most of your serverside code with (see example):

https://wiki.multitheftauto.com/wiki/OnPlayerResourceStart

YES! THANKS VERY MUCH IT WORKS!

 

Can I ask some questions else?
1. Can I'll write to your's PM for some questions in future? Looks like you are advanced user and can help with many problems 😅

2. Nothing go bad if I use "root" in client events? Because on wiki was written that root should not use, only in specific moments.

 

15 hours ago, fxl said:


https://wiki.multitheftauto.com/wiki/TriggerClientEvent


this one should work:

    triggerClientEvent(source, "setCameraForSpawn", source)
    triggerClientEvent(source, "toggleHUD", source)

I tried this too, but without new event it didn't work. And still thank you, that was very important message for best understanding of client triggers!

  • Like 1
Link to comment
  • Moderators

 

12 hours ago, DarkStalker30 said:

1. Can I'll write to your's PM for some questions in future? Looks like you are advanced user and can help with many problems 😅

You can, but it is sometimes better to ask your main questions here. And if nobody knows the answer, you can always send me a message to a specific topic. There are more people out there that could have provided this answer.

 

12 hours ago, DarkStalker30 said:

2. Nothing go bad if I use "root" in client events? Because on wiki was written that root should not use, only in specific moments.

The variable root is predefined variable, containing the root element pointer.

https://wiki.multitheftauto.com/wiki/Element_tree

Which is the great parent of parents ^^. Take a look at the schematic (on the wiki).

Tre.png

 

Events work with a kind parent to child relation. Triggering an event on an element with a lot of children can increase CPU usage significant (execution time more or less 0.5% for each child extra, based on a sample created on my specific system).

  • So it is to reduce the CPU usage.
  • But also making sure you do not clash with other resources. Because an event is a global interaction, a resource can trigger an eventHandler for another resource.

It is recommended to trigger an event with the resourceRoot, a specific element of that resource or a player.

 

The resourceRoot is a predefined variable that is the root element for a specific resource (See purple nodes in the schematic). 

  • There is a resourceRoot for every resource and each of them is unique.
  • [For each resource]: The resourceRoot of clientside is linked to the resourceRoot of serverside. They are considered more or less the same (In technical terms that is not the case, but you will not notice it).

The benefit of using resourceRoot is that the eventHandlers that listen to resourceRoot, will only trigger for the same resourceRoot or (by default) their children.

 

The following example shows how a resource communicates from serverside to clientside. And the communication is scoped with the resource itself.

  • This does not mean that other resources can't listen to the "onGreeting" event, but the eventHandler will not trigger for other resources that run the exact same code.
-- Server

-- (playerSource is not yet defined)
triggerClientEvent ( playerSource, "onGreeting", resourceRoot )

 

-- Client
addEvent( "onGreeting", true )
addEventHandler( "onGreeting", resourceRoot, function () end ) -- Default: resourceRoot + children

addEvent( "onGreeting", true )
addEventHandler( "onGreeting", resourceRoot, function () end, true ) -- resourceRoot + children

addEvent( "onGreeting", true )
addEventHandler( "onGreeting", resourceRoot, function () end, false ) -- only resourceRoot

 

See also:

local thisElement = resourceRoot 

addEvent( "onGreeting", true )
addEventHandler( "onGreeting", thisElement, function () end ) -- thisElement should have the same value >

-- Triggering an event on the same side(server or client)
triggerEvent("onGreeting", thisElement) -- > as thisElement or a child of it.

 

If using root on an eventHandler, it will trigger for all elements, since this is the great parent of parents... All resources will be able to trigger this event, no matter which element they use.

addEvent( "onGreeting", true )
addEventHandler( "onGreeting", root, function () end )

 

Is it possible to trigger the following eventHandler from another resource?

addEvent( "onGreeting", true )
addEventHandler( "onGreeting", resourceRoot, function () end )

Technically yes, elements from another resource are all part of the element tree. So as long as you use the right element, it is possible.

 

 

(normally I do not explain this much)

 

  • Like 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...