Hi there!
I’ve been trying to learn building runtime code for Nakama in Golang. I’m trying to work off the LUA example in the godot-nakama-demo and I was able to convert the world_rpc.lua
over to Golang, but I’ve hit a few barriers with the world_control.lua
The first and most major one is that it looks like the module/match object is being intiallized like this in LUA:
local world_control = {}
And I am not sure exactly how that would be done in Golang.
The other big issue I’ve been having which sort of builds off the previous is how to use Nakama’s build in fucntions with the module/match obect such as world_control.match_loop
. Searching through github.com/heroiclabs/nakama-common/runtime
, I think it may be something like: func (m *world_control) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state MatchState, messages []runtime.MatchData) interface{} {
but I am not so sure.
I have a few ideas of where and this is what I have so far. I want to make sure I am going in the right direction: world_control.go · GitHub
Any and all help is appreciated! Thank you in advanced!
1 Like
Hi @josephbmanley welcome!
Were you able to make progress on how to port the Lua example of the match handler logic from the Godot demo over to Go code?
The first and most major one is that it looks like the module/match object is being intiallized like this in LUA:
And I am not sure exactly how that would be done in Golang.
This is an LTable (Lua table type) which you would represent in Go with a struct
:
type worldControl struct {
}
@josephbmanley looks like you are headed in the right direction. The key here is the following.
The heart and soul of the GoLang code is the following function:
func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
// All your initialisation code goes in this function
}
You have to define this function in the main package of your code. Within this function, you will register all the hooks, rpcs as well as the matchhandlers. E.g. to register your matchhandler, you need to write code like this
func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
if err := initializer.RegisterMatch("worldmatch", func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule) (runtime.Match, error) {
return &world_control{}, nil
}); err != nil {
return err
}
}
Make sure that world_control implements all the functions requird by the match handler and then when you compile and run the code, world_match will be registered as a match handler against the key “worldmatch”. You can then start a match that corresponds to this match loop as follows:
matchId, err := nk.MatchCreate(ctx, "worldmatch", params)
// params is a map[string]interface{} which you will receive in the MatchInit function of the match handler.
Hope this helps.
4 Likes