Hi there!
I’m currently trying to build a system that generates shareable match_ids (match codes, as I’m calling them) that allow players to join games using the code for my Lua/Godot game. I’m doing it in a manner suggested here; therefore I have a function that is called on the creation of a match that generates a match code, uses it as the key for storing the actual match_id in a collection, and then returns both the code and the match_id to the player. The main functions for this are:
local function create_match()
local match_id = nakama.match_create("match_control", {})
local payload = {
["match_id"] = match_id,
["match_code"] = create_match_code(match_id)
}
return nakama.json_encode(payload)
end
-- Create match_code, store match_code,match_id as a key,value relationship
-- and return match_id
function create_match_code(match_id)
-- Keep trying it until a code that works is found
local count = 0
while count < 30 do
local code = generate_match_code()
local data = {
["match_id"] = match_id
}
local object = { collection = "match_codes",
key = code, value = { data }}
local success = nakama.storage_write({object})
if success then
return code
end
count = count + 1
end
error("Game code could not be created")
end
Where I’m having trouble with is retrieving that match_id with the code. I’ve tried doing it both with a Lua RPC that the Godot client calls, and directly from the client using read_storage_objects_async()
. Both just return empty dictionaries with no objects. I think it might have something to do with my lack of definition of user_id’s in my read and write, but that is necessary as any player should be able to look this data up, right?
Below are my attempts in Lua and Godot:
Lua RPC attempt
-- Takes a match_code and returns its associated match_id
local function match_code_to_match_id(context, payload)
local match_code = nakama.json_decode(payload).match_code
local object_id = { collection = "match_codes",
key = match_code }
local objects, err = nakama.storage_read({object_id})
if objects then
nakama.logger_info("Value: "..serialize_table(objects[1].value))
return nakama.json_encode(objects[1].value)
end
error(err)
end
var response : NakamaAPI.ApiRpc = await _client.rpc_async(_session, \
"match_code_to_match_id", JSON.stringify(payload))
Pure Godot attempt
var response : NakamaAPI.ApiStorageObjects = await _client.read_storage_objects_async(_session, \
[NakamaStorageObjectId.new("match_codes", match_code)])