Table Changes in the Wrong Index (SOLVED)

function build_tooltip(owner,order,chars,font,pos,size,valuez,buttonz)
	print(tostring(owner) .. " ORDERED TOOLTIP")
	tooltips = tooltips + 1
	margin = 10 * chars
	tooltip = gui.new_box_node(pos,size)
	TListTooltips[tooltips] = tooltip; TListOwners[tooltips] = owner
	vItems = get_table_length(valuez)
	bItems = get_table_length(buttonz)
	
	bPositions = {}
	bPosition = pos
	bSize = vmath.vector3(size.x,2*chars,0)
	row = 0
	column = 1
	thisB = 1
	
	if bItems > 1 then
		bSize.x = bSize.x / 2
	end
	
	for b=0,bItems - 1,1 do
		if b == 2 then
			b = 0; row = row + 1
			print("SYSTEM:TOOLTIP: New button column")
		end
		bPosition.x = bPosition.x + (bSize.x * b)
		bPosition.y = bPosition.y - (row*(2*chars))
		if bPositions[thisB] == nil then
			bPositions[thisB] = bPosition -- THE PROBLEM CHILD
		end
		pprint(bPositions)
		print("SYSTEM:TOOLTIP: New button position: " .. bPositions[thisB])
		thisB = thisB + 1
		print("SYSTEM:bPositions.this = " .. thisB)
	end

When I use this code it’s supposed to format whatever amount of buttons I order for the tooltip and place them nicely in the right position. The problem I’m having is that, for some reason, the variable bPositions[1] is set to a position, and then bPositions[1] AND [2] are both set to a second position. On the second time around, (because I only ordered two buttons on this test) the table should only be changed in it’s 2nd index. I’ve checked and rechecked, but somehow both are being changed at the same time, when I only set one.

When I set the position into the table, the variable in that table is now static, correct? As in, if the position changed outside the table, the position inside the table would be kept separate and would stay the same.

Nope. vector3 is userdata, reference type.

You can create new vector everytime: bPositions[thisB] = vector3(bPosition)

3 Likes

That would explain it. Thank you very much.