Jump to content

[Help pls] (for) loop


Recommended Posts

hi guys

I am trying to get all the blips using the following code

 

for i,v in ipairs(getElementsByType("blips")) do 
  -- todo
end

My problem is that I don't want to get repeated blips

For example, if we make 2 blips with ID icon 10 using the 'createBlip' function, it will output this icon twice.

createBlip(0,0,0,10)
createBlip(1,1,1,10)

for i,v in ipairs(getElementsByType("blips")) do
	local id = getBlipIcon(v)
	outputChatBox("ID Icon : "..id)
	--If these two blips are the only blips mad
	--It becomes our output . . .
	-- ID Icon : 10
	-- ID Icon : 10

	
end

I don't want it to receive blips with the same ID icon, that is, if we have 5 blips with ID icon 10, give us only one of the 5.

Link to comment

You can simply keep track of the ids by adding them to a table and then check against that table to see if it's already seen that id.

local ids = {}
for i,v in ipairs(getElementsByType("blips")) do
    local id = getBlipIcon(v)
    if not ids[id] then
        ids[id] = true
        outputChatBox("ID Icon : "..id)
    end
end
ids = {} -- clear it if necessary (depends on your application)

You'll probably want to put this inside a function so that you can trigger it again later, or have the function return the unique ids. But this all depends on the application.

  • Thanks 1
Link to comment
3 hours ago, amirmahdi said:

hi guys

I am trying to get all the blips using the following code

 

for i,v in ipairs(getElementsByType("blips")) do 
  -- todo
end

My problem is that I don't want to get repeated blips

For example, if we make 2 blips with ID icon 10 using the 'createBlip' function, it will output this icon twice.

createBlip(0,0,0,10)
createBlip(1,1,1,10)

for i,v in ipairs(getElementsByType("blips")) do
	local id = getBlipIcon(v)
	outputChatBox("ID Icon : "..id)
	--If these two blips are the only blips mad
	--It becomes our output . . .
	-- ID Icon : 10
	-- ID Icon : 10

	
end

I don't want it to receive blips with the same ID icon, that is, if we have 5 blips with ID icon 10, give us only one of the 5.

function getOneBlipByID (id)
	for i, v in ipairs (getElementsByType("blips")) do
    	if getBlipIcon(v) == id then
      		return v
      	end
   	end
  	return false
end

createBlip (0, 0, 0, 10)
createBlip (1, 1, 1, 10)
createBlip (2, 2, 2, 10)

getOneBlipByID (10)

 

  • Thanks 1
Link to comment
19 hours ago, AngelAlpha said:
function getOneBlipByID (id)
	for i, v in ipairs (getElementsByType("blips")) do
    	if getBlipIcon(v) == id then
      		return v
      	end
   	end
  	return false
end

createBlip (0, 0, 0, 10)
createBlip (1, 1, 1, 10)
createBlip (2, 2, 2, 10)

getOneBlipByID (10)

 

Thanks for the help But it returns false

19 hours ago, Tails said:

You can simply keep track of the ids by adding them to a table and then check against that table to see if it's already seen that id.

local ids = {}
for i,v in ipairs(getElementsByType("blips")) do
    local id = getBlipIcon(v)
    if not ids[id] then
        ids[id] = true
        outputChatBox("ID Icon : "..id)
    end
end
ids = {} -- clear it if necessary (depends on your application)

You'll probably want to put this inside a function so that you can trigger it again later, or have the function return the unique ids. But this all depends on the application.

Thanks for the help But this program destroys all blips with ID 10

 

I actually want to For example, if we have made 10 blips in the map And 5 of them have ID icon 10 and the other 5 have different ID icon.

Our output will be as follows

createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)

createBlip(0,0,0,2)
createBlip(0,0,0,7)
createBlip(0,0,0,5)
createBlip(0,0,0,15)
createBlip(0,0,0,26)

for i,v in ipairs(getElementsByType("blips")) do
	local id = getBlipIcon(v)
	outputChatBox("ID Icon : "..id)
end

--output
-- ID Icon : 10
-- ID Icon : 10
-- ID Icon : 10
-- ID Icon : 10
-- ID Icon : 10
-- ID Icon : 2
-- ID Icon : 7
-- ID Icon : 5
-- ID Icon : 15
-- ID Icon : 26

But I want the output to be as follows

createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)
createBlip(0,0,0,10)

createBlip(0,0,0,2)
createBlip(0,0,0,7)
createBlip(0,0,0,5)
createBlip(0,0,0,15)
createBlip(0,0,0,26)

for i,v in ipairs(getElementsByType("blips")) do
	local id = getBlipIcon(v)
	outputChatBox("ID Icon : "..id)
end

--output
-- ID Icon : 10
-- ID Icon : 2
-- ID Icon : 7
-- ID Icon : 5
-- ID Icon : 15
-- ID Icon : 26

The program removed blips with the same icon id !!!

Link to comment

I Solved

thanks for @Tails @AngelAlpha

 

here is my code

idx = {}
newArray = {}
createBlip(0,0,0,10,2,255,255,255)
createBlip(100,100,100,10,2,255,255,255)
createBlip(200,200,200,10,2,255,255,255)
createBlip(400,400,400,1,2,255,255,255)
createBlip(300,300,300,1,2,255,255,255)


for k, v in ipairs(getElementsByType("blip")) do
	local id = getBlipIcon(v)
	table.insert(idx,{iconid1=id,element1=v})

end


local function inArray(arr, elemesnt) 
    for _, value in ipairs(arr) do 
        if value.iconid == elemesnt then
            return true
        end
    end
    return false 
end

local function removeDuplicates(arr,same)
    
    for i, elemesnt in ipairs(arr) do
        if not inArray(newArray, elemesnt.iconid1) then 
            table.insert(newArray, {iconid=elemesnt.iconid1,element=elemesnt.element1}) 
        end
    end
     
end

addCommandHandler("removedup",function()
	removeDuplicates(idx,10)
	setTimer(function()
		for _, elemesnt in ipairs(newArray) do
			outputChatBox(tostring(elemesnt.iconid))
          	--output is
          	--10
          	--1
		end
	end,2000,1)
end)

 

Link to comment
  • Tut locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...