-
Posts
6,097 -
Joined
-
Last visited
-
Days Won
218
Everything posted by IIYAMA
-
local's are faster, because they are only active inside of an area. Real world example: It is something similar as buying things online in your own country(can be done within 24-hour) or order them from another(takes more than a day). Note: default MTA default functions are as fast as if you declare them as a local. Thought I would only declare functions as local if they are ONLY used for the code below or in case of onClienRender. There are two variants and the main difference while using is this: iprint(type(exampleFunction)) -- nil local function exampleFunction () iprint(type(exampleFunction)) -- function end iprint(type(exampleFunction)) -- function iprint(type(exampleFunction)) -- nil local exampleFunction = function () iprint(type(exampleFunction)) -- nil end iprint(type(exampleFunction)) -- function Fix for this one: iprint(type(exampleFunction)) -- nil local exampleFunction iprint(type(exampleFunction)) -- nil exampleFunction = function () iprint(type(exampleFunction)) -- function end iprint(type(exampleFunction)) -- function For more information: https://www.lua.org/pil/6.2.html
-
At the place of creation > GUI, not when you click the button.
-
The second one is using the ipairs function: https://www.lua.org/pil/7.3.html Benefit of the ipairs: ipairs returns the value as wel as the index. (no need to index on the next line as with the normal for loop) ipairs stops if there is no item at a given index: (not always a benefit thought) table = { 1, nil, -- < stops here 3 } Benefits of the normal for loop: Faster because it isn't using a function to loop. (functions are relative slow in compare to the rest of the code.) Can loop through nil values if required. table = { 1, nil, -- < keeps going! 3 } Can loop with different steps: for i=2, 10, 2 do --[[ 2 4 6 8 10 ]] end Can loop invert: for i=10, 1, -1 do --[[ 10 9 8 7 6 5 4 3 2 1 ]] end With other words there is no reason to use ipairs in my opinion.
-
Once you understand the basics of variables, functions, debugging, tables and loops, the only thing that is your way is your mental model and your creativity. Which is something that has to grow over time. The fastest way to get at an higher level is to know exactly what your code does, which is achieve able by debugging (or lots of reading). So for example this code looks complex: (useless code btw) thisTable = {"randomStuff", [20] = "randomStuff", "randomStuff", [30] = "randomStuff"} function functionName (parameter1, parameter2) local newTable = {} for i=1, 30 do newTable[#newTable + 1] = (parameter1[1] or "nothing here") .. " | " .. math.random(1000) .. " | " .. parameter2 end return newTable end local result = functionName (thisTable, "text") Adding debug lines: thisTable = {"randomStuff", [20] = "randomStuff", "randomStuff", [30] = "randomStuff"} iprint("thisTable:", thisTable) -- show table structure function functionName (parameter1, parameter2) iprint("parameter1:", parameter1, ",parameter2:", parameter2) local newTable = {} iprint("newTable:", newTable) local countLoops = 0 for i=1, 30 do iprint('What does >(parameter1[1] or "nothing here")< do? ', (parameter1[1] or "nothing here")) newTable[#newTable + 1] = (parameter1[1] or "nothing here") .. " | " .. math.random(1000) .. " | " .. parameter2 countLoops = countLoops + 1 end iprint("Loop has executed:", countLoops, "times.") return newTable end iprint("functionName:", functionName, ", type value:", type(functionName)) local result = functionName (thisTable, "text") iprint(result) -- show end result But after debugging: (You have to do that yourself, post result please. Which is automatic your motivation towards me.)
- 10 replies
-
- 2
-
-
-
I am sorry, I do not have time for personal / private coaching. If you start from bottom you have to start with LUA and not with MTA scripting. There are enough basic tutorials around.
- 10 replies
-
- 1
-
-
No it is not, we came one step closer to a fix, because it solved multiple issues. Did you debug the place of creation (of the data)? Please show me your debug skills.
-
-- ... elseif source == preview then if guiGridListGetSelectedItem(carGridList) == -1 then outputChatBox("Select one vehicle!", 255, 0, 0) return end -- Retrieve data local modelID = guiGridListGetItemData(carGridList, guiGridListGetSelectedItem(carGridList), 1) local price = guiGridListGetItemData(carGridList, guiGridListGetSelectedItem(carGridList), 2) -- modelID = tonumber(modelID) if modelID then local x, y, z = getElementPosition(localPlayer) local vehicle = createVehicle(modelID, x, y, z - 10) local ptimer = setTimer(function(vehicle) if isElement(vehicle) then destroyElement(vehicle) end end, 45000, 1, vehicle) else iprint("modelID is a",modelID) end elseif source == cancel then -- ... You might want to receive the data at the right place.
-
This might be a solution, yet maybe not what you want. https://wiki.multitheftauto.com/wiki/SetElementVisibleTo (serverside)
-
Changing the type to share will not solve the issue, because that means that the function should be created on both sides of the resource: tws-main Thought it will look like it is working because getPlayerMoney already exist as a MTA default function. There is a tutorial for this issue:
-
local money = exports["tws-main"]:getPlayerMoney() iprint(money) I do not know that resource, if you check the meta.xml file of tws-main you can see the export functions that are available.
-
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 :(
-
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
-
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
-
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)
-
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
-
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()
-
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
-
Please read the first sentence of the dx function page: https://wiki.multitheftauto.com/wiki/DxDrawLine
-
Images are still there. I want to see less code, not more lol.
-
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
-
show me the code you are now using.
-
Did you checked the images? (they can be very laggy if they are drawn from files + being large)
