Solarus-Games English Forum

Solarus => Development => Topic started by: squidliver on September 29, 2021, 11:04:41 PM

Title: how do you activate a function when all the enemies on a map are killed
Post by: squidliver on September 29, 2021, 11:04:41 PM
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.
Title: Re: how do you activate a function when all the enemies on a map are killed
Post by: Spikira on October 05, 2021, 01:12:04 PM
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
Title: Re: how do you activate a function when all the enemies on a map are killed
Post by: Max on October 07, 2021, 04:57:30 PM
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: [Select]
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

Code: [Select]
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
Title: Re: how do you activate a function when all the enemies on a map are killed
Post by: Spikira on October 08, 2021, 09:13:12 AM
Its enemy:on_dead(), not enemy:on_killed().

Also, a for loop would be most effective actually.
Title: Re: how do you activate a function when all the enemies on a map are killed
Post by: PeterB on January 04, 2022, 10:01:23 PM
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.