iPollo Posted May 21, 2021 Share Posted May 21, 2021 i'm experiencing lag running this code. Some one can help me to improve it or give me the hint to follow the best way to do it? in short the code does: it takes all elements in the determined range and checks if it is a collectable item, after that it adds the values of that item in a temporary table. When the player is next to the item (or items) the item's icon is drawn on it. Everything works ok, but the fps drops and it frozes slightly --========================================================================== -- Declarações --========================================================================== -- VAR: Booleanas local isPlayerCloseToItems = false -- VAR: Inteiros local playerCloseItemCount = 0 -- VAR: Tabela dos itens próximos local PLAYER_CLOSE_ITEMS = {} --========================================================================== -- Eventos --========================================================================== addEventHandler("onClientResourceStart", resourceRoot, function() lootCheckTimer = setTimer(lootCheck, 250, 0) print("cu") end) addEventHandler("onClientRender", root, function() if(isPlayerCloseToItems) then --dxDrawImageOnElement(0, 0, 3, texture, 10, -0.8, 1) for i = 1, playerCloseItemCount, 1 do for i = 1, 6, 1 do dxDrawItemIcon(PLAYER_CLOSE_ITEMS[i][3], PLAYER_CLOSE_ITEMS[i][4], PLAYER_CLOSE_ITEMS[i][5] - 1, PLAYER_CLOSE_ITEMS[i][6]) end end --dxDrawMaterialLine3D(0, 0, 2.7, 0, 0, 2.5, texture, 0.2, tocolor(255, 255, 255, 150)) end end) --========================================================================== -- Funções --========================================================================== function lootCheck() local x, y, z = getElementPosition(localPlayer) local closeElements = getElementsWithinRange(x, y, z, 10) local itemCount = 0 playerCloseItemCount = 0 isPlayerCloseToItems = false -- Limpar a tabela antiga count = #PLAYER_CLOSE_ITEMS for i=1, count do PLAYER_CLOSE_ITEMS[i]=nil end -- Pega o id dos itens próximos for key, value in pairs(closeElements) do if(getElementData(value, "isItem")) then print("CHAMADA INEXPERADA") playerCloseItemCount = playerCloseItemCount + 1 PLAYER_CLOSE_ITEMS[playerCloseItemCount] = {getElementData(value, "itemid"), getElementData(value, "itemName"), getElementData(value, "itemX"), getElementData(value, "itemY"), getElementData(value, "itemZ"), dxCreateTexture(getElementData(value, "itemIcon"))} end end if(playerCloseItemCount ~= 0) then isPlayerCloseToItems = true else isPlayerCloseToItems = false end for i = 1, playerCloseItemCount, 1 do for k = 1, 6, 1 do print(PLAYER_CLOSE_ITEMS[i][k]) end end end function dxDrawItemIcon(x, y, z, iconTexture) dxDrawMaterialLine3D(x, y, z + 2.7, x, y, z + 2.5, iconTexture, 0.2, tocolor(255, 255, 255, 150)) end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now