How to get object that had been clicked on “on_input” function
You have to add a touch event in the input bindings.
Then you can access touch/click events using
--in the on_input function
if action_id == hash("touch") then
-- clicked. Do things.
end
An extension of this can be used to create click on game objects. Check out this example by @britzl here
There is more than one way of doing this.
Most simple examples:
Also you can use box2d triggers.
That is not this, i just wan’t to get the object that had been clicked not a click type or etc.
you cant, need to check the action.x and action.y and compare them in a for loop, each gameobject position.
ohhh my, that is terrible. Is defold so bad? In unity i can do it easy
you have more control of what you do, and you decide better the implementation of all.
(Im talking about How Defold is designed, it has some things at low level.)
But, you can use nodes, and you have a function for what you need
gui.pick_node(node, x, y)
and return true or false
Yeah, thank you. But if it is not gui, and is just random generated object? Or i need to stack all of my game object on collection and find one of them by coordinates? And one more question, what contains on “self”, and how i can see what array variable contains when i need it.
If you create a GO in editor you already know his url, if you create GO at runtime, when you create it, with for example a factory, the funtion you use, return the id of that object, you need to save all this referenceS
I explain more later, going to sleep.
You eed to get used to, here we havent getcomponent, or get children of a runtime created GO with others GO inside
Thank you so much!
No no, it can be done easily in Defold.
- For gui nodes (mostly used for UI but can be used for an entire game) there is gui.pick_node(node, x, y) that will return true when the specified x & y coordinates are within the bounds of the gui node. This does require a loop of the nodes you want to check for clicks.
- Attach a collision object to things you wish to be able to click on and one collision object that follows the mouse/finger. When you get a collision you have your mouse over and mouse out detection done. Combine this with pressed+release events and you’re done. This can be done more or less automatically for you if you use my defold-input library and the cursor script: GitHub - britzl/defold-input: Simplify input related operations such as gesture detection, input mapping and clicking/dragging game objects
You can learn about self
here:
You can iterate a Lua table using pairs() or ipairs() and you can use pprint() a table to see the contents (or set a debugger breakpoint).
ok
I’ve updated the input manual to mention the different approaches for input detection on objects:
I said the pick node, but its only for nodes…