Solarus > Development
how do you activate a function when all the enemies on a map are killed
squidliver:
how do you activate a function when all the enemies on a map are killed? i want to be able to have a chest appear or a door open when the enemies on a map are killed.
Spikira:
sol.timer.start(map, 500, function()
if not map:has_entities([enemy_name]) then
[door_name]:open()
[chest_name]:set_enabled()
end
return true
end)
remember to name the entities in the actual map, not just the breed
should be smth like this: enemy_1, enemy_2, etc.
also chest should be set as not enabled whether through script or declared in map file
Max:
You can also define the function that's called when enemies are killed. Assuming the enemies are named "chest_enemy_1", "chest_enemy_2", etc, then
--- Code: ---function chest_enemy_1:on_killed()
if not map:has_entities("chest_enemy") then
chest:set_enabled(true)
end
end
--- End code ---
You'll have to define that for all the chest enemies since you don't know which will be killed first. You can copy the code, or use a for loop, if you know those. Like
--- Code: ---for x in map:get_entities("chest_enemy") do
function x:on_killed()
--check if other enemies exist
end
end
--- End code ---
I don't remember offhand if the function should be on_killed or on_dead, so check the API docs
Spikira:
Its enemy:on_dead(), not enemy:on_killed().
Also, a for loop would be most effective actually.
PeterB:
https://forum.solarus-games.org/en/index.php/topic,1044.0.html
This thread in Your Scripts also details this, there are also many other scripts here which may help you out as well.
Navigation
[0] Message Index
[#] Next page
Go to full version