I’m trying to get a random citizen of my own population. I’ve come up with some code to do it, but I’m not sure if it’s the best way to go about doing so. Here’s the code:
local pop = stonehearth.population:get_population(player_id) local citizens = pop:get_citizens() local random_int = rng:get_int(0, pop:get_citizen_count()) local i = 0 for _, citizen in citizens:each() do if random_int == i and citizen and citizen:is_valid() then self._sv._random_citizen = citizen break else i = i + 1 end end
If anyone knows a more performant way to grab a random citizen, I hope they’ll share If not, this can be used as a reference by anyone who is wondering how to do so. I know that there is a function “citizens:get(citizen_id)” that might eliminate having to iterate through citizens:each(), but then I’d need some way to easily get a random citizen’s id, which I don’t
. Perhaps @sdee or @yshan would be so kind to give their opinions