Indexing Resources

Link, unlink, or retrieve resources from a specific path by indexing resources in your Elements game or application.

Indexing allows you to not only link and unlink resources at a path, but it also allows you to retrieve resources. In fact, we can even use wildcards to retrieve a list of resources. Take creation path we used in the above example:

local creation_path = 
    string.format("games/%s/%s/%s", player1_id, player2_id, game_id)

we can use index.list to get all games between player 1 and player 2 like so:

local index = require "namazu.index"
...
local path = 
    string.format("games/%s/%s/%s", player1_id, player2_id, "*")

-- Array of { path, resource_id } for each game
local ids_of_games_between_players = index.list(path)

or even all of the games for player 1 if we change the path to: local path = string.format("games/%s/*", player1_id)

At this point, now that you have their ids, you can do whatever you like with the resources. The most common use case would be to invoke a method on them, for example:

-- Get all the games for the listed ids
local list_of_games = {}

for path, id in pairs(ids_of_games_between_players) do
    local game_info, response_code = resource.invoke(id, "get_info")

    if(response_code == responsecode.OK) then
        list_of_games[#list_of_games + 1] = game_info
    end
end

return response.formulate(200, list_of_games)

Index Functions

Require "namazu.index" from any Lua script.

Last updated