Jump to content

Question about difference in 'addEventHandler'


AxL

Recommended Posts

Hello.

I have question about difference in parameter "attachedTo" in "addEventHandler" (client side).

I have 3 examples from wiki:

1: https://wiki.multitheftauto.com/wiki/OnC ... ourceStart

2: https://wiki.multitheftauto.com/wiki/OnClientCursorMove

3: https://wiki.multitheftauto.com/wiki/OnClientGUIClick

As You see, in event "onClientResourceStart" we have 3 kinds of "attachedTo". What's a difference between them? What'll be happen if in example "onClientCursorMove" I'll write "getRootElement" etc?

This same question about difference between "getRootElement" and "getLocalPlayer".

Example: https://wiki.multitheftauto.com/wiki/OnC ... WeaponFire

In first 'example script' is used localplayer, but in second "root" (guesses, it's rootelement).

But in other events, sometimes is used rootelement, and sometimes is used localplayer. When I must use localplayer, and when rootelement?

So, how can I know what, when I must use? What's the difference between them?

Is there any tutorial, or sth from where I can learn it?

Link to comment

you need to understand this:

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

https://wiki.multitheftauto.com/wiki/Event_system

https://wiki.multitheftauto.com/wiki/GetLocalPlayer

you use getRootElement() or root if you want to trigger the event for every valid element (if the source of the event is a player, it will be all players, if it is a marker - all markers, if it is a resource - all resources, ect.)

if you want to trigger a event for a specyfic group of elements, you need to cteate a dummy element, set it as a parent of your elements, and trigger the event for that parent

you can also trigger events for a single element - getLocalPlayer() is a example of that (note: getLocalPlayer() is client only function!)

Link to comment

I understand it at ServerSide Scripts, but I'm not sure how it's works at ClientSide Scripts.

Look at example: https://wiki.multitheftauto.com/wiki/OnC ... WeaponFire

One time is used localplayer, and another rootelement.

So:

  
-- First Example 
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement ) 
         outputChatBox ( "Don't kill people with minigun, it's lame!", 255, 0, 0 ) 
end 
addEventHandler ( "onClientPlayerWeaponFire", getLocalPlayer(), onClientPlayerWeaponFireFunc ) 
  
-- Second Example: 
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement ) 
         outputChatBox ( "Don't kill people with minigun, it's lame!", 255, 0, 0 ) 
end 
addEventHandler ( "onClientPlayerWeaponFire", getRootElement(), onClientPlayerWeaponFireFunc ) 
  

In first example it'll send message to one player, who initiated this event, and in second example it'll send message to all on-line players, yes?

But, if it's true, why in events like: https://wiki.multitheftauto.com/wiki/OnClientMouseEnter we use rootelement, not localplayer?

Link to comment

it's important to know whot is the source of the event (this information is on the wiki in every event description...)

about the First Example:

source of "onClientPlayerWeaponFire" is a player, and getLocalPlayer() returns a player, so event will be trigger for one player

about the "onClientMouseEnter" event:

the source is a gui element, so you can't use the player element

to trigger that event for single element you can for exemple:

myWindow = guiCreateWindow ( 0, 0, 0.5, 0.4, "Information", true ) 
addEventHandler( "onClientMouseEnter",myWindow,  
    function(aX, aY) 
        outputChatBox( "You're pointing at a GUI window at ("..tostring(aX)..", "..tostring(aY)..")") 
    end 
) 

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