# Click on game object

**URL:** <https://forum.defold.com/t/click-on-game-object/461>\
**Category:** Questions\
**Created:** [November 10, 2015, 12:14am UTC](https://forum.defold.com/t/click-on-game-object/461 "2015-11-10T00:14:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![genesislive](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/genesislive/32/187_2.png) [@genesislive](https://forum.defold.com/u/genesislive)\
**Post date:** [November 10, 2015, 12:14am UTC](https://forum.defold.com/t/click-on-game-object/461/1 "2015-11-10T00:14:55Z")

</div>

Hi guys!  
I’m struggling with a simple concept: detect if a og has been clicked.  
Now I’m using on\_input, but the event is dispatched even if I click outside the om itself…  
Any idea to solve this?

---

<div class="post-metadata">

**Author:** ![Johan](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan/32/127_2.png) [@Johan](https://forum.defold.com/u/Johan)\
**Post date:** [November 10, 2015, 7:52am UTC](https://forum.defold.com/t/click-on-game-object/461/2 "2015-11-10T07:52:46Z")

</div>

That topic is covered in this discussion:

> [@Is there any nice way of transforming a screen coordinate to world space?](https://forum.defold.com/t/is-there-any-nice-way-of-transforming-a-screen-coordinate-to-world-space/71/11):
>
> This should definitely be something built-into the engine. Just the fact that there’s no way to get the world-space position of where you clicked, if the window is ever any other resolution than the default, is rather tedious and limiting. I’ll work it out on my end, but it’s something Defold should have in the future

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 10, 2015, 12:12pm UTC](https://forum.defold.com/t/click-on-game-object/461/3 "2015-11-10T12:12:53Z")

</div>

I’m interpreting this also as a question of when a game object receives input. It seems from your question that you expect to get calls to on\_input only when touch events end up “inside” the game object. If this is what you expect then I’m afraid that’s not how it works. on\_input will be called for every go that has acquired input, unless a previous go has consumed the input event. Read more on how input works here: [http://www.defold.com/doc/input](http://www.defold.com/doc/input)

---

<div class="post-metadata">

**Author:** ![Jon\_Eriksson\_Aspehei](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jon\_Eriksson\_Aspehei](https://forum.defold.com/u/Jon_Eriksson_Aspehei)\
**Post date:** [November 20, 2015, 4:48pm UTC](https://forum.defold.com/t/click-on-game-object/461/4 "2015-11-20T16:48:37Z")

</div>

I too need to be able to click a game object. Its essential for my game.

In the GUI you can do this:

` if action\_id == input\_touch and action.pressed then  
local circle = go.get\_id()

```
 	if go.pick_node(btn, action.x, action.y) then
	print("I'm clicked")
	end
 
 end`

```

How can it not exist a similar function for game objects or sprites?

---

<div class="post-metadata">

**Author:** ![Ramose](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ramose/32/182_2.png) [@Ramose](https://forum.defold.com/u/Ramose)\
**Post date:** [November 21, 2015, 4:39am UTC](https://forum.defold.com/t/click-on-game-object/461/5 "2015-11-21T04:39:24Z")

</div>

I have same problem here. So there’s no direct way to detect if go is clicked/touched?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [November 21, 2015, 10:04am UTC](https://forum.defold.com/t/click-on-game-object/461/6 "2015-11-21T10:04:53Z")

</div>

No, you have to translate the click position to your world space, which is actually a 3D space as opposed to the GUI that is 2D.

---

<div class="post-metadata">

**Author:** ![Jon\_Eriksson\_Aspehei](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jon\_Eriksson\_Aspehei](https://forum.defold.com/u/Jon_Eriksson_Aspehei)\
**Post date:** [November 21, 2015, 1:03pm UTC](https://forum.defold.com/t/click-on-game-object/461/7 "2015-11-21T13:03:37Z")

</div>

Right now I’m doing like this:

```
`function on_input(self, action_id, action)
 if action_id == input_mouse and action.pressed then
 
 	local spritePos = go.get_world_position("myGo") -- getting my go position
 	local mouseX = action.x -- getting mouse position
 	local mouseY = action.y
 	local rightBoundary = spritePos.x - 200 --200 is half the width of my go
 	local leftBoundary = spritePos.x + 200
 	local upperBoundary = spritePos.y + 200
 	local downBoundary = spritePos.y - 200
 	
 	if mouseX >= rightBoundary and mouseX <= leftBoundary and mouseY >= downBoundary and mouseY <= upperBoundary then
 		print("clicked")
 	else
 		print("outside")
 	end
 
 end
end `

```

This works for right now since I don’t have a moving camera and don’t need to worry about worldspace coordinates. Just wish that there would be something like in action script:  
mySprite.addEventListener(MouseEvent.CLICK, myFunction);

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [November 21, 2015, 9:04pm UTC](https://forum.defold.com/t/click-on-game-object/461/8 "2015-11-21T21:04:35Z")

</div>

That would not be possible since you are free to render your world as you wish in your render script. However, we might be able to provide some helper functions that makes it easier to do these operations.

---

<div class="post-metadata">

**Author:** ![TechnoCentrist](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/technocentrist/32/514_2.png) [@TechnoCentrist](https://forum.defold.com/u/TechnoCentrist)\
**Post date:** [February 28, 2016, 4:57pm UTC](https://forum.defold.com/t/click-on-game-object/461/9 "2016-02-28T16:57:02Z")

</div>

Hi,  
well this is really bad - imagine you have bunch of sprites on the pile on the same coordinations, and you want to drag them away. That would not be possible without some serious workarounds.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 28, 2016, 9:48pm UTC](https://forum.defold.com/t/click-on-game-object/461/10 "2016-02-28T21:48:44Z")

</div>

This should be fairly trivial to implement yourself, especially if you don’t do anything special in the render script right? The thing is that as @sicher said, you can render your world as you wish since you have full control of the render script, and this makes it impossible to provide a solution that works out of the box for all kinds of render scripts. BUT we should probably provide some helper scripts at least.

---

<div class="post-metadata">

**Author:** ![Paul\_L](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/paul_l/32/1413_2.png) [@Paul\_L](https://forum.defold.com/u/Paul_L)\
**Post date:** [June 10, 2016, 8:42am UTC](https://forum.defold.com/t/click-on-game-object/461/11 "2016-06-10T08:42:18Z")

</div>

> [@britzl](#):
>
> BUT we should probably provide some helper scripts at least.

could you provide us any helper scripts to detect a mouse click on the particular game object?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 10, 2016, 1:52pm UTC](https://forum.defold.com/t/click-on-game-object/461/12 "2016-06-10T13:52:35Z")

</div>

I created a small example of how you could handle input on game objects containing one or more sprite components. It’s based around a lua module to which you can add sprites and callback functions when a sprite receives input. It takes into account game object position and scale and sprite scale and size. You need to manually provide the sprite position (offset) since that value can’t be accessed at run-time. I uploaded the example here:

> **[publicexamples/examples/click\_game\_object at master · britzl/publicexamples](https://github.com/britzl/publicexamples/tree/master/examples/click_game_object)**
>
> Examples for the Defold engine. Contribute to britzl/publicexamples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![RG1](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rg1/32/5106_2.png) [@RG1](https://forum.defold.com/u/RG1)\
**Post date:** [June 10, 2016, 2:26pm UTC](https://forum.defold.com/t/click-on-game-object/461/13 "2016-06-10T14:26:04Z")

</div>

Very useful codes. I should to do some refactor my current game in the next version and apply it to other games as well.

---

<div class="post-metadata">

**Author:** ![Paul\_L](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/paul_l/32/1413_2.png) [@Paul\_L](https://forum.defold.com/u/Paul_L)\
**Post date:** [June 11, 2016, 2:26am UTC](https://forum.defold.com/t/click-on-game-object/461/14 "2016-06-11T02:26:24Z")

</div>

> [@britzl](#):
>
> I created a small example of how you could handle input on game objects

thank you for your quick response, buddie

---

<div class="post-metadata">

**Author:** ![JAWhye](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jawhye/32/1229_2.png) [@JAWhye](https://forum.defold.com/u/JAWhye)\
**Post date:** [June 28, 2016, 1:01am UTC](https://forum.defold.com/t/click-on-game-object/461/15 "2016-06-28T01:01:55Z")

</div>

> [@sicher](#):
>
> That would not be possible since you are free to render your world as you wish in your render script.

97% of game developers don’t want to mess with a render script, they just want to be able to work on a game. (Actual number made up, but it wouldn’t surprise me if it’s that high or higher.) Not being able to easily know when a sprite/go is tapped/clicked is a horrible misstep, I think.

Defold is reminding me of Moai – very powerful, but more useful for _programmers_ and not run-of-the-mill game developers. I’m not saying you should have made another GameSalad, but there’s a “foundation” that people expect with a game engine and Defold only has scaffolding in some places.

Jay

PS - Moai is, for the most part, dead. Defold does have more muscle behind it than Moai did, but I’d hate to see Defold being just a niche player.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [June 28, 2016, 3:41am UTC](https://forum.defold.com/t/click-on-game-object/461/16 "2016-06-28T03:41:18Z")

</div>

IMO this example is a very good foundation for general 2D render script setups. It can maintain aspect ratio at any size, constrain the game area within the window, draw outside of the game window, and be able to translate screen input to game input. Examples like this just need more awareness so they can be adopted and devs can get on with the game making.

> **[publicexamples/examples/fixed\_aspect\_ratio at master · britzl/publicexamples](https://github.com/britzl/publicexamples/tree/master/examples/fixed_aspect_ratio)**
>
> Examples for the Defold engine. Contribute to britzl/publicexamples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [June 28, 2016, 7:30am UTC](https://forum.defold.com/t/click-on-game-object/461/17 "2016-06-28T07:30:10Z")

</div>

; TL/DR My suggestion of general input control is found in the bottom.

Two of the key aspects of Defold are flexibility and usability. The former requires an architecture which assumes very little of what you intend to do. The latter is often the opposite, too much genericness (a word?) confuses things and you see no clear path forward. In other words, we aim to provide you with something that _helps you along fast_, but does not impose a limit on what you can make. Our approach to solving this is to provide the flexibility through engine architecture, and usability through editor UX in combination with bootstrap data, which consists of the special case “builtin” content found in every project, as well as the template projects you can use to start things of. The grander vision is that when the library system has been more publically packaged, the users will contribute to this bootstrap data as well by providing sharable content. @britzl and others has already kicked this off by using github and other services, and of course this forum.  
You can compare this to a general purpose programming language, where the syntax/grammar/compilers corresponds to “engine architecture”, and system libraries, user libraries, and online stackoverflow snippets corresponds to the “bootstrap data”. (You could even argue that the compilers belong to the bootstrap data, but let’s not go there)  
The priority is always engine architecture first and bootstrap data second, as the architecture dictates what can follow. When it comes to bootstrap data, utility functions and the like, there is no “physical” boundary on what we can provide. But it suffers under the noise-factor, too much would also be confusing and annoying.  
We are now very well aware of that a lot of users want something that helps with the problem of input and screen/world space conversions. We have a few ideas around how to solve this in a good way, but it’s not yet clear which one outperforms the others, or if there is an even better way. What I’m trying to say is that we do intend to solve the “conveniences” for people too, but not until we have an idea that provides us with a cozy gut feeling, to minimise the noise-factor. And since this is something that can be achieved right now without too much work, it will naturally have lower priority than things that can’t be done at all. There is a bandwidth problem to deal with too. In reply to @JAWhye, it is not yet a horrible misstep, it just hasn’t been fixed yet.

This is my suggestion how to do it now, a sufficiently general solution to touch input you can use until Defold supports it out of the box:

- Everything you want to touch has a collision object with a specific mask, e.g “touch” or “interact”
- One script deals with the input.
- Using the view/projection matrices of the camera (which can be queried by this script every frame from the @render: message socket, which corresponds to the render script) it transforms the screen space coordinates into world space. I bet a lot of people have already done the screen/world conversion, if someone could provide a snippet for it it would be much appreciated. Until then, there is plenty of info online
- It then spawns a new game object with a collision object at this point (e.g. a sphere, which will account for touch radius).
- The new touch-gameobject will collect collisions and dispatch them back to the input-handling script.
- If it’s important to distinguish type on the object you touch, you could by convention also add a script property that defines this for them. Another approach could be to send messages to them about the interaction and they would, by convention, have means to deal with these messages.

---

<div class="post-metadata">

**Author:** ![JAWhye](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jawhye/32/1229_2.png) [@JAWhye](https://forum.defold.com/u/JAWhye)\
**Post date:** [June 28, 2016, 7:49am UTC](https://forum.defold.com/t/click-on-game-object/461/18 "2016-06-28T07:49:54Z")

</div>

I feel bad that I made you take so much time to generate that reply, but I do thank you for it – it helps calm my soul. 😉 And I hope you didn’t take my previous post as a flame – it would be awesome for Defold to become a leading game engine and while digging into it I kept coming up with (what I saw as) dead-ends.

Thanks for that suggestion on how to handle the problem for now. That looks like an interesting solution.

Jay

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [June 28, 2016, 11:16am UTC](https://forum.defold.com/t/click-on-game-object/461/19 "2016-06-28T11:16:14Z")

</div>

Absolutely fine, I just wanted to clarify where we are heading and why. 🙂 Keep posting the dead-ends in the forum and we can all pitch in with explanations, or suggestions for workarounds if the features are missing.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 28, 2016, 11:55am UTC](https://forum.defold.com/t/click-on-game-object/461/20 "2016-06-28T11:55:59Z")

</div>

> [@Ragnar\_Svensson](#):
>
> Everything you want to touch has a collision object with a specific mask, e.g “touch” or “interact”

I did this for the [Link and Slide example I posted a while back](https://forum.defold.com/t/link-and-switch/2220/2) and it works really great. It’s a very nice use of collision objects.

[Next page](https://forum.defold.com/t/click-on-game-object/461.md?page=2)
