Remove collection (SOLVED)

this is my enemie.collection

https://drive.google.com/open?id=0B8ayXJxIPt2ydlg2T1NNa3JCeFk

and this is my code to remove the enemie when hit a bullet:


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

the questions:

  1. the collection is destroyed? or just destroy enemie1.go?
  2. how can get properties from bullet, maybe damage

Very important lesson: collections do not exist in the running game, so there’s no collection to destroy. A collection is simply a collection (duh!) of game objects, and when it is spawned using a collection factory or added directly in the editor inside another collection it will not exist as something that you can access in the running game. Only the game objects will exist at run-time. So, when you have an enemy.collection consisting of a number of game objects you need to delete all of them, even if they happen to be children of the game object that your script is attached to. Consider this setup:

If I in enemy.script do go.delete() then the body game object will be deleted but the turret game object will still exist. You need to do go.delete("turret") to delete that as well.

If you declare script properties using go.property("damage", 200) then you can access those “from the outside” using go.get("url_or_id_of_game_object", "damage").

6 Likes

Based on your answer I edited my collection:

https://drive.google.com/open?id=0B8ayXJxIPt2ycXRzSXozTXlfSkE

and my code is:

function init(self)
	self.health = 2;
	self.ids = {"cuerpo", "gun", "enemie1", "spawn_point"}
end

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

function hurt(self, damage)
	self.health = self.health - damage
	if self.health <= 0 then
		kill(self)
	end
end

function kill(self)
	go.delete_all(self.ids)
end

is correct? I need remove cuerpo, gun, enemie1 and spawn_point
collision object no need remove?

Regards.

Yes, that looks correct.

Collision objects are components, so they’ll disappear when the game object that contains them is deleted. (“enemie1” in this case.)

“cuerpo”, “gun”, and “spawn_point” are game objects (which just happen to be children of another game object), so they have to be purposefully deleted.

3 Likes