Xwaw Posted February 2, 2023 Share Posted February 2, 2023 (edited) Hello, I recently returned to making a server after a year and I wanted to remind myself how the "source", "root", "this", "sourceResource", "sourceResourceRoot", "client" etc. Elements worked. Unfortunately, when I checked the MTA wiki, I didn't understand any of these elements. In general, the best help would be if someone explained to me how these elements work on examples for AddEventHandler because in mta wiki there is only one example for a "source" and I guess "root"? element Edited February 2, 2023 by Xwaw Link to comment
Trust aka Tiffergan Posted February 2, 2023 Share Posted February 2, 2023 In MTA, when a script is attached to an object, that object is considered the "source". The "root" element refers to the highest level parent object of the source. For example, if a script is attached to a vehicle, then the source would be the vehicle and the root would be the game world. "This" refers to the object that the script is attached to, which is equivalent to the "source". "SourceResource" refers to the resource that the script is part of. Resources are collections of files and scripts that define a specific aspect of the game, such as a custom game mode or a custom vehicle. "SourceResourceRoot" refers to the root directory of the resource that the script is part of. "Client" refers to the client-side script that is running on a player's computer. This is opposed to a server-side script, which runs on the game server. In the context of AddEventHandler, these elements can be used to specify the object that the event handler is attached to. For example: addEventHandler("onPlayerJoin", root, function() outputChatBox("Welcome to the server!") end) In this example, the event handler is attached to the root element, meaning that it will trigger for every player that joins the server. The function passed to AddEventHandler will be called, displaying a message in the chat for each player that joins. Link to comment
Xwaw Posted February 2, 2023 Author Share Posted February 2, 2023 2 hours ago, Trust aka Tiffergan said: In MTA, when a script is attached to an object, that object is considered the "source". The "root" element refers to the highest level parent object of the source. For example, if a script is attached to a vehicle, then the source would be the vehicle and the root would be the game world. "This" refers to the object that the script is attached to, which is equivalent to the "source". "SourceResource" refers to the resource that the script is part of. Resources are collections of files and scripts that define a specific aspect of the game, such as a custom game mode or a custom vehicle. "SourceResourceRoot" refers to the root directory of the resource that the script is part of. "Client" refers to the client-side script that is running on a player's computer. This is opposed to a server-side script, which runs on the game server. In the context of AddEventHandler, these elements can be used to specify the object that the event handler is attached to. For example: addEventHandler("onPlayerJoin", root, function() outputChatBox("Welcome to the server!") end) In this example, the event handler is attached to the root element, meaning that it will trigger for every player that joins the server. The function passed to AddEventHandler will be called, displaying a message in the chat for each player that joins. Re: And give an example of a script for which you will use e.g. "this" Link to comment
Trust aka Tiffergan Posted February 2, 2023 Share Posted February 2, 2023 3 hours ago, Xwaw said: Re: And give an example of a script for which you will use e.g. "this" local myButton = createButton(0.5, 0.5, 0.1, 0.1, "Click me!", true) addEventHandler("onClientGUIClick", myButton, function() outputChatBox("Button clicked!") end) In this example, the script creates a GUI button and adds an event handler for the onClientGUIClick event. When the button is clicked, the event handler is triggered, and this refers to the button element that was clicked. The output message will display "Button clicked!" in the chatbox. 1 Link to comment
Moderators IIYAMA Posted February 2, 2023 Moderators Share Posted February 2, 2023 5 hours ago, Trust aka Tiffergan said: "This" refers to the object that the script is attached to, which is equivalent to the "source". Just a side note (you probably already know this: this can be the same as source, but it doesn't have to be. For example here: local myButton = createButton(0.5, 0.5, 0.1, 0.1, "Click me!", true) addEventHandler("onClientGUIClick", resourceRoot, function() outputChatBox("Button clicked!") end) this is always resourceRoot While source in this set-up is always a GUI element of this resource. For @Xwaw See image below, is the element tree, where you can see some of the main parent elements in MTA. root at the very top is the parent for ALL elements within your game: This includes players, resources(resourceRoot's) > maps/'scripted created elements' and everything that hangs directly under those. resourceRoot is the main parent of each resource. Which can be used to listen to events created by elements that are part of a specific resource. Info: https://wiki.multitheftauto.com/wiki/Element_tree 2 Link to comment
Xwaw Posted February 4, 2023 Author Share Posted February 4, 2023 Thank all of u very much ^^ Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now