Jump to content

IIYAMA

Moderators
  • Posts

    6,058
  • Joined

  • Last visited

  • Days Won

    208

Everything posted by IIYAMA

  1. thx! Lots of after editing ? pfff pfff fff Will add some missing content later.
  2. Events tutorial The reason why I created this topic, is that a lot of people are struckeling with them. In this tutorial I will only discus the very basic of them. If you want more, then there is a list of links at the end with more information. If I made any mistakes in the code, please let me know because I am not going to test every part. What are events? (basic description) Events are something custom added by MTA to make it easier to bring scripting(Lua) closer to our game. If we do not have events, then the only thing we can do is give instructions to our game. But our code will never detect changes in our game. The same question again: "So what are the events?" Events are a way to communicate changes in our game to our scripts (or from our scripts). So for example my little ped(cat) gets ran over by a car. Then I really want to know about that, don't I? When an event activates, I describe this as: triggered (from the word trigger) Full wiki information can be found here: Event_system Before we can use events, what do we need to know? There are two things we need to know: The reason why it is triggered. <What happens?/when something happens?> In MTA this is the eventName of the event. (Example: you <ran over> my ped) Who is the one using the event? The event system in MTA is using elements as base (baseElement). This makes it easier to combine the what/when happens? with the one who is related to it. In MTA this is the source of the event. (Example: you ran over my <ped>) Trigger an event A scripting example: (this is not an official event) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped triggerEvent("onPedRanOver", ped) In this example, a custom event gets triggered to tell you that my new created ped has been ranOver. The eventName is "onPedRanOver" and the baseElement is ped. local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) In this SERVERSIDE example, I am also adding an extra argument drunkDriver to use the player that ran over the ped. This is not required, but it makes it more complete. See syntax. Syntax bool triggerEvent ( string eventName, element baseElement, [ var argument1, ... ] ) TriggerEvent Receiving triggers Receiving triggers is a bit more complicated, because there are a lot of options for it. You can receive events by listening to them. It is like: You know that something is going to happen but you do not know when. The first step that you have to take is related to this question: "Do I want to receive a custom or not custom event?" Custom events are self created events. They only exist when a scripter makes them. Two lists of NOT CUSTOM EVENTS but default MTA events, serverside and clientside: Server_Scripting_Events Client_Scripting_Events Do I want to receive a CUSTOM event? In case of a custom event, you have to register/enable it first. If you do not enable it and trigger it, you will receive a warning/error about that in your debug console. Syntax bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] ) AddEvent The example below, shows you how to enable a custom event only for trigger events within the same server/client side. addEvent("eventName") -- Is the same as: addEvent("eventName", false) If you put the second argument to false or not fill it in, this means that you can't communicate from the other server/client-side. This option is most likely used for security reasons. Some events shouldn't be able to trigger by the other side For example, worst case scenario: (remote events enabled for a default MTA event) Serverside code: addEvent("onPlayerWasted", true) Clientside code: triggerServerEvent("onPlayerWasted", player, 0, localPlayer, 0, 9, false) OnPlayerWasted If this event is enabled for remote trigger events, then it might be possible to cheating kills/deaths score. Of course, it is not likely that players can run their own clientside code, but it is not impossible in case of not trust able community resources. Enable a custom event for trigger events that crossing sides (From clientside to serverside. From serverside to clientside). addEvent("eventName", true) This event can now be used by remote trigger event functions. See list: Client to server TriggerClientEvent TriggerLatentClientEvent Server to client TriggerServerEvent TriggerLatentServerEvent Enable the event from our previous example: addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) If you do not use cross triggering, then I recommend to use the addEvent function in the same resource as where you are going to trigger from. This makes sure that the event is already added and that you will never receive this kind of error/warning "Event isn't added". If you put it in another resource which hasn't started yet, then after triggering you would still receive that error/warning. Start listening The next step is to add the addEventHandler. This function is used to listen to events. When an event is triggered, this handler(addEventHandler) will call the function you have attached to it, in MTA this function is called the handlerFunction. Syntax bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] ) AddEventHandler Resource 1 addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 function handlerFunction () end addEventHandler("onPedRanOver", root, handlerFunction) The first 3 arguments, the require ones: eventName attachedTo handlerFunction Making sure that the addEventHandler options are correct set-up. Resource 1 addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 function handlerFunction () end addEventHandler("onPedRanOver", root, handlerFunction) There are two conditions for an eventHandler to call the handlerFunction. 1. The event has to be exactly the same. In this case the event "onPedRanOver" is the same in both resources. 2. In both functions, triggerEvent and addEventHandler is an element being used. This element has to be exactly the same. (from where you trigger as well as where you receive) <OR> The triggered element from resource 1, has to be a CHILD of the element in resource 2. The root element is the very top layer of the MTA element structure. It will accept all elements you want to use for your events. See the element tree: If you do not understand the element tree please read this page: Element_tree Source variable The source of an event is the element that triggers the event. This variable isn't passed as an parameter, but it is predefined. This means that it is already created before hand. Some predefined variables do only exist under special conditions. The source variable is one of those, it is a hidden and local variable which is only available when a function is called by an event. List of predefined variables. addEvent("onPedRanOver", false) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) function handlerFunction (drunkDriver) iprint(source) -- ped element end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) In this example the ped is the source. See how those two code blocks are connected: addEvent("onPedRanOver", false) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) function handlerFunction (drunkDriver) iprint(source) -- ped element end addEventHandler("onPedRanOver", resourceRoot , handlerFunction) resourceRoot In some examples, you see people use the resourceRoot instead of the root element for their addEventHandlers. The resourceRoot is an element created by a resource. This element holds all elements of that resource as (in)direct children. In the example above, the resourceRoot as baseElement will not work, because there are two resources. Each resource has it's own resourceRoot element. The resourceRoot is accessible with the same keyword: resourceRoot, but if you were to inspect the element in multiple resources, then the user data (element identifier) value is not the same. outputChatBox(inspect(resourceRoot)) If we were to put everything in one resource, then it would work: ? addEvent("onPedRanOver", false) -- function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) In case of remote triggering, the resourceRoot in serverside and clientside is considered the same.(As long as they are part of the same resource) Why/when would we use resourceRoot? 1. Limit eventHandlers to the resource elements If you have 1000 markers in your server. One of the resources is for example a trucker mission, where you can get money by hitting markers. The resourceRoot element will make sure that the onMarkerHit event will only trigger for markers created by that resource. addEventHandler("onMarkerHit", resourceRoot, function () -- source element is the marker end) OnMarkerHit 2. Another benefit is that you are able to re-use the same eventNames. Resource 1 addEvent("onPedRanOver", false) function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 addEvent("onPedRanOver", false) function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) These two resources do use the same event, but will not trigger each other their addEventHandlers. Warning: If root was used, then they will!!!! ;@ Lets cross triggering with resourceRoot! Clientside triggerServerEvent("example", resourceRoot) Serverside addEvent("example", true) -- second argument is true! cross triggering enabled! addEventHandler("example", resourceRoot, function () end) getPropagated In this bigger example we will be talking about the option getPropagated. If this option is disabled, it will not detect children any more. Keep reading! After that start code scanning from A, to B and then to C. Syntax addEventHandler bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] ) Example: Clientside -- A triggerServerEvent("onClientPlayerLoaded", resourceRoot) -- trigger an event to serverside --------------------------------------- -- C addEvent("onResponseServer", true) -- first listener addEventHandler("onResponseServer", resourceRoot, function () outputChatBox("getPropagated enabled") end, true) -- getPropagated true by default. -- second listener addEventHandler("onResponseServer", resourceRoot, function () outputChatBox("getPropagated disabled") end, false) -- getPropagated is false. Serverside -- B addEvent("onClientPlayerLoaded", true) -- second argument is true! cross triggering enabled! addEventHandler("onClientPlayerLoaded", resourceRoot, function () --[[ client is a predefined variable, which represents the client/player that communicates with the server More information about predefined variables: https://forum.multitheftauto.com/topic/33407-list-of-predefined-variables/ ]] triggerClientEvent(client, "onResponseServer", resourceRoot) -- first trigger event local element = createElement("randomElement") -- making a randomElement triggerClientEvent(client, "onResponseServer", element) -- second trigger event end) How does this this code works? A. When a client his code has been started, it will execute a triggerServerEvent. (It doesn't wait for any other clientside files to be loaded) B. The server receives the event. And sends two triggerClientEvents back: The first one is using the resourceRoot as baseElement. The second one is using a randomElement as baseElement. Both are using the event "onResponseServer" C. There are two addEventHandlers listening to the event: "onResponseServer" The first one is using getPropagated and the second one is not using getPropagated. The randomElement that is created, is by default an indirect child of the resourceRoot of the same resource. What will happen? When firing the first trigger event, both listeners will call their handlerFunction. But when firing the second trigger event, only the first listener will call it's handlerFunction. The randomElement is an indirect child of resourceRoot, but because getPropagated is disabled it will not call it's handlerFunction. Other tutorials related to this one: See also this tutorial about deeper limiting event ranges within your resource and reducing addEventHandlers https://forum.multitheftauto.com/topic/100069-tut-addeventhandler-on-a-group-of-elements-small-tutorial/ More information Full wiki information: Event_system A list of more information about triggering events: (Client to client / server to server) TriggerEvent Client to server TriggerClientEvent TriggerLatentClientEvent Server to client TriggerServerEvent TriggerLatentServerEvent A list of more information about receiving events: AddEvent AddEventHandler RemoveEventHandler Two lists of MTA events, serverside and clientside: (warning: not custom events) Server_Scripting_Events Client_Scripting_Events Cancel events CancelEvent WasEventCancelled (warning: custom events ONLY) GetCancelReason (Server only) Cancel latent events and their status GetLatentEventHandles CancelLatentEvent GetLatentEventStatus
  3. You can't add them there automatic. But there are two other ways to start resources. 0. https://wiki.multitheftauto.com/wiki/GetResources https://wiki.multitheftauto.com/wiki/GetResourceState https://wiki.multitheftauto.com/wiki/XML (this can be a external or internal (meta.)xml file, and you can also generate for your mtaserver.conf file) or https://wiki.multitheftauto.com/wiki/JSON Ways of starting: https://wiki.multitheftauto.com/wiki/StartResource https://wiki.multitheftauto.com/wiki/RestartResource (requires admin rights) <include /> https://wiki.multitheftauto.com/wiki/Meta.xml (warning: the source resource can't be started if the included resources are missing or broken, this can be annoying or a benefit for validation)
  4. Yes it is advanced stuff, for if you have a lot of players. How about you give this first a try? https://wiki.multitheftauto.com/wiki/Resource_Web_Access Basis, contents: 1, 2, 2.1 https://wiki.multitheftauto.com/wiki/HttpWrite
  5. If it is not looped or streamed, then it will destroy itself when it ends.
  6. You can also make the sound a child of a player. setElementParent(sound, player) This will auto destroy the sound when the player leaves the server.
  7. @koragg Remember, your database is already in use by your server. If external parties can request data from it, this can overuse your database in case of spam requests. If it is going to be used a lot, then I recommend to make 2 API's. 1 PHP on your MTA server. 1 Nodejs, on a server of choice. Benefits/extra's/how to use: The Nodejs server will request the scoreboard once in a while. (For buffer purposes) On the PHP side you can set states to check if there are updates in the data, it is recommended but not required. The website(for client requesters) that you are going to use, can be a part of Node or even hosted at a different server of choice. (Your MTA server can send messages to your Note server to tell that the scoreboard is updated, if you want quick updates on your endpoint for your clients.) Using a Nodejs server can protect your server from spam requests and buffer the data in the memory. It also gives you access to sockets. (for live updates)
  8. Not that I know of. Just make sure that you test it in an empty environment without gamemodes running.
  9. IIYAMA

    Performance

    For the samples just memory, but it doesn't hurt to do both.
  10. IIYAMA

    Performance

    Alright, Go to this page: https://wiki.multitheftauto.com/wiki/Resource:Performancebrowser Next to high CPU usage, your code can also be creating a memory leak. This means that your code is not able to clean itself. Just like buy stuff your entire life, but you never throw it away. At the end it gets piled in your basement... https://wiki.multitheftauto.com/wiki/Resource:Performancebrowser#Category_-_Lua_memory To check this: -1. Go to this page: https://wiki.multitheftauto.com/wiki/Resource:Performancebrowser#Category_-_Lua_memory 0. Start your resourcebrowser. 1. Set category: lua memory 2. Start all your resources you would normally need. 3. Wait a minute or two for the memory values to be normalised. 4. Make a screenshot of the values, this is your first sample. 5. repeat step 4 multiple, with a delay of your choice. 6. Now compare the samples. The column current should always get down at a given point. If it keeps increasing forever, then you have a memory leak in your code. Check also column timers, it is not recommended to have here a leak. This can/will crash your server after a while.
  11. IIYAMA

    Performance

    I am not a professional at hardware performance, so the information below might not be 100% correct. You have per CPU core an amount of processing power available. If you have a duo core then your pc have 2 cores. And for a quad-core you have 4 of those. (virtual cores excluded) Your system will assign application processes to those cores. (MTA is an application) MTA can only use one of those cores. So that is your limit. If you have a lot of cores it doesn't usually mean you have more performance in MTA. But it can it can relieve pressure by being able to keep other applications at different cores. If my interpretation is correct, then 100% is the available amount of (1 CPU core) power assigned for your resources. This is most likely not 100% of this single CPU core. yes I know, it is complicated stuff. Crossing that value should result in high frame dropping. Please correct me if I am wrong.
  12. IIYAMA

    Performance

    @mehmet tickCount shows execution timing. local timeNow = getTickCount() for i=1, 1000 do local newFunction = function () end end outputChatBox("It took: " .. (getTickCount() - timeNow) .. " ms, to execute this code.") 2.5% CPU is high, but not critical. You should be more worried about resources that show ~10%+. The gamemode `tactics` is a bad example of that.
  13. function openGUI ( player ) local updates = getUpdates ( ) triggerClientEvent ( player, 'Updates:onPanelChangeState', player, 'main', updates ) end addCommandHandler ( "news", function (player) openGUI(player) end) addEventHandler("onPlayerLogin", root, function () openGUI(source) end) Read the docs: https://wiki.multitheftauto.com/wiki/AddCommandHandler https://wiki.multitheftauto.com/wiki/OnPlayerLogin Keep in mind that players can login before they have loaded their scripts, unless the login command is blocked. (the panel will not open in that case)
  14. If you do that every 5 seconds forever, then ask the question: "is your network activity not in progress forever?" Make sure to check that! /shownetstat
  15. Hmmmm ? Why not? The circle is perfect rounded. While making my svg version a while ago, I checked that fact.
  16. arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below. https://wiki.multitheftauto.com/wiki/AddCommandHandler if argument then -- check end @Hugo_Almeidowski
  17. There are already 4 ways to do this, is that not enough? ? A 5e way. Calculate the distance from the center of the image. A 6e way. Compromise, use a different design that is easier to calculate the button detection.
  18. IIYAMA

    All about peds

    @Peti 1. + 3. To answer this, you need to understand the difference between client and serverside: The server is a computer. Which RUNS serverside. The Clients are multiple computer connected to the server. Which RUNS clientside. The internet and the network is in between. (ping is the delay which separates those two types of computers) In case of a local-server the ping 0, but both sides are still separated. And that will give the result as @Patrick2562 said. 2. You make peds created by clientside/serverside shoot at clientside. The reason why it can't be done serverside is because MTA doesn't support it yet.
  19. IIYAMA

    Why?

    See also this tutorial, it explains how the addEventHandler works in your code. ??
  20. IIYAMA

    Why?

    It means that the sound has been created successfully. (Are you sure that you are in the same dimension as the ped?)
  21. IIYAMA

    Why?

    @Hugo_Almeidowski hmm, just to be sure debug the sound element. local soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true) outputChatBox(inspect(soundREEE))
  22. IIYAMA

    Why?

    @Hugo_Almeidowski Did you check the debug console? I didn't change anything before the audio started to play, so either it was already broken or there is an error/warning.
  23. yea that can work. I recommend to open the design in Photoshop/something-similar and get all the positions from there. (you might need to do a little bit of maths afterwards to adjust it to the (possible) responsive layout in game)
  24. It doesn't tell you which field you selected. dxGetTexturePixels is a very slow process.(unless it is only done once at the start) But feel free to try it your way. I am interested in what you discover on the way.
  25. IIYAMA

    Why?

    ID elements have to be unique. There shouldn't be two elements with the same ID. local pedContainer = createElement("pedContainer") function TocarSomPed(ped) local pX, pY, pZ = getElementPosition(ped) local dimensaoP = getElementDimension(ped) local IDPed = getElementID(ped) outputChatBox("O nome deste ped é " ..IDPed) local soundREEE = playSound3D("reeet.mp3", pX, pY, pZ, true) setElementDimension(soundREEE, dimensaoP) attachElements(soundREEE, ped) setElementParent(soundREEE, ped) -- the SOUND is now a child of the PED. setElementParent(ped, pedContainer) -- PED is now a child of pedContainer, which has an addEventHandler attached to it. See line 1 and line 31. end addEvent("onPedCriado", true) addEventHandler("onPedCriado", root, TocarSomPed) -- function pedMorreuClient() local setarIDSom = source -- source is the ped. See page: https://wiki.multitheftauto.com/wiki/OnClientPedWasted local soundREEE = getElementChild ( setarIDSom, 0 ) -- get the first child of this PED, which is the SOUND. See line 12. if soundREEE then destroyElement(soundREEE) end end addEventHandler("onClientPedWasted", pedContainer, pedMorreuClient) https://wiki.multitheftauto.com/wiki/GetElementChild https://wiki.multitheftauto.com/wiki/SetElementParent This is information about how children and parents work in MTA: https://wiki.multitheftauto.com/wiki/Element_tree#Example
×
×
  • Create New...