Hi,
First, I want to thank all of you because the community is really helpful and supportive.
Sorry, again, for newbie question: How to get a component property of a game object inside a collection? I mean If the script is outside the collection.
Let me explain: I’m making a hangman game, and I have a collection named “keyboard”, with letters as game objects inside it. The player can click on the letters.
I’m searching for a way to get the letter inside the game object that the player clicked, so I can compare if it matches with the letters of the answer. Already done the comparison part.
I think it will be more clearer with the images below.
Thank you in advance for your answers.
I see two potential solutions:
-
Collision based. Using collision detection (e.g. using defold-input to determine which object you clicked on. Then use a table with mappings from url to letter. That way you can lookup the letter given the url of the other collision object. I feel it is overkill in your case.
-
Given that the letters are laid out in a grid, you can convert the input coordinate to “keyboard coordinates”. Then you can look up that keyboard coordinate in a table.
Another solution would be to create the keyboard using a GUI instead and use gui.pick_node() to check which letter you clicked. Put all letter nodes in a list and compare them one by one:
function init(self)
local keys = {
a = gui.get_node("key_a"),
b = gui.get_node("key_b"),
...
z = gui.get_node("key_z"),
}
self.key = keys
end
function on_input(self, action_id, action)
if action_id == hash("click") and action.pressed then
for letter, node in pairs(self.keys) do
if gui.pick_node(node, action.x, action.y) then
print("pressed key with letter ", letter)
end
end
end
end
Thank you, I think I’ll go for the GUI, but the input coordinate gave me ideas. Maybe for another game after this one