Collecting objects (SOLVED)

I can’t make my objects collectable. I tried to do it by adding this code to the object’s script and not adding anything o the hero’s script, but it was not working. I am new in Lua and can’t understand how works “msg.post()”. I would be glad if somebody explained it me on this example. Thank’s.

function on_message(self, message_id, message, sender) if message_id == hash("contact_point_response") then go.delete() end end

I happen to be working on the same thing at the moment.

It’s explained pretty well in the beginner tutorials.

Basically on your coin and your hero game objects you have to have a collisionobject and a shape. Then in the collisionobject you need to set their mask and group to something like this:

coin:
group: coin
mask: player

player:
group: player
mask: coin

Also, make sure to set your type appropriately based off your game.

Finally, assuming you had the script on your coin, you would do something like this:

function on_message(self, message_id, message, sender)
     -- handle object collision
    if message_id == hash("contact_point_response") then
    	if message.group == hash('player') then
    	    -- give the player his point!
    	    settings.add_point()
    		
    	    -- kill self
    	    go.delete()
        end
    end
end

Of course adapt you collection logic as needed. Hope that helps, but it’s explained more thoroughly in the manual and the tutorial I linked :slight_smile:

3 Likes

Ok, so, if a game object has a collision component attached to it and the collision component is configured correctly to collide with one or more other collision groups the physics system will automatically generate contact_point_response and collision_response messages. These messages are sent to any script components attached to the colliding game objects.

You snippet of code looks fine. If nothing is happening then you have not configured the collision components correctly. Some things to check:

  1. Does your player/hero game object have a collision component?
  2. Does your coin game object have a collision component?
  3. Does your hero collision component have a mask value that is matching the coin’s collision component group?
  4. Does your coin collision component have a mask value that is matching the hero’s collision component group?
  5. Have you attached the script with the snippet of code you posted to the coin game object?
  6. How does the game look when running if you enable physics.debug in game.project? Do you see any contact responses?

Other than that, please read and re-read the physics manual and the tutorial.

2 Likes

Thanks, it was too helpful!

1 Like