Author Topic: how do you activate a function when all the enemies on a map are killed  (Read 1654 times)

0 Members and 1 Guest are viewing this topic.

squidliver
  • Newbie
  • *
  • Posts: 1
    • View Profile
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.
« Last Edit: September 29, 2021, 11:19:05 PM by squidliver »

Spikira

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: how do you activate a function when all the enemies on a map are killed
« Reply #1 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

Max

  • Sr. Member
  • ****
  • Posts: 274
    • View Profile
Re: how do you activate a function when all the enemies on a map are killed
« Reply #2 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

Spikira

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: how do you activate a function when all the enemies on a map are killed
« Reply #3 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.