Pick node isn't working

I’ve been working on a new gui and I have several successful similar executions of the pick node function but this one for whatever reason isn’t working. When I click on the node (and I checked the click coordinates do hit the bounding box) it doesn’t return true. Not sure what I’m doing wrong. Here’s the code:

local inventories = require("assets.modules.inventory")

local slot
local inventory_panel
local desc_text
local desc_name
local desc_count
local menu
local slots
local x_button

function init(self)
	slot = gui.get_node("slot_template")
	inventory_panel = gui.get_node("slot_box")
	desc_text = gui.get_node("description_text")
	desc_name = gui.get_node("item_name_text")
	desc_count = gui.get_node("item_count_text")
	menu = gui.get_node("menu")
	gui.set_enabled(menu, false)
	gui.set_clipping_visible(menu, false)
	slots = {}
	x_button = gui.get_node("x_button")
end

function final(self)
	-- Add finalization code here
	-- Remove this function if not needed
end

function update(self, dt)
	-- Add update code here
	-- Remove this function if not needed
end

function on_message(self, message_id, message, sender)
	if message_id == hash("show") then
		msg.post(".", "acquire_input_focus")
		gui.set_enabled(menu, true)
		gui.set_clipping_visible(menu, true)
		count = 0
		inventory = message.object
		pprint(inventories)
		pprint(message)
		pprint(inventories[message[object]])
		for i, x in pairs(inventories[inventory]) do
			slots[i] = {gui.clone_tree(slot), x}
			pprint(slots[i][1])
			gui.set_parent(slots[i][1].slot_template, inventory_panel, true)
			local parent_pos = gui.get_position(inventory_panel)
			local parent_size = gui.get_size(inventory_panel)
			gui.set_position(slots[i][1].slot_template, vmath.vector3(-parent_size.x/2, parent_size.y/2, 0))
			gui.set_scale(slots[i][1].slot_template, vmath.vector4(.1, .068, 0, 0))
			print(gui.get_position(slots[i][1].slot_template))
			gui.set_enabled(slots[i][1].slot_template, true)
			local data = slots[i][2]:getStats()
			print(data.name)
			gui.set_text(slots[i][1].slot_label, data.name)
			count = count + 1
		end
	end
end

function on_input(self, action_id, action)
	if action_id == hash("mb1") and action.pressed then
		if gui.pick_node(x_button, action.x, action.y) then
			gui.set_enabled(menu, false)
			gui.set_clipping_visible(menu, false)
		else
			for i, item in pairs(slots) do
				print(i)
				local slot_pos = gui.get_screen_position(item[1].slot_template)
				print("location data:\nslot position: "..slot_pos.x..", "..slot_pos.y.."\naction.x: "..action.x.." action.y: "..action.y)
				if gui.pick_node(item[1].slot_template, action.x, action.y) then
					print("success")
				end
			end
		end
	end
end

function on_reload(self)
	-- Add input-handling code here
	-- Remove this function if not needed
end

Really hard to tell why it isn’t working just from looking at the code. Can you share the project with me (bjorn.ritzl@king.com)?

game - Copy.zip (2.3 MB)
here you go.

1 Like

Ok, I’ve noticed a few things:

  1. The game_gui.gui and inventory_gui.gui are both parented to the same game object in main.collection. This becomes a problem since the input stack works on game objects and not on individual components. If you “acquire_input_focus” from game_gui.gui_script it is the gameobject that gets input and any user input is received on the game object and forwarded to any on_input() functions on components, meaning that both game_gui.gui_script and inventory_gui.gui_script will get calls to their respective on_input() functions.
  2. You use several global functions. Some examples are move(), show_inventory(), inventory_add() on player_script.script. Global functions can potential cause problems as your game grows as they can cause unintended side effects. For instance when you declare the same global function from multiple scripts. You should use local functions (ie using the local keyword)
  3. While gui scenes generally ignore everything that has to do with z-values it actually matters when doing node picking. A gui node with a z-value scale of 0 will not pass a gui.pick_node() test. Your cloned inventory nodes are given a z-value scale of 0: gui.set_scale(slots[i][1].slot_template, vmath.vector4(.1, .068, 0, 0))
1 Like

Thank you that fixed my problem.