Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 17/07/24 in all areas

  1. And big thanks for all this explains, it helps me very much
    1 point
  2. You can, but it is sometimes better to ask your main questions here. And if nobody knows the answer, you can always send me a message to a specific topic. There are more people out there that could have provided this answer. The variable root is predefined variable, containing the root element pointer. https://wiki.multitheftauto.com/wiki/Element_tree Which is the great parent of parents ^^. Take a look at the schematic (on the wiki). Events work with a kind parent to child relation. Triggering an event on an element with a lot of children can increase CPU usage significant (execution time more or less 0.5% for each child extra, based on a sample created on my specific system). So it is to reduce the CPU usage. But also making sure you do not clash with other resources. Because an event is a global interaction, a resource can trigger an eventHandler for another resource. It is recommended to trigger an event with the resourceRoot, a specific element of that resource or a player. The resourceRoot is a predefined variable that is the root element for a specific resource (See purple nodes in the schematic). There is a resourceRoot for every resource and each of them is unique. [For each resource]: The resourceRoot of clientside is linked to the resourceRoot of serverside. They are considered more or less the same (In technical terms that is not the case, but you will not notice it). The benefit of using resourceRoot is that the eventHandlers that listen to resourceRoot, will only trigger for the same resourceRoot or (by default) their children. The following example shows how a resource communicates from serverside to clientside. And the communication is scoped with the resource itself. This does not mean that other resources can't listen to the "onGreeting" event, but the eventHandler will not trigger for other resources that run the exact same code. -- Server -- (playerSource is not yet defined) triggerClientEvent ( playerSource, "onGreeting", resourceRoot ) -- Client addEvent( "onGreeting", true ) addEventHandler( "onGreeting", resourceRoot, function () end ) -- Default: resourceRoot + children addEvent( "onGreeting", true ) addEventHandler( "onGreeting", resourceRoot, function () end, true ) -- resourceRoot + children addEvent( "onGreeting", true ) addEventHandler( "onGreeting", resourceRoot, function () end, false ) -- only resourceRoot See also: local thisElement = resourceRoot addEvent( "onGreeting", true ) addEventHandler( "onGreeting", thisElement, function () end ) -- thisElement should have the same value > -- Triggering an event on the same side(server or client) triggerEvent("onGreeting", thisElement) -- > as thisElement or a child of it. If using root on an eventHandler, it will trigger for all elements, since this is the great parent of parents... All resources will be able to trigger this event, no matter which element they use. addEvent( "onGreeting", true ) addEventHandler( "onGreeting", root, function () end ) Is it possible to trigger the following eventHandler from another resource? addEvent( "onGreeting", true ) addEventHandler( "onGreeting", resourceRoot, function () end ) Technically yes, elements from another resource are all part of the element tree. So as long as you use the right element, it is possible. (normally I do not explain this much)
    1 point
  3. YES! THANKS VERY MUCH IT WORKS! Can I ask some questions else? 1. Can I'll write to your's PM for some questions in future? Looks like you are advanced user and can help with many problems 2. Nothing go bad if I use "root" in client events? Because on wiki was written that root should not use, only in specific moments. I tried this too, but without new event it didn't work. And still thank you, that was very important message for best understanding of client triggers!
    1 point
  4. I recommend to replace most of your serverside code with (see example): https://wiki.multitheftauto.com/wiki/OnPlayerResourceStart
    1 point
  5. Debugging Do you know what debugging is? You might think you do, but unfortunately (in my opinion) only ~15% of the scripters in the community do know the full definition of it. Many people think that debugging code is the same as looking in to the Debug Console and waiting for warning + errors to show up. That's indeed debugging and yet it never provide all information you need to build your scripts. It only can say what goes wrong at a certain line. With other words, the Debug Console by default will only show a limited amount of mistakes you have made in your code. So what is next? You fixed all warnings and errors and yet it doesn't work. You start with making your code visible! I guess 70% would think: Making code visible? Ehhh how??? Let me write it down a little bit different: By using Debug Information making the behaviour of the code visible. I guess 50% would think: Eh what? behaviour of code????? Let me give you an example. Example: (1) outputDebugString("the script has started") -- < this is a debug line if true then outputDebugString("code works here") -- < this is a debug line else outputDebugString("code shouldn't be working here") -- < this is a debug line end Debug console "the script has started" "code works here" The debug console is NOT information for players, it is information for YOU developers! BTW this is a debug line outputDebugString("test") -- < this is a debug line In this case it is just a piece of code that shows information in the debug console. Example: (2) local playerName1 = "snake1" local playerName2 = "cow" if playerName1 == playerName2 then outputDebugString("players playerName1 and playerName2 do share the same name. Name: " .. tostring(playerName1)) -- < this is a debug line else outputDebugString("players playerName1 and playerName2 do NOT share the same name. playerName1: " .. tostring(playerName1) .. ", playerName2: " .. tostring(playerName2)) -- < this is a debug line end Debug console "players playerName1 and playerName2 do NOT share the same name. playerName1: snake1, playerName2: cow" Easy isn't? The concept behind this debug method is to see what the code does / doesn't execute. Is this method handy? It is actually the very basic of debugging, for code that doesn't contain any errors/warnings. I would say it is handy and it is a very powerful method too. It is also handy for people who do not know how to script. If you want people to help you with your code, but you do not know what is wrong with it. You can add those debug lines and point out to where the code stops working. This will make it more efficient for you and the scripter to work out the problem, because the scripter knows where to look. How much debug lines do you have to add to your script? 1? 10? 100? 1000? You could start with around 100 debug lines and as you learn how to script, you can reduce it to 10+ debug lines. Too much debug lines are not always good, because they will give you too much information and it will cost time to manually filter them. So I recommend you to remove some of them afterwards. When you are finished with the tested code, you can remove 90+% of them. Feel free to disable them instead of removing them, if you know that you are going to need them again. For complex code, I use around 25 debug lines, SO DON'T HOLD BACK! Render events It is strongly recommended to remove debug lines that are executed on onClientRender/render events when you are finished with your code. Because that can have influence on the smooth fps.(It will not drop much of the fps, but it can make it feel unsmooth) Clearing the debug console? /cleardebug Know your tools: outputDebugString -- Show a message on the Debug Console bool outputDebugString ( string text, [ int level=3, int red=255, int green=255, int blue=255 ] ) --- outputConsole -- Show a message on the F8 panel. bool outputConsole ( string text ) -- client bool outputConsole ( string text, [ element visibleTo=getRootElement() ] ) -- server --- inspect -- Convert one mixed value to a string. string inspect ( mixed var ) --- print -- Show a message on the terminal / serverwindow / Debug Console. bool print ( string var1[, string var2, string var3...] ) --- tostring() -- Convert a value in to a string. (but for objects/elements, inspect works better) --- iprint -- Show a message on the terminal / serverwindow / Debug Console (convert multiple mixed values automatic to string, no need for tostring or inspect) bool iprint ( mixed var1[, mixed var2, mixed var3...] ) --- outputChatBox -- You can also debug with outputChatBox (even though it is less efficient) bool outputChatBox ( string text [, int r=231, int g=217, int b=176, bool colorCoded=false ] ) -- client bool outputChatBox ( string text [, element visibleTo=getRootElement(), int r=231, int g=217, int b=176, bool colorCoded=false ] ) -- server Debug message levels 0: Custom message 1: Error message 2: Warning message 3: Information message (default) Addition by @Hale https://wiki.multitheftauto.com/wiki/OutputDebugString Advanced tools: local line = debug.getinfo(1).currentline -- get the line of the script where the code has been executed. 1 = current function. (can be useful if you want to get the line where this function has been called from) https://www.lua.org/pil/23.1.html WIKI MTA: WIKI MTA debugging tutorial/information. https://wiki.multitheftauto.com/wiki/Debugging
    1 point
×
×
  • Create New...