I want to walk over to an object, and then press a key to bring up a message about the object. I don’t want the message to be automatic when I collide with the object. Any leads for applicable tutorials or information? Much appreciated!
I would split this into two problems:
- Detect when player walks over to an object
- Detect key press when on an object
Detect when player walks over to an object
The solution is to add a trigger collision object and set a flag when entering within the radius of the object and clearing the flag when exiting. I would also store which object the player is standing on.
Detect key press when on an object
The solution is to check if the above mentioned flag is set when your “interact” action is triggered in on_input()
Something like this:
function init(self)
self.on_object = false
self.object = nil
end
function on_message(self, message_id, message, sender)
if message_id == hash("trigger_response") then
-- check if triggering an object of typoe "object"
if message.other_group == hash("object") then
if message.enter then
self.on_object = true
self.object = message.other_id
else
self.on_object = false
self.object = nil
end
end
end
end
function on_input(self, action_id, action)
if action_id == hash("interact") then
if action.pressed and self.on_object then
-- standing on an object and pressing the "interact" button
print("I'm standing on object", self.object)
end
end
end
Hi there, here is my absolute mess of a code that does nothing. Would you mind helping me clean it up? I’m trying to have my player enter the trigger box, I press my already keybound E for interact, and a sprite of my choice (a dialogue box that I’ve created as a sprite) pops up. Then, additionally, I want to press another key (haven’t bound as I don’t know what to call it, but it will be spacebar) to continue to the next text box sprite to continue the dialogue, until the conversation is complete, and I want the text box sprite to disappear and the trigger response to end unless interact is done again.
I’m so sorry for a complex question, I appreciate your help.
function init(self)
msg.post(".", "acquire_input_focus") -- <1>
self.on_object = false
self.object = nil
end
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
go.set_position(go.get_position() + message.normal * message.distance)
if message.group == hash("player") then
--I have no idea how to make my text box sprite just pop up...
end
end
if message_id == hash("trigger_response") then
if message.other_group == hash("obstacle") then
if message.enter then
self.on_object = true
self.object = message.other_id
else
self.on_object = false
self.object = nil
end
end
end
end
function on_input(self, action_id, action)
if action_id == hash("interact") and action.pressed then -- <2>
sprite.play_flipbook("#sprite", "text_box_blank") -- [6]
end
if action_id == hash("interact") then
if action.pressed and self.on_object then
-- standing on an object and pressing the "interact" button
print("I'm standing on object", self.object)
end
end
end
To create a dialogue/popup with some text you have two options:
- Create using a sprite and a nested label component
- Crate a GUI (GUI scenes in Defold)
In both cases you can create these and put them in a .go file. Next you spawn that game object using a factory.
I would suggest that you start there. Create your dialogue box and add it to a game object and a factory. Once you have thet you can show it by calling factory.create("#mydialogue")
in the right place in your script.
Thank you, I’ll try that!