How would I go about deleting a group of gui elements

currently I’m trying to delete all of gui heart elements at once to accomedate the heart system in my previous posts, I’ve tried running this script, with idlist being each heart being added to a list
image

however I’m getting this error

image

I have no idea what this means.

What’s at line 33?

1 Like

You’re starting the loop at i = 0, however Lua indexes arrays starting at i = 1.

6 Likes

You only supplied the first line of the error message. It’s hard to guess what the rest of the message says.

I guess this error throws when the function in question is called for a second time.
The first call deletes the nodes but the table is untouched and still holds the values.
In the second run, the function throws the error because the nodes have been already deleted.

A short test (please correct me if there is dodgy code in it):

local boxes_array = {}
local input_counter = 0
local pos = 50

-- make some box nodes
local function make_boxes()
	for i = 1, 4 do
		local box = gui.new_box_node(vmath.vector3(pos, pos, 0), vmath.vector3(50, 50, 0))
		gui.set_color(box, vmath.vector4(1,1,1,1))
		pos = pos + 50
		gui.set_position(box, vmath.vector3(pos, pos, 0))
		table.insert(boxes_array, box)
	end
	-- reset pos so next clones are positioned like the previous ones
	pos = 50
	print("making boxes")
end

-- delete the box nodes
local function delete_boxes()
	for i = 1, #boxes_array do
		gui.delete_node(boxes_array[i])		
	end
	print("deleting boxes")
end

function init(self)
	msg.post(".", "acquire_input_focus")
	make_boxes()
end

function on_input(self, action_id, action)
	if action_id == hash("touch") then
		if action.pressed then
			input_counter = input_counter + 1
			print("input number: "..input_counter)
			print("lenght table: "..#boxes_array)
			delete_boxes()
			--make_boxes()
			for i = 1, #boxes_array do
				print(boxes_array[i])
			end
			print()
		end
	end
end

init makes the boxes:

call 1: boxes deleted, table still holds the values, call 2: error throws

Solution: remove the table values, eg. like this:

-- delete the box nodes
local function delete_boxes()
	for i = 1, #boxes_array do
		gui.delete_node(boxes_array[i])		
		boxes_array[i] = nil
	end
	print("deleting boxes")
end

output after call 2: