Open MWFIAE opened 3 years ago
@MWFIAE We discussed this on the community channel can you share the adjustments to your code which do work?
Basically I had to scrap the dictionary and make the calls static like this:
let InitModule: nkruntime.InitModule = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
initializer.registerRpc("CheckVersion", checkVersion);
initializer.registerRpc("CreateItem", createItem);
initializer.registerRpc("Debug", debugRpc);
initializer.registerRpc("DeleteFormation", deleteFormation);
initializer.registerRpc("GetGameHistory", getGameHistory);
[... a whole bunch more...]
};
@MWFIAE Thanks. We'll look into it to make this easier and support more complex registration types. At the moment we extract the function pointers from the JavaScript VM to optimize the access path when the initializer
is used to register the functions and match handlers in the server.
We'll see what we can do to achieve the same performance profile but with the extra convenience as well.
Any news on this? I would also like to initialize all rpc functions with one simple for loop.
I think this is related, I have a separate TypeScript file for the match itself and export the Match methods like so:
export let matchHandler: nkruntime.MatchHandler<nkruntime.MatchState> = {
matchInit,
matchJoinAttempt,
matchJoin,
matchLoop,
matchLeave,
matchTerminate,
matchSignal
};
Then when I try to register like this:
import { matchHandler } from './match/adventure';
initializer.registerMatch('adventure', matchHandler);
I get this error:
{"level":"fatal","ts":"2022-03-08T13:56:21.411Z","caller":"main.go:146","msg":"Failed initializing runtime modules","error":"GoError: js match handler \"matchInit\" function for module \"adventure\" global id could not be extracted: not found\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat registerAdventureMatch (index.js:212:42(5))\n\tat InitModule (index.js:223:25(9))\n"}
So I tried it like @MWFIAE said and referenced each method by itself like so:
initializer.registerMatch('adventure', {
matchInit,
matchJoinAttempt,
matchJoin,
matchLoop,
matchLeave,
matchTerminate,
matchSignal
});
This works so I'm happy as I've spent a really long time trying to solve the above issue, but I do wish there was a better way at the moment.
@novabyte I want to conditionally load rpc functions based on a value from ctx.env
but i also get the error
Description
With nakama 3.1 it is no longer allowed to create rpc calls as anonymous functions. which means the following code will no longer work:
Steps to Reproduce
Expected Result
No error message is returned and the rpc function(s) are successfully registered
Actual Result
Following error is logged in the console:
nakama | {"level":"error","ts":"2021-02-08T15:50:28.427Z","caller":"server/runtime_javascript.go:1382","msg":"Failed to eval JavaScript modules.","error":"GoError: js rpc function key could not be extracted: key not found\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerRpc.func1 (native)\n\tat index.js:280:45(48)\n"}
Context
Client: n/a
Your Environment