Jump to content

triggering event only for players


drifterCZE

Recommended Posts

Hello, sorry if it was sad anywhere on forum or on wiki, but I was looking for it and hadn't found anything.

I want to trigger function but only when the "source" is an player. how to do it via addEventHandler?

addEventHandler ( "onElementDataChange", -playersElement-, myFunction ) 

on Wiki isn't anything about how to trigger events for specified group of elements (I dont want to create dummy element and check each few second if players don't changed)

Thanx for help

Link to comment
Hi add this condition at the begining of your function that is triggered with onElementDataChange
if ( getElementType( source ) == "player" ) then 
    -- your instructions 
end 

that will not solve his problem- he still need to trigger the event for root element, and as far i know it mean looping through all elements in all resources...

the only alternative is to use the dumy element, but you dont need to check it each few second, as players count only change on join and quit,

so you only need to set player parent if onPlayerJoin is triggerd (you need to use root element on this event, but it will triger only once per join)

Link to comment
  • Moderators

He said:

I want to trigger function but only when the "source" is an player

So my script is correct !:

function myFunction() 
    if ( getElementType( source ) == "player" ) then 
        -- your instructions 
    end 
end 
addEventHandler ( "onElementDataChange", getRootElement(), myFunction ) 

I know I'm french but I understood what he wants.

Link to comment

@Citizen: You may undarstand the sentence, but not necessarily a context

how to do it via addEventHandler?

"addEventHandler ( "onElementDataChange",-playersElement-, myFunction )"

on Wiki isn't anything about how to trigger events for specified group of elements

and youre cod is correct, but it not solvs the problem (drifterCZE want to save some memory resources)

An event triggered on the root element will be triggered on every element in the element tree. This should be avoided where possible.
You can create 'dummy' elements to catch events from a group of child elements

https://wiki.multitheftauto.com/wiki/Event

https://wiki.multitheftauto.com/wiki/SetElementParent

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

Link to comment

https://wiki.multitheftauto.com/wiki/OnElementDataChange

this event handler uses "source" as the element who element data has been changed, so you could just

example from wiki:

function outputChange(dataName,oldValue) 
    if getElementType(source) == "player" then -- check if the element is a player 
        local newValue = getElementData(source,dataName) -- find the new value 
        outputChatBox("Your element data '"..tostring(dataName).."' has changed from '"..tostring(oldValue).."' to '"..tostring(newValue).."'",source) -- output the change for the affected player 
    end 
end 
addEventHandler("onElementDataChange",getRootElement(),outputChange) 

This is only triggered for who element data has been changed if i'm right.

Link to comment
This is only triggered for who element data has been changed if i'm right.

function is called every time some data changes on any element (vehicle, ped, ect), some resorces (zombies for example) change the data for non player elements all the time, so the function is caled for no reason, only to chack if the source is a player

drifterCZE want to be shure that the source is always a player, and the function will be not caled for non player elements

I'm sorry but i can not explain it better :(

Link to comment
  • Moderators
This is only triggered for who element data has been changed if i'm right.

function is called every time some data changes on any element (vehicle, ped, ect), some resorces (zombies for example) change the data for non player elements all the time, so the function is caled for no reason, only to chack if the source is a player

drifterCZE want to be shure that the source is always a player, and the function will be not caled for non player elements

I'm sorry but i can not explain it better :(

We understood correctly wojak but he said that because he didn't know the getElementType function so for him the only way to trigger this function for only players is to make the selection in his addEventHandler.

He is here to learn so we ( me and SolidSnake ) show him another ( the better ) way to make this selection with a new function for him ( getElementType ).

Link to comment

easy way is not always beter ;)

i think someone who understand the element tree (drifterCZE) also know how getElementType works...

there may be no difference for human observer, but there is a diference fo a computer.

I will try a real life analogy:

Imagine you work in the ofice and you can help people with last name starting on C, but people with last name starting on A,B (+ rest of alphabet) kep coming through your'e door, and you have to say to every one of them:

"I can't help you, now get out!"

this is what trigering onElementDataChange for root element and checking the source means

it would be easier to stick the note on the door "I can only help people with last name on C" - create a dummy element, set it as a parent of elements you want to trigger the event for, and trigger the event for the dummy element

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