How to have text appear on screen when a collision occurs (SOLVED)

I was wondering how to have a text_gui appear on the screen after a player “collects” the key or collides with it. I have used the platformer template.

You could use labels for this too.

To detect the event, you want to look for a collision_response message with the key.

function on_message(self, message_id, message, sender)
    if message_id == hash("collision_response") then
        if other_id == hash("keys") then
			-- spawn a label GUI here?
			-- send message to GUI to show text?
			-- put data into a shared module queue?
		end
    end
end

Then send a message to the GUI script to display the GUI text node. Or have scripts share access to a module.

If you need help with more specific things just ask.

Should also note you’ll probably want to set a flag on the on_message and then let your update function do the spawning / message passing so you only get one event per frame.

In order to set up the message mentioned in @Pkeod’s answer, you additionally will have to set up collision objects. To do this, you will have to set up physics. To get started with this, you might want to check out the runner tutorial.

You will want to set up a collision object for your character, and a collision object for the key. This will then broadcast the “collision_response” messages as desired. If you have any questions about the implementation of this, just let us know (either here or in a separate question).

1 Like

Thanks for helping out! I got the program to work but I have a couple questions though:

  • Where can I find “distance” fonts, as the font in the template is kind of blurry?
  • How do I set up the “flags” so I can have the event occur only once?

Find a font online which has a license you can use. Add it to your project. Create a new .font file then change the settings to something like this

2018-05-21%2018_30_53-How%20to%20have%20text%20appear%20on%20screen%20when%20a%20collision%20occurs%20-%20Questions%20-%20Defold%20g

Then you can assign that font to a GUI.

By a flag I mean something like

if self.key_collected ~= true then
    -- send message
    self.key_collected = true
end

That will only allow sending the message once even if you get multiple collision responses per frame (which you probably will).
You can also set a true/false flag and resolve it on the next update of the object.

2 Likes

Ok thanks for helping! That seems like all I need for this thread.

2 Likes