Jump to content

event system


Recommended Posts

hi, after reading the wiki event system many time, i still don't understand it very well, the part that i dont understand is the second argument of addEventHandler , the one that indicate what element the handler is attached to, i don't understand when the handler will be or not be called(according to wich is the source element that triggered this event).

an exemple of code

  
function init_gui() 
    GUILogin = { 
            button = {}, 
            window = {}, 
            edit = {}, 
            label = {} 
    } 
    GUILogin.window[1] = guiCreateWindow(325, 269, 364, 161, "Menu ~~ Connexion", false) 
    guiWindowSetSizable(GUILogin.window[1], false) 
  
    GUILogin.label[1] = guiCreateLabel(10, 87, 99, 23, "Mot de passe :", false, GUILogin.window[1]) 
    guiSetFont(GUILogin.label[1], "default-bold-small") 
    guiLabelSetHorizontalAlign(GUILogin.label[1], "center", false) 
    guiLabelSetVerticalAlign(GUILogin.label[1], "center") 
    GUILogin.edit.password = guiCreateEdit(109, 87, 245, 23, "", false, GUILogin.window[1]) 
    GUILogin.label[2] = guiCreateLabel(10, 24, 344, 53, "Bonjours, Notre systéme indique que vous avez deja un \ncompte, veuillez inserer votre mot de passe pour vous \nconnecter. si ce n'est pas le cas veuillez changer de nom", false, GUILogin.window[1]) 
    guiLabelSetHorizontalAlign(GUILogin.label[2], "center", false) 
    guiLabelSetVerticalAlign(GUILogin.label[2], "center") 
    GUILogin.button.login = guiCreateButton(10, (161 - 27) -10, 98, 27, "Connexion", false, GUILogin.window[1]) 
    GUILogin.button.register = guiCreateButton(364 - 98 - 10, (161 - 27) -10, 98, 27, "Connexion", false, GUILogin.window[1]) 
    guiSetVisible(GUILogin.window[1], false) 
end 
addEventHandler("onClientResourceStart", getResourceRootElement(), init_gui) 
  
addEvent("showLoginGUI", true) 
addEventHandler("showLoginGui", getRootElement(), function() guiSetVisible(GUILogin.window[1], true) end) 
  

the last line :

addEventHandler("showLoginGui", getRootElement(), function() guiSetVisible(GUILogin.window[1], true) end) 

i don't know what i must put in the second argument : getRootElement() or getResourceRootElement() or localPlayer

Link to comment

Hello,

The wiki page for addEventHandler provides a good description of this parameter: "The handler will only be called when the event it is attached to is triggered for this element, or one of its children.". To understand this, you need to be familiar with the Element Tree. Basically, the attachedTo parameter allows you to restrict the events to a particular element. If I have, for example, 3 GUI buttons, If I were to attach an onClientGUIClick to the Root Element, it would be triggered when any of the 3 buttons are clicked. However, if I attach the same event to just one of those 3 buttons specifically, it will only get triggered when that specific button is clicked. To know what element to attach an event handler to, look at the Source section on the event's wiki page. For events like onClientClick, it says: "The source of this event is the client's root element." . The root element is on the top of the element hiearchy, therefore your only option here is to attach it to the root element. As mentioned before, with an event like onClientGUIClick, you can attach it to the root element to receive GUI click events from ANY resource, you can attach it to getResourceRootElement() to receive GUI click events from only THIS resource, or you can attach it to a specific GUI element and only get events from that single element. Of course, you can always attach an event to any element you like. With custom events (say, showLoginGui like yours) it's important to consider how the event will propagate. If you have the same event registered in two different resources and have both attached to the Root Element, you couldn't really control which one you want to trigger. The Event System page provides a good description of how this works exactly, I highly recommend you read it.

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