Sign in to follow this  
AshFTW

[HELP] root, resourceRoot, source, etc...

Recommended Posts

Can someone explain me, how to use variables like: "root", "resourceRoot", "source", "localPlayer" at client/server side?

I just cant feel difference between these elements...

For expample, this script will be not work:

--server.lua 
addEventHandler ("onPlayerLogin", resourceRoot, function () 
    triggerClientEvent(source, "onVehiclesDataReceive", source, getPlayerVehicles (source)) 
end) 

and I cant understand why, but if I replace 'resourceRoot' to 'root', script will be work.

What I must learn (or understand), to avoid those mistakes?

(Sorry for bad english)

Share this post


Link to post

In MTA all elements are organized in a tree layout where "root" is the top, by using root you can reach all available elements below it. On the second level you'll find categories such as "resources" and "players", "onPlayerLogin" is an event attached to a player which means that you can assign it to a player element or the root element to make it trigger for all players. "resourceRoot" is the root element of a resource, as there are no player elements within a specific resource your event will not trigger at all when using "resourceRoot".

For more details check this: https://wiki.multitheftauto.com/wiki/Element_tree

Share this post


Link to post

Not enough examples here...

Many questions about the server-side event, like "I cant feel difference between root and resourceRoot"

Not a function. Event.

addEvent ("requestInformationUpdate", true) 
function requestInformationUpdate(vehicle) 
    if isGuestAccount(getPlayerAccount(source)) then return end 
    triggerClientEvent(source,"onVehicleInformationUpdate", source, getElementZoneName(vehicle), getElementHealth(vehicle), getElementData(vehicle,"fuel")) 
end 
addEventHandler ("requestInformationUpdate", root, requestInformationUpdate) 

What if i change "root" to "resourceRoot"?

Share this post


Link to post
Not enough examples here...

Many questions about the server-side event, like "I cant feel difference between root and resourceRoot"

Not a function. Event.

addEvent ("requestInformationUpdate", true) 
function requestInformationUpdate(vehicle) 
    if isGuestAccount(getPlayerAccount(source)) then return end 
    triggerClientEvent(source,"onVehicleInformationUpdate", source, getElementZoneName(vehicle), getElementHealth(vehicle), getElementData(vehicle,"fuel")) 
end 
addEventHandler ("requestInformationUpdate", root, requestInformationUpdate) 

What if i change "root" to "resourceRoot"?

Then the event will only trigger if it get's triggered by the resource(resourceRoot) or his children, instead of everything (root).

It is important to remember that events pass up and down the element tree. An event triggered on the root element is triggered on every element in the tree. An event triggered on any other element is triggered on its ancestors (its parent element and its parent's parent etc) and its children, grandchildren and great-grandchildren. You can use the getPropagated argument to specify if you wish your handler to receive events that have propagated up or down the tree.

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

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

Share this post


Link to post
Then the event will only trigger if it get's triggered by the resource(resourceRoot) or his children, instead of everything (root).

I mostly understand, but when I triggered server event from client side (same resource), it can't be executed.

Okay, I have files:

:veh/client.lua, where included client GUI

:veh/server.lua, where this code will be triggered from client.lua:

addEvent("onVehicleBuyRequest", true)  
addEventHandler("onVehicleBuyRequest", resourceRoot, function(model, price, px, py, pz, rx, ry, rz)  
    local veh = createVehicle (model, px, py, pz, rx, ry, rz) 
        --[blah blah blah] 
    outputChatBox ("Congratulations! You bought a ".. getVehicleNameFromModel(model) .."! Press F2 to see it!", source) 
end) 

If i wanna to call "onVehicleBuyRequest" from other resource, I will change

addEventHandler("onVehicleBuyRequest", resourceRoot, function(model, price, px, py, pz, rx, ry, rz) 

to

addEventHandler("onVehicleBuyRequest", root, function(model, price, px, py, pz, rx, ry, rz) 

But I don't wanna do this event available from other resource. I leave "resourceRoot". And... Script won't be executed. Why?

Edit: same problem with triggered event from serverside to clientside

Share this post


Link to post

I' guessing that you trigger the event like this:

triggerServerEvent("onVehicleBuyRequest",getLocalPlayer(),model, price, px, py, pz, rx, ry, rz ) 

The problem is that playrs are never the children of “resourceRoot” but only “root” so using “resourceRoot” is almost like not adding the event...

The solution:

Client:

triggerServerEvent("onVehicleBuyRequest",resourceRoot,getLocalPlayer(),model, price, px, py, pz, rx, ry, rz ) 

Server:

addEvent("onVehicleBuyRequest", true)  
1.addEventHandler("onVehicleBuyRequest", resourceRoot, function(the_player,model, price, px, py, pz, rx, ry, rz)  
2.    local veh = createVehicle (model, px, py, pz, rx, ry, rz) 
3.        --[blah blah blah] 
4.    outputChatBox ("Congratulations! You bought a ".. getVehicleNameFromModel(model) .."! Press F2 to see it!", the_player) 
5.end) 
  

Note that in this case the Player is not longer the source of the event(Resource is used instead) so we need to add the aditional paramater to represent the player (the_player).

Edited by Guest

Share this post


Link to post

How to realize it here?

function requestElementHide(element) 
    if isGuestAccount(getPlayerAccount(source)) then return end 
    --hide function 
end 
addEventHandler ("requestElementHide", resourceRoot, requestElementHide) 
addEventHandler ("onPlayerLogout", resourceRoot, requestElementHide) 
addEventHandler ("onPlayerQuit", resourceRoot, requestElementHide) 

Share this post


Link to post

Nonono, whether it is possible to compress this?

function requestVehicleHide(vehicle) 
    if isGuestAccount(getPlayerAccount(source)) then return end 
    --[some code] 
end 
addEventHandler ("onPlayerLogout", resourceRoot, requestVehicleHide) 
addEventHandler ("onPlayerQuit", resourceRoot, requestVehicleHide) 
  
addEventHandler ("requestVehicleHide", resourceRoot, function (player,vehicle) 
    if isGuestAccount(getPlayerAccount(player)) then return end 
    --[some code] 
end) 

Share this post


Link to post

Just ignore the previous post...

  
function requestVehicleHide(player,vehicle) 
        if isGuestAccount(getPlayerAccount(player)) then return end 
        --[some code] 
end 
function preRequestVehicleHide(vehicle) 
        requestVehicleHide(source,vehicle) 
end 
addEventHandler ("onPlayerLogout", root, preRequestVehicleHide) 
addEventHandler ("onPlayerQuit", root, preRequestVehicleHide) 
addEventHandler ("requestVehicleHide", resourceRoot, requestVehicleHide) 

"onPlayerLogout" and "onPlayerQuit" can not be attached to "resourceRoot"

Sorry if I got you confused

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.