Different modules for different games

Hi again Heroic Labs,

Loving Nakama so far, but I have one small question; I was wondering if it’s possible to load different authoritative multiplayer server modules through the lua function makematch()*? For instance, a platform with minigames (each with custom game logic/modules), all available through the matchmaker.

Thank you!

*Found here https://heroiclabs.com/docs/gameplay-multiplayer-server-multiplayer/

local nk = require(“nakama”)

local function makematch(context, matched_users)
– print matched users
for _, user in ipairs(matched_users) do
local presence = user.presence
nk.logger_info((“Matched user ‘%s’ named ‘%s’”):format(presence.user_id, presence.username))
for k, v in pairs(user.properties) do
nk.logger_info((“Matched on ‘%s’ value ‘%s’”):format(k, v))
end
end

local modulename = “pingpong”
local setupstate = { invited = matched_users }
local matchid = nk.match_create(modulename, setupstate)
return matchid
end
nk.register_matchmaker_matched(makematch)

@Korama Sure, the multiplayer engine in designed to let you register different match handlers with different names which can be created with the MatchCreate server runtime function. You can register different match handlers with code like you have above:

-- minigame1.lua
local nk = require("nakama")

return {
    -- implement all the match lifecycle functions
}
-- minigame2.lua
local nk = require("nakama")

return {
    -- implement all the match lifecycle functions
}

Then you’d create a match from one of the mini games with:

local matchid = nk.match_create("minigame1", { some = "initial input" })
-- OR
local matchid = nk.match_create("minigame2", { some = "initial input" })

Hope this helps.

1 Like

Thanks @novabyte, that does help - but what I’m wondering is if there’s any way the makematch function above can discriminate between different matchmaker queries, and thus load different multiplayer modules depending on what those queries are. C’est possible?

@Korama Yep, just look at the data properties passed in for the "matched_users". For the match maker to return a match which fits what the players looked for each player would have needed to pass in the match type they’re interested in.

For example with a search query in the matchmaker like: "+properties.match_type:minigame1" the player has said they’re only interested to match against players who exact searched on the "minigame1". Their data properties passed into the matchmaker would have looked like:

var query = "+properties.match_type:minigame1";
var stringProps = new Dictionary<string, string>() {
  {"match_type", "minigame1"}
};
var numericProps = new Dictionary<string, int>() {};
var matchmakerTicket = await socket.AddMatchmakerAsync(query, 2, 4, stringProps, numericProps);

You can extract these properties which each player matched by the matchmaker within the "matched_users" LTable in the Lua code above.

2 Likes

Perfect that’s exactly it! Thank you so much :heart:

1 Like