On collision access other-id (object)

i want to access attributes of the objects, that sth. collided with.
so inside the "function on_message(self, message_id, message, sender)"
i try to handle it:

DEBUG:SCRIPT:
{
other_position = vmath.vector3(373.33435058594, 150.80889892578, 0.5),
other_id = hash: [/instance7],
group = hash: [default],
}

so far i can only access the other_id with an if statement like:
if message.other_id == hash ("/spaceship") then

the problem is, that objects spawned by factories have artifical names
/instance5
/instance7
and i dont know which TYPE of object it is.
so i like to access theses instances somehow.
/instance5.getmytype() (getmytype() would be a function)

1 Like

How many types do you have? If you have a handful or so you could assign them to different collision object groups and check message.group. You could also use a Lua look-up table keyed on instance id with value bring the type. Or you could have the type as a go.property() and access it using go.get()

1 Like

If you’re going to use script properties, remember that other_id is the ID of the game object, so to get a script’s properties you have to first get the URL of that script component.

-- get the component called "script" on the "other_id" game object. 
msg.url(nil, other_id, "script")
1 Like

so far collision group all “default”.
i tried for this specific case to set the “bullet from the player” and the “enemy” to a new group like “hittable”.
yet once i change from default group to any other name, all the on_message functions never get triggert in my scripts.

i want to access the gameobject. i still dont see how i could do it with the “msg.url() statement”.
or can i get the attribute of the object too?
as
Result=msg.url(nil,other_id, “type_variable”)

You need to have a bi-directional mapping of groups and mask:

the bullet group has a mask for enemy <-> the enemy group has a mask for bullet

What do you mean when you say this? Do you want to read a go.property() from a script on the game object? Or do you want to know the “type” of the game object? Or something else?

can you give me a hand what bi-directional mapping looks like?
player bullet:
group: bullet
mask: ??

enemy:
group: enemy
mask: ??

yes, accessing a go.property of the “other_id” would be sufficient.

You need to specify collision groups in collision’s properties:

So it will be:

Player bullet - Group: bullet Mask: enemy
Enemy - Group: enemy Mask: bullet

In my game I have laser which deal damage to mobs. Laser has following script:

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		msg.post(message.other_id, "do_damage", {damage = self.damage * self.damage })
	end
end

Mob, who takes damage has following script:

function on_message(self, message_id, message, sender)
	if message_id == hash("do_damage") then
		self.hp = self.hp - message.damage
	end
end

ok, now i understand how this collision group feature works! thanks a lot. mask means (groups) that trigger the collision message. with this i can easily distinguish the objects.
i appreciate your help and i guess, the manual could explain this a bit better…:wink:

and one question towards your solution:
why do you register the collision on the enemy and then send a msg to the origin/player (with: do_damage)?
why not register the collision at the origin/player and call the function do_damage directly there?

it looks a bit indirect. but i shows very well the concept of message sending.

I may have other collisions for my mob and I want mob to take damage only when it collides with laser. Is there a way to define which exactly collision happens?

Also, I have damage unique data for each of 4 lasers and I store it in laser’s script.

Beware you can only have a maximum of 16 groups!

Thanks for all replies, i will summarize 2 solutions quickly.

Working Solution1:
If you have a go.property on the gameobject called “hitpoint” the code to access the property on collision is:

function on_message(self, message_id, message, sender)         
          local my_url=msg.url(nil, message.other_id, "script")         
          local value=go.get(my_url, "hitpoint")
          print(value)
end

Working Solution2:
Use the CollisionGroups as stated above. Yet its limited to know which GROUP collided with the object.

function on_message(self, message_id, message, sender)         
 if message.group == hash("pbullet") then
--do sth when collision with pbullet happend
end
end
1 Like

thanks a lot !! very useful