Jump to content

IIYAMA

Moderators
  • Posts

    6,097
  • Joined

  • Last visited

  • Days Won

    218

Everything posted by IIYAMA

  1. addEventHandler + group elements I noticed that some people like to add 10000000000000000 addEventHandlers for each element, while you probably only need 1 addEventHandler. Using ONE addEventHandler on a group of elements? Answer: local group = createElement("groupMyCutePeds") -- Create a custom element and save it in to the variable <group>. -- Create 3 peds. local ped1 = createPed(120, 5540.6654, 1020.55122, 1240.545) local ped2 = createPed(120, 5541.6654, 1021.55122, 1240.545) local ped3 = createPed(120, 5542.6654, 1022.55122, 1240.545) -- Set the parent of the 3 peds. setElementParent(ped1, group) setElementParent(ped2, group) setElementParent(ped3, group) -- Add an addEventHandler and use the <group> as <attachedTo> element. addEventHandler("onPedWasted", group, -- "onPedWasted" = serverside. "onClientPedWasted" = clientside. function () outputChatBox("One of my cute peds just died. ;'( No exceptions!") end) Code is untested, but the method is tested. Syntax for functions in example createElement syntax element createElement ( string elementType, [ string elementID = nil ] ) setElementParent syntax bool setElementParent ( element theElement, element parent ) addEventHandler syntax bool addEventHandler ( string eventName, element attachedTo, function handlerFunction, [ bool getPropagated = true, string priority = "normal" ] ) DO NOT disable getPropagated getPropagated: A boolean representing whether the handler will be triggered if the event was propagated down or up the element tree (starting from the source), and not triggered directly on attachedTo (that is, handlers attached with this argument set to false will only be triggered if source == this). If you disable this, children of the <group> element are not included. Make use of the element tree Element tree For applying addEventHandlers to elements created by the resource: Use: resourceRoot / getResourceRootElement For applying addEventHandlers to elements created by scripts of the resource: Use: getResourceDynamicElementRoot For applying addEventHandlers to elements created by maps of the resource: Use: getResourceMapRootElement I hope your code will be without... print(10^10^10^10) -- Print here: https://www.lua.org/cgi-bin/demo ...addEventHandlers in the future.
  2. If it is related to a dimension, try first to figure out which dimension it is. (Visible in Admin panel)
  3. Maybe it is related to a specific dimension?
  4. Using one addEventHandler on a group of elements?

     

    Answer:

    local group = createElement("groupMyCutePeds") -- Create a custom element and save it in to the variable <group>.
    
    
    -- Create 3 peds.
    local ped1 = createPed(120, 5540.6654, 1020.55122, 1240.545)
    local ped2 = createPed(120, 5541.6654, 1021.55122, 1240.545)
    local ped3 = createPed(120, 5542.6654, 1022.55122, 1240.545)
    
    -- Set the parent of the 3 peds.
    setElementParent(ped1, group)
    setElementParent(ped2, group)
    setElementParent(ped3, group)
    
    
    -- Add an addEventHandler and use the <group> as <attachedTo> element.
    addEventHandler("onPedWasted", group, -- "onPedWasted" = serverside. "onClientPedWasted" = clientside.
    function ()
        outputChatBox("One of my cute peds just died. ;'( No exceptions!")
    end)

    (untested muhahaha :D)

  5. You can do that, but that one is only visible in the ingame console.(Key to open: F8) If you want to make it visible in the server console, use print().
  6. Cpu leak? Don't you mean memory leak or high cpu usage? Performance speed can be tested with:(processing Speed>CPU) getTickCount() Memory leak can be checked with: http://luatut.com/collectgarbage.html
  7. You could check if your gamespeed is changed.
  8. Login as Admin Write the command: /debugscript 3
  9. I honestly do not understand why people add so many addEventHandlers, one on the parent is enough. local peds = { -- {model, x, y, z, rotZ, animblock, animname} {287, 1877.8, -2291.6, 13.6, 88, "SHOP", "SHP_serve_loop"}, {287, 1878.3, -2288.5, 13.6, 58, "SHOP", "SHP_serve_loop"}, {287, 1878.4, -2294.7, 13.6, 112, "SHOP", "SHP_serve_loop"}, {61, 1883.36, -2291.3, 13.6, 273.25, "COP_AMBIENT", "coplook_loop"}, } local pedsParent = createElement("pedsParent") local id = {} -- do not edit this table local function spawnPed(data, index) local p = createPed(data[1], data[2], data[3], data[4], data[5]) -- spawn ped with model, x, y, z and rotZ setPedAnimation(p, data[6], data[7]) -- set the ped's animation (you might want to change this into a setTimer statement if the animation doesn't apply correctly) setElementParent(p, pedsParent) id[p] = index -- save the ped's key of the table (so we can find his data) end local function handleRespawn() -- source: the ped that died -- id[source] = the key/id of the ped that died -- peds[id[source]] = data on the ped's key/id local index = id[source] if index then id[source] = nil -- clean up! Important, else data leak! local data = peds[index] -- find the ped's data if data then setTimer(destroyElement, 1000, 1, source) -- destroy ped after 1s setTimer(spawnPed, 1500, 1, data, index) -- use our cusom function to spawn ped after 1.5s end end end addEventHandler("onPedWasted", pedsParent, handleRespawn) -- attach handler for respawning -- initialize for i, ped in ipairs(peds) do -- iterate through all ped data spawnPed(ped, i) -- use our custom function to spawn ped end (credits to MrTasty)
  10. Debug your code properly. And make sure you enable screenshot upload in you MTA settings.
  11. Wiki: https://wiki.multitheftauto.com/wiki/PlaySound3D Creates a sound element in the GTA world and plays it immediately after creation for the local player. setElementPosition can be used to move the sound element around after it has been created. Remember to use setElementDimension after creating the sound to play it outside of dimension 0. Possible solution: local sound = playSound3D(MusicLink,Playerx,Playery,Playerz,true) local customMusicElement = createElement("customSoundElement") setElementParent(sound, customMusicElement) setElementParent(customMusicElement, MusicPlayer) addEventHandler("onClientRender", root, function () local customMusicElements = getElementsByType("customMusicElement", resourceRoot) for i=1, #customMusicElements do local customMusicElement = customMusicElements[i] local player = getElementParent(customMusicElement) if player then local soundElement = getElementChild(customMusicElement, 0) if soundElement then local x, y, z = getElementPosition(player) setElementPosition(soundElement, x, y, z) end end end end) It is a shame that not many people are using the element tree, since it saves you a lot table cleaning trouble.
  12. Afaik exactly the same way.
  13. function RecieveDatas(BoolTeam_, BoolChar_) if BoolChar_ then BoolTeam = BoolTeam_ BoolChar = BoolChar_ end end Parameters are local variables. They can't leave the function block. A simple underline _ after the variable makes it a different variable. function functionName(parameter1, parameter2) -- function block end
  14. Reading(+decrypting) and loading of the scripts takes probably much longer.
  15. If you are not inside a vehicle, there is no vehicle you can point at. To solve that issue: local vehicle = getPedOccupiedVehicle ( player ) if vehicle then -- the rest of the code end -------------------------------------------------- -------------------------------------------------- local vehicle = getPedOccupiedVehicle ( source ) if vehicle then -- the rest of the code end function czest(player,CommandName,wartosc) tonumber(wartosc) czas = wartosc end addCommandHandler("hz", czest) function czest(player,CommandName,wartosc) wartosc = tonumber(wartosc) if wartosc then czas = wartosc end end addCommandHandler("hz", czest)
  16. There was something strange about the <load> function. Afaik it didn't work for me because of some sort of security(MTA) problem. But that was a while ago, so it might be changed over time. Well, try first figure out where you crash starts. Probably line: 16 or 22 (Your default code) Run this. Write /doit and after that /testNext until your MTA starts to crash. local filePath = ":theScript/temporalCache/mapTest/water.lua" function hold (reason) iprint("Code pauses, reason:", reason) coroutine.yield() end local coroutineOfscriptReadaa function scriptReadaa() hold("Called function scriptReadaa") local file = fileOpen(filePath) if not file then error("Error opening File.") return end hold("File file is open") local data data = fileRead(file, fileGetSize(file)) hold("I did read the file") fileClose(file) hold("I did close the file") loadScript(data) hold("Everything seems to be OK") end addCommandHandler("doit", function () if not coroutineOfscriptReadaa then hold("Lets get started. I am your slave. Write: /testNext To go to the next step(s).") coroutineOfscriptReadaa = coroutine.create(scriptReadaa) coroutine.resume(coroutineOfscriptReadaa) end end) addCommandHandler("testNext", function () if coroutineOfscriptReadaa then local state, error = coroutine.resume(coroutineOfscriptReadaa) if not state then iprint("coroutine ended. You can use /doit again.") coroutineOfscriptReadaa = nil end end end) function loadScript(scriptData) hold("Called the loadScript function") local simpleContador = 0 local Xfunction = load(function() simpleContador = simpleContador + 1 return scriptData end) hold("Called the load function.") if Xfunction then hold("Xfunction does exist.") pCallPrev = Xfunction pcall(pCallPrev) hold("Called the pcall function") else outputDebugString("Couldn't load the script") return end triggerEvent("onClientResourceStart", resourceRoot, getResourceFromName("SHIscriptloader")) hold("triggerEvent onClientResourceStart has been called.") end --##water.lua containts the following function function startclient() setWaterColor(0, 0, 255) end addEventHandler("onClientResourceStart", resourceRoot, startclient) This example gives also answer on your question here:
  17. https://wiki.multitheftauto.com/wiki/OnClientClick > GUIEditor.button[1] function () https://wiki.multitheftauto.com/wiki/GuiStaticImageLoadImage > GUIEditor.staticimage[1] end
  18. Refresh the resources before you show the state of the resources. You might want to consider add a timer between the two actions. (refresh and show states) Try to give the resources the right to use the function refreshResources. (not sure if that is required for this function, but I think it is)
  19. Bass error 2 is a 'file open' error. It looks like you have access to the audio, while others do not. What if you upload the music to Youtube (private) and stream it from there?
  20. First of all check if the file exist. https://wiki.multitheftauto.com/wiki/FileExists It is possible that you downloaded the music file but the others did not because you changed some code.
  21. if isElement(atmObject["object"]) and getElementType(player) == "player" and atmObject["object"] == source then if isElement(atmObject["object"]) and getElementType(player) == "player" and atmObject["object"] == source then You forgot to add this. Note: playerWhoClicked is always a player. > and getElementType(player) == "player"
  22. You shouldn't be using string manipulate functions on JavaScript Object Notation, it might corrupt your file. Make sure you check your string after you generate it, afaik it should be converted correctly both ways. Debugging = understanding. iprint(tablaPrueba) local json = toJSON ( tablaPrueba ) iprint(json) iprint(fileR) fileR = fromJSON(fileR) iprint(fileR)
  23. @Overkillz Reminder.
  24. I wasn't 100% sure what you were asking for so, I thought I gave it a try. The example does it showing how long lua is busy processing the code in between. But maybe you want to show all messages within X amount of time. local myTableA = {"Message1","Message2","Message3","Message4","Message5","Message6","Message7","Message8"} local messageIndex = 1 local totalDuration = 5000 local durationTimer = math.max(math.ceil(totalDuration / #myTableA), 50) -- can't be lower than 50 ms else the timer doesn't work. addEventHandler("onClientResourceStart", resourceRoot, function () setTimer(function () outputChatBox(myTableA[messageIndex]) messageIndex = messageIndex + 1 end, durationTimer, #myTableA) end) Is this what you are looking for instead? (untested) P.s. loops can be delayed by pause the code, but that is dangerous because you have to manually resume them. (experts only recommended) If you want to know that, I am to happy to send you a link.
  25. local operationStartTime = getTickCount() --------------- -- operation -- --------------- outputChatBox("It took " .. (getTickCount() - operationStartTime) .. " ms to execute this operation.")
×
×
  • Create New...