how do you activate a function when all the enemies on a map are killed

Started by squidliver, September 29, 2021, 11:04:41 PM

Previous topic - Next topic
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.

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

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


function chest_enemy_1:on_killed()
  if not map:has_entities("chest_enemy") then
    chest:set_enabled(true)
  end
end



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


for x in map:get_entities("chest_enemy") do
  function x:on_killed()
    --check if other enemies exist
  end
end




I don't remember offhand if the function should be on_killed or on_dead, so check the API docs

Its enemy:on_dead(), not enemy:on_killed().

Also, a for loop would be most effective actually.

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.