Jump to content

Issue Index-ing table


Overkillz

Recommended Posts

Hello dear guys, today I've just got an issue index-ing some values inside a table. More than an issue, its a question.

Why Index-ing is not working properly. Sometimes I need to edit the layer-1 (Position 1) and sometimes I need to edit the 15th (Position 15) and might form 2-14 they arre not exists

addLayer(1,"arrowdown")
addLayer(2,"arrows")
addLayer(3,"curve")

local tableTest1 = {}

function addLayer(slotNo,layerName)
	if not slotNo or not layerName then return end
	
	tableTest1[tonumber(slotNo)] = {name = layerName, active = true}
	iprint(tableTest1)
end

mta-screen_2019-10-16_22-21-33.png

I need to keep them in the order that Im adding them to the table.

Sometimes the index is visible in the table and idk when I add a layer from client.Lua to addLayerClient.Lua 

Sometimes even the first index isn't appeared and the rest of them yes. and the structure in this case would be

INFO {{
    active = true,
    name = "addowdown"
    },
  	[2] = {
  	active = true,
    name = "whatever"
  	},
  	[8] = {
  	active = true,
    name = "whatever2"
  	}

I know there are several ways to fix it like adding an id to each table inserted and later use table.sort or things like that, but I would like to know whats wrong with this.

I hope you can bring me a hand. Best Regards.

Link to comment

I think the reason is, that the first table has the index 1, but theres no need to show it by the iprint, beacuse it is trivial. Now the reason is, that the 2. and the 8. indexes has the printed key, that it is want to show, that beetwen the 2 key there are unused keys. I if you loop over the table by ipairs, you will get a key argument also for the first table.

Link to comment

I already know about looping them using pairs to prevent breaks when an index is not detected between 2 numbers like the (2nd & 8th)

But, here is my real question. For example. Im drawing a list of images which are stored in a table. But ...

In example, I add the first img with index 1, layer I will add another img with index 8 and to finish I will add the last img with index 3.

The img with the index 8 should be drawn over the index 1 & 2, but sometimes. IDK why, the img with index 2 is over drawing the img with the index 1 & 8.

Example Code.

Spoiler

local imgTable = {}

function drawImages()
	for i,img in pairs(imgTable) do
		dxDrawImage(posX,posY,sizeX,sizeY,img.path)
	end
end

function addImgToTable(cmd,index,imgPath)
	imgTable[tonumber(index)] = {path = imgPath, active = true}
end
addCommandHandler("addimg",addImgToTable)

 

Regards.

Link to comment

Because there is difference beetween pairs and ipairs. And yes, you are find out the problem, what is it. When you use pairs the sequence will be not the sequence of the ordered number keys. 

Use the ipairs, and you might have what you want to earn. 

  • Like 1
Link to comment

With ipairs it will stop looping as soon as it encounters a non-existent index. I.e. for 1,2,8 will only execute for 1 and 2, and because 3 does not exist it will break out of the loop. You'll need to use an auto-indexed table with the layer priority as a part of the layer data table, and table.sort after every addition

Edited by MrTasty
  • Like 1
Link to comment
2 hours ago, MrTasty said:

With ipairs it will stop looping as soon as it encounters a non-existent index. I.e. for 1,2,8 will only execute for 1 and 2, and because 3 does not exist it will break out of the loop. You'll need to use an auto-indexed table with the layer priority as a part of the layer data table, and table.sort after every addition

Yeah, actually you are right... I overlooked the fact...

I think sorting the table every time when a new layer filled with data can lose the data of the layer index. I think, it's can be a good solution, if we specify a maximum number for the layer, like 100.
Now with this information we can do like this:

 

local imgTable = {}

function drawImages()
	for i=1,100 do
		if imgTable[i] then
			dxDrawImage(posX,posY,sizeX,sizeY,imgTable[i].path)
		end
	end
end

function addImgToTable(cmd,index,imgPath)
	imgTable[tonumber(index)] = {path = imgPath, active = true}
end
addCommandHandler("addimg",addImgToTable)

 

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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