Pick_node on clone_tree nodes issue

Hello there! I have these nodes (slice and sprite) that I want to duplicate, rotate and use to “touch” them:

nodes-screeshot

I have already duplicated them inside init() drawing nice 8 slices of a cheese:

	self.cheese = {
		node = gui.get_node("cheese"),
		slices = {},
	}

	local slice_original = gui.get_node("slice")
	for i = 1, 8 do
		self.cheese.slices[i] = gui.clone_tree(slice_original)
		-- each slide sum rotates -45º (clockwise) starting at 45º (1 o'clock)
		gui.set(self.cheese.slices[i].slice, "euler.z", -45 * (i - 1) + 45)
		gui.set_enabled(self.cheese.slices[i].slice, true)
		gui.set_color(self.cheese.slices[i].sprite, self.color.primary)
	end

The problem comes when trying to “pick” them:

function on_input(self, action_id, action)
	if action_id == touch and action.pressed and not self.solved then
		for _, slice in pairs(self.cheese.slices) do
			if gui.pick_node(slice.slice, action.x, action.y) then
				gui.set_color(slice.sprite, vmath.vector3(1))
			end
		end
	end
end

I expect to color just the slice picked but all of them became white.

I’ve thought different ways to make it work without cloning but I want to understand what I’m doing wrong. Thank you very much for your future response.

1 Like

Did not use radial gui much but when checking for mouse, it probably uses the size on the properties panel. Since the size is a rectangle, it is possible that they are overlapping. So, when clicked, it counts as you clicked all of them.

You could try using a smaller rectangle fitting on the slice but may not work perfectly

1 Like

So, if I understood correctly there is not a way to “touch” only a triangle, everything would be inside a touchable rectangle shape, right?

1 Like

I cannot say impossible as I don’t have much experience with Defold UI. Maybe @Insality @Pawel or @britzl could give a more definitive answer

2 Likes

There is only one small error:

Look, when you type in Lua:

self.my_table.something

It’s same as:

self.my_table["something"]

So when you type slice.slice it’s same as slice["slice"]

The function gui.clone_tree() returns a table with keys being hashes, not strings, look what is pprinted:

{ --[[0x7d3410438a00]]
  slices = { --[[0x7d3410438ae0]]
    1 = { --[[0x7d3410438b80]]
      hash: [slice] = unknown@(0, 0, 0),
      hash: [sprite] = box@(0, 0, 0)
    },
    2 = { --[[0x7d3410438e40]]
      hash: [slice] = unknown@(0, 0, 0),
      hash: [sprite] = box@(0, 0, 0)
    },
...

So you should access slice[hash"slice"] not slice.slice (so not slice["slice"]).

This tiny “hash” function call is what is making your code behave unexpected :wink:

Try:

if gui.pick_node(slice[hash"slice"], action.x, action.y) then
	gui.set_color(slice[hash"sprite"], vmath.vector3(1))
end

in on_input.

And:

		gui.set(self.cheese.slices[i][hash"slice"], "euler.z", -45 * (i - 1) + 45)
		gui.set_enabled(self.cheese.slices[i][hash"slice"], true)
		gui.set_color(self.cheese.slices[i][hash"sprite"], self.color.primary)

in init

Then it should work :wink:

Good tip is also to store hashes upfront, so make local sprite_hash = hash"sprite", etc :wink:

*And hash"sprite" is same as hash("sprite"), because Lua allow such syntactic sugar for function calls with single strings or table arguments.

Here’s small repro for you:

GuiCloneTest.zip (49.9 KB)

2 Likes

Thank you very much for your test sample. I still encountered some weirdness when picking, if I pick far from center it color the picked one but near center it colors all:

I simplified the project with only pies but issue is still there:

GuiCloneTest.zip (63.8 KB)

1 Like

Yeah, those are overlapping - don’t you want to have each slice separately? Perhaps use pie node for pick?

Yes, in the last test I uploaded I’m picking the pie:

if gui.pick_node(slice[hash"slice"], action.x, action.y) then
	gui.set_color(slice[hash"slice"], vmath.vector3(1))
end

Or at least I thought that… am I not pointing the pie? :thinking: