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
I don’t think you can address citizens[] at all (using squared brackets). I always get nil values returned. Also, when you call citizens:get(citizen_id), citizen_id is actually the entity id (so usually a four digit number). Sadly, I have no idea where to find the functions that citizens has so I can’t check what can be called, passed, or returned either
so when you call citizens:get() or citizens:each(), that is not found in population_faction.lua or population_service.lua, it must be within the c++ engine.
function Commands:add_citizen_command(session, response, entity, job)
local player_id = session.player_id
local pop = stonehearth.population:get_population(player_id)
local citizen = pop:create_new_citizen()
citizen:add_component('stonehearth:job')
:promote_to('stonehearth:jobs:worker')
local explored_region = stonehearth.terrain:get_visible_region(player_id):get()
local centroid = _radiant.csg.get_region_centroid(explored_region):to_closest_int()
local town_center = radiant.terrain.get_point_on_terrain(Point3(centroid.x, 0, centroid.y))
local spawn_point = radiant.terrain.find_placement_point(town_center, 20, 30)
radiant.terrain.place_entity(citizen, spawn_point)
return true
end