Updated at July 17, 2026 02:08 PM
Index iterators
By our gameplay, all caught pokémons are returned back to the map. We do
this for all pokémons on the map every 60 seconds using respawn()
method. We iterate through pokémons by status using Tarantool index
iterator function
index_object:pairs()
and reset the statuses of all "caught" pokémons back to "active"
using box.space.pokemons:update().
respawn = function(self)fiber.name('Respawn fiber')for _, tuple in box.space.pokemons.index.status:pairs(self.state.CAUGHT) dobox.space.pokemons:update(tuple[self.ID],{{'=', self.STATUS, self.state.ACTIVE}})endend
For readability, we introduce named fields:
ID = 1, STATUS = 2,
The complete implementation of start() now looks like this:
-- create game objectstart = function(self)-- create spaces and indexesbox.once('init', function()box.schema.create_space('pokemons')box.space.pokemons:create_index("primary", {type = 'hash', parts = {1, 'unsigned'}})box.space.pokemons:create_index("status", {type = "tree", parts = {2, 'str'}})end)-- create modelslocal ok_m, pokemon = avro.create(schema.pokemon)local ok_p, player = avro.create(schema.player)if ok_m and ok_p then-- compile modelslocal ok_cm, compiled_pokemon = avro.compile(pokemon)local ok_cp, compiled_player = avro.compile(player)if ok_cm and ok_cp then-- start the gameself.pokemon_model = compiled_pokemonself.player_model = compiled_playerself.respawn()log.info('Started')return trueelselog.error('Schema compilation failed')endelselog.info('Schema creation failed')endreturn falseend