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?
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
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.
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.)
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)