Can I program on touch event in Python?

I just want to know if I can do exactly the same things as coblocks in python, essentially how can I program something like “when a door is touched it must open” ?
This is NOT a python question, since I am a professional python programmer, but need to understand which is the interface we can use with cospaces. Otherwise we are obliged to make a hybrid solution, maybe using coblocks to set a variable when touched and then write a python blocks running when that variable changes?

Same question but in Javascript/Typescript?

Seriously @Stefan @Ilya.Shkuratov, Why is this so much harder than it should be.

It might vastly increase Cospaces adoption if you didn’t make things so hard through a lack of documentation. I just spent 30 minutes going through the API docs and can find nothing. I imagine that Claudio did the same.

If the answer is in the docs maybe you could make it searchable by adding keywords like “touch” or “collide” on the the page with the answer.

1 Like

Hi @Claudio_Pacchiega, @jlfjlf

there is no special touch event in CoSpaces API, you should search for onClick instead, that will be triggered also on touch.

In both Typescript and Python you can set the onClick handler via input namespace:

// Typescript
item.input.onClick(() => {
    Debug.log("Item clicked")

// Python
item.input.on_click(lambda : print("Item clicked"))
1 Like

Just a little hint for you.

While we trying to improve our documentation you may find useful and often more efficient to explore API through scripting editor itself. For example, in this case you could try to explore though code completion the members of item class and guess by the name and documentation hints where the desired functionality are located.

Hi @Ilya.Shkuratov

Attached example does not seem to work…

This activates when the camera “walks” over the Cuboid:

This does not work when the camera “walks” over the cuboid (works only when clicked):

Example here (just walk forward and turntable will be triggered when walked on)

Thanks for this quick and valuable information. I definitely suggest to put a “Hello click” rosetta stone in the three languages as a very simple tutorial step in documentation. While the answer is trivial the process for using it is not and can put down some people willing to use cospace for intermediate coding.
I want to use CoSpaces as a valuable mean for teaching coding to kids and students, showing them how they can pass from blocks to python (typescript is a bit strange and difficult language to learn at start).

Hi @jlfjlf

So, what you really want is collision handler, and not ‘touch’. ‘Touch’ is this context typically means the ‘touch event’ that is triggered when the user presses on the touch screen.

Here is an example how to set a collision handler:

item.onCollisionEnter((collidedItem) => {

Here is respective documentation

Thank you @Ilya.Shkuratov

That worked perfectly!

…wish the docs were more searchable:)