Solarus-Games English Forum

Solarus => Development => Topic started by: Yosha on July 31, 2018, 09:30:02 AM

Title: Store map entities in a table automatically
Post by: Yosha on July 31, 2018, 09:30:02 AM
Hello !! Long time no see !

I'm wondering if it was possible create a table with map entities ?
Here is my need :
I want to create some npc randomly on a map, but, I want those npc to be created on random destinations already on the map. I found how to do, but I have to make a table with all destination manually, and I wanted to know if it was a possibility to automatically generate a table with all map destinations (it could be usefull if the map had 50 or more destinations)

I think that using the map data file could be a solution but I don't see how to do it... (maybe with parsing the map entities corresponding to a destination ?)

Thanks in advance for solutions
Title: Re: Store map entities in a table automatically
Post by: Christopho on July 31, 2018, 10:18:06 AM
map:get_entities_by_type() is made for you.
Code (lua) Select

local destinations = {}
for destination in map:get_entities_by_type("destination") do
  destinations[#destinations + 1] = destination
end
Title: Re: Store map entities in a table automatically
Post by: Yosha on August 01, 2018, 01:22:52 PM
Thanks a lot, Grand Master of Solarus ! It was more easy than I expected !!  ;D
I just have to add get_name()  to make it work !
local destinations = {}
for destination in map:get_entities_by_type("destination") do
  destinations[#destinations + 1] = destination:get_name()
end
Title: Re: Store map entities in a table automatically
Post by: Christopho on August 01, 2018, 03:12:23 PM
Thanks ^^
Then you have a table of destination names and not a table of destinations.