How to make the condition of collision with an object and pressing a key?

Hi all.
I am slowly sorting out the engine and have run into one problem that I cannot solve.

How to make the condition of collision with an object and pressing a key (for example, to enter the door) work.

I do it in the object script:

function on_input(self, action_id, action)
msg.post(".", "acquire_input_focus")
end

local function obj1(self, action_id, action)
if action_id == hash("up") and action.pressed then
-- print("obj1 and UP")
msg.post("game:/loader", "new_level", {})
end
end

function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") then
-- print("obj1 ok")
obj1(self, action_id, action)
end
end

But it doesn’t respond in any way to clicking up. What am I forgetting?

hi and welcome.

on_input is triggered after the script acqured input focus. so you wont get any input action here.(usually we call acquire_input_focus in init).

after that you can process input action in on_input, like what you do in obj1.

I think you should check the mannul for more information.

Just think in terms of assynchron logic:

function init(self)
  msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
  if action_id == hash("up") and action.pressed then
    if self.door_triggered then
       msg.post("game:/loader", "new_level", {})
     end
  end
end

function on_message(self, message_id, message, sender)
  if message_id == hash("trigger_response")  then
     if message.enter then
        self.door_triggered = true
     else
        self.door_triggered = false
     end
  end
end
5 Likes

Thank you. I moved msg.post(".", “acquire_input_focus”) to the init, but it didn’t work. I get collision messages , but I don’t get any messages when I click the button. :thinking:

Sorry for the questions, but I have never written code outside of bash and automation. For me it is a completely new experience and sometimes not easy to understand.)

Note: Input will be sent to on_input() and not on_message().

Have you set up a some input bindings?

Yes, of course, everything is standard there.

Everything works fine for the character, he moves upwards, the problem is the interaction with the object.

Two things:

  • There is no built-in concept of clicking or interacting with a game object. You need to build this yourself. Yes, you can detect user input, but if you need to know if a mouse click happened while over a specific game object you need to implement this (more info here)
  • For UI nodes there is a gui.pick_node(node, x, y) which will tell you if the x and y coordinate was inside the bounding box of the node. Example: Button

With this said, I think it is best if you share the entire script (share as text, and use code fences ``` before and after for formatting)

1 Like

incorrectly said , not a click, but pressing the “up” button

i can recomend add print(action_id) as first line in on_input() method to check that there is no any mistakes and the object receives input messages

Yes, I tried, nothing comes out.

This.

1 Like

When I started copying the script here, that’s when I saw my mistake. It was
local function init(self)

Why I didn’t notice it, I don’t understand. Thank you very much!