Solarus > Development

Seach for an entity?

(1/2) > >>

ponderitus:
Hey, long time. I was working on an item the other day and wondered if there is a way to check for entities on the map on use..I was trying to make something that would freeze an enemy if it exists

ie.

--- Code: ---function item:on_using()
  item:check_enemy()
  if entity type enemy then
    enemy:freeze()
    --function of item, function()
     enemy:unfreeze()
    end)
  else
    --function of item
  end
  self:set_finished()
end

function item:check_enemy()
--check map for enemies
end

--- End code ---

Max:
I think you could do something like
```local map = item:get_map()
for enemy in map:get_entities_by_type("enemy") do
   enemy:freeze()
end```

Kamigousu:
If you don't mind naming your entities with a prefix you can use map:has_entities(prefix)/map:get_entities(prefix). Here is a link to the API page for the former: https://www.solarus-games.org/doc/latest/lua_api_map.html#lua_api_map_has_entities

then just run your for loop on the results and freeze what ever you need to.

EDIT: realising now you may have wanted to freeze any given enemy/all enemies on the map. In this case, you could use the code provided by Max.

ponderitus:
cheers for the help guys, the code max gave kinda works, it finds them...but the enemy:freeze() gives me

"attempt to call method 'freeze' (a nil value)"

i tried using enemy:immobilize() but that pushes the enemy away from the hero so it looks kinda weird.

Max:
Oh, you had been typing enemy:freeze(), so I assumed that was a method you'd added yourself to the metatable or something.

Depending on how simple you enemies are, you might be able to add an enemy:freeze() method, as long as you remember to deal with the fact that all when an enemy is damaged, all timers and stopped on them and enemy:restart() is called.

If you don't want to do that, unfortunately enemy:immobilize() is the only option, and you can't set the length of time they're frozen and there's no way to unfreeze them.

Navigation

[0] Message Index

[#] Next page

Go to full version