I just started working on the server side things of Nakama and wanted to make use of the matchLoop for my game in Unity. Looking at the documentation I saw that I had to use initializer.registerMatch to do that. I defined the needed functions in a separate file called “match_handler.ts”. After starting the server it gives the following errors:
server-nakama-1 | {"level":"error","ts":"2022-10-04T15:22:43.334Z","caller":"server/runtime_javascript.go:1601","msg":"Failed to eval JavaScript modules.","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}
server-nakama-1 | {"level":"error","ts":"2022-10-04T15:22:43.334Z","caller":"server/runtime.go:614","msg":"Error initialising JavaScript runtime provider","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}
server-nakama-1 | {"level":"fatal","ts":"2022-10-04T15:22:43.334Z","caller":"main.go:158","msg":"Failed initializing runtime modules","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}
It’s weird that it says that it failed to find the initModule, as I have defined that in my main.ts file, and it works when I remove the registerMatch call. I’ve tried many different examples and looking at existing repo’s but just can’t seem to fix it.
- Versions: Nakama 3.13.1, Windows/Docker
- Server Framework Runtime language (If relevant): TS/JS
match_handler.ts:
{// Match initialization function (runs once when the match is created either via rpc or through the matchmaker)
const matchInit: nkruntime.MatchInitFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, params: {[key: string]: string}) : {state: nkruntime.MatchState, tickRate: number, label: string} {
logger.debug('Match initialized.');
return {
state: { },
tickRate: 1,
label: ''
};
};
// When a player tries to join
const matchJoinAttempt: nkruntime.MatchJoinAttemptFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence, metadata : {[key : string]: any}) : {state: nkruntime.MatchState, accept: boolean } | null {
return {
state,
accept: true
};
};
// When one (or multiple) player(s) actually join(s)
const matchJoin: nkruntime.MatchJoinFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null {
return {state};
};
// When a player leaves
const matchLeave: nkruntime.MatchLeaveFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null {
return {state};
};
// Runs every tick
const matchLoop: nkruntime.MatchLoopFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) : {state: nkruntime.MatchState} | null {
return {state};
};
// Runs when the match gets terminated
const matchTerminate: nkruntime.MatchTerminateFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, graceSeconds: number) : {state: nkruntime.MatchState} | null {
return {state};
};
const matchSignal: nkruntime.MatchSignalFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, data: string) : {state: nkruntime.MatchState, data?: string} | null {
logger.debug('Lobby match signal recieved: ' + data);
return {state, data};
};}
Main.ts:
const InitModule: nkruntime.InitModule = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
logger.debug("Hello World!!@!!");
initializer.registerMatch('match', {
matchInit,
matchJoinAttempt,
matchJoin,
matchLeave,
matchLoop,
matchSignal,
matchTerminate
});
}