Jump to content

IIYAMA

Moderators
  • Posts

    6,062
  • Joined

  • Last visited

  • Days Won

    208

Everything posted by IIYAMA

  1. Variables are shared. Unless They are declared as locals. local localVariable Or if they are parameters. (which are also locals) function thisFunction (parameter1, parameter2) end A local variable can not leave it's block, neither it's file(which becomes it's block). http://lua-users.org/wiki/ScopeTutorial -- start file block function exampleFunction () -- start function block if true then -- start IF block -- ... -- end IF block elseif true then -- start ELSEIF block -- ... -- end ELSEIF block else -- start ELSE block -- ... -- end ELSE block end -- end function block end -- end file block -- start file block -- function exampleFunction () -- start function block if true then -- start IF block -- ... -- end IF block elseif true then -- start ELSEIF block -- ... -- end ELSEIF block else -- start ELSE block -- ... -- end ELSE block end -- end function block end -- -- end file block local variable -- I can not leave :(
  2. local r, g, b, alpha = getMarkerColor ( CPMarker[i]) setMarkerColor ( CPMarker[i], r, g, b, 128) local r, g, b, alpha = getMarkerColor ( CPMarker[i]) setMarkerColor ( CPMarker[i], r, g, b, 0) setElementAlpha does not work for markers because they have their own method to set the alpha. (which is actually not really consistent to be honest ) bool setMarkerColor ( marker theMarker, int r, int g, int b, int a ) Required Arguments theMarker: The marker that you wish to set the color of. r: The amount of red in the final color (0 to 255). g: The amount of green in the final color (0 to 255). b: The amount of blue in the final color (0 to 255). a: The amount of alpha in the final color (0 to 255). https://wiki.multitheftauto.com/wiki/SetMarkerColor
  3. First you have to figure out why it is a nil. Check if the data is correctly set to the correct element. Please debug also at the place where you set the data. But to make you do not receive that warning: modelID = tonumber(modelID) -- You already declared it as a local, so you don't have to re-do that. if modelID then --... create the vehicle here else iprint("modelID is a", modelID) end
  4. That is right. (lots of work to make it perfect)
  5. Wut?, using a coroutine for attaching elements. For your metalmodel, a coroutine is a functionality that will enable you to pause code, so you can prevent it from crash while it takes to long fulfil it's purpose. So for a better understanding, I can compare it to the real world: Context: I am writing you this reply, but it is already getting late and I have to go to bed. If I do not submit this reply, it will save where I stopped and I can continuing it later on. I decided to submit it (even though I didn't finish it). This error means that I want to continuing writing my message, but I already submitted it so that is not possible any more. (Editing posts is something else)
  6. if getVehicleType(v[1]) ~= "Plane" or getVehicleType(v[1]) ~= "Helicopter" or getVehicleType(v[1]) ~= "Boat" then if getVehicleType(v[1]) ~= "Plane" and getVehicleType(v[1]) ~= "Helicopter" and getVehicleType(v[1]) ~= "Boat" then For improved performance: (199% faster) local vehicleType = getVehicleType(v[1]) if vehicleType ~= "Plane" and vehicleType ~= "Helicopter" and vehicleType ~= "Boat" then And even faster: local notAcceptableVehicles = {Plane = true, Helicopter = true, Boat = true} -- function () -- ... local vehicleType = getVehicleType(v[1]) if not notAcceptableVehicles[vehicleType] then -- ... -- end
  7. Put a debug or outputChatBox function on top of the code you are loading, just to be sure.(onClientResource start event doesn't trigger once loaded, only for other resources) If that doesn't work, debug results of the pcall function: local status, err = pcall(loaded) iprint("status:",status,"error:",err) And don't be afraid to call the function manually: loaded()
  8. function createcarShopwindow() --... local carShopwindow = guiCreateWindow(left, top, windowW, windowH, "Carshop", false) --... return carShopwindow end local values can't just leave the function block. They are stuck in their scope. The return statement can return any values to the place where you called the function. local carShopwindow = createcarShopwindow() Calling the function createcarShopwindow and receiving returned values. Please follow this tutorial for a better understanding about local scopes. http://lua-users.org/wiki/ScopeTutorial
  9. Please read the first sentence of the dx function page: https://wiki.multitheftauto.com/wiki/DxDrawLine
  10. Images are still there. I want to see less code, not more lol.
  11. 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 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) Edited. (found the problem) I tested it. (you should test it too) It seems to be the load function. Nor sure why or what. I too recommend you: + environment set: https://www.lua.org/manual/5.1/manual.html#pdf-setfenv
  12. show me the code you are now using.
  13. Did you checked the images? (they can be very laggy if they are drawn from files + being large)
  14. Added a tutorial:

     

  15. 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.
  16. If it is related to a dimension, try first to figure out which dimension it is. (Visible in Admin panel)
  17. Maybe it is related to a specific dimension?
  18. 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)

  19. 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().
  20. 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
  21. You could check if your gamespeed is changed.
  22. Login as Admin Write the command: /debugscript 3
  23. 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)
  24. Debug your code properly. And make sure you enable screenshot upload in you MTA settings.
×
×
  • Create New...