About add a customize function on nakama

I’m new here just after set up a Nakama server with doctor, I’m trying to add a function about sending a String or Json to my unity client once they are connect.However I have studied on the Nakama document for a while still didn’t figured that out.Since I don’t have any experience on lua or go i’m not sure what i did was on the right direction.

So, to add a function like what I mentioned above, isn’t it need to have a correct Lua or a go script compared to a specific version of so document and added to the modules file, then let it run.
On this point I have tried for dozens of different lua or go samples on “https://heroiclabs.com/docs/
most of them will crash Nakama server but “onesignal.lua” .Apparently I did something wrong making these crash but I’m kind of confused which code is a must makes a correct lua or so which can let nakama server running.

And thank you for read my broken English.

Hi @LoserLHM welcome :wave:

So, to add a function like what I mentioned above, isn’t it need to have a correct Lua or a go script compared to a specific version of so document and added to the modules file, then let it run.

Yes. To send inputs to the server and create your own logic to interact with it you need to write your logic in Lua or Go. These are quite different languages (and Go needs to be compiled) but it will be loaded at runtime when the server starts up.

The smallest example of a Lua script which you can put into a file and load with the server:

local nk = require("nakama")

local function custom_rpc_func(context, payload)
  nk.logger_info(("Payload: %q"):format(payload))

  -- "payload" is bytes sent by the client we'll JSON decode it.
  local json = nk.json_decode(payload)

  return nk.json_encode(json)
end

nk.register_rpc(custom_rpc_func, "custom_rpc_func_id")

You can see this code in our docs. The final line of code which calls the function register_rpc tells the server what ID to give the function when it’s called by game clients as an RPC.

You would place the script above in a file named whatever you like (for example “script.lua”) and make sure that the server can load it via --runtime.path <some/folder> or by default in a folder located relative to the folder where the server binary is called “data/modules”.

The code can then be called from your game client code like in the examples in our documentation:

https://heroiclabs.com/docs/runtime-code-basics/#an-example-module

Apparently I did something wrong making these crash but I’m kind of confused which code is a must makes a correct lua

This should not be possible. There’s no way to crash the server with your own Lua code because all code is executed in a sandbox with its own virtual machine. If you’ve got server logs it’d be good to share them so we can see what errors you see.

Hope this helps.

Thanks for reply, that help a lot, at least now I know the direction didn’t go wrong.
So today I successful and literally copy and paste the lua script of your reply into my modules file which it did’t “reboot” the server and successfully run!!
I tried before I post this topic but it just won’t go well

Okay anyhow,so I learned a little bit about the RPC concept,isn’t it like the clients look for the RPC function and these lua and go script provid it.

Now after the lua script running, my unity client is going to look for the rpc right?

The code can then be called from your game client code like in the examples in our documentation:

https://heroiclabs.com/docs/runtime-code-basics/#an-example-module

The example of documentation is about pokemon so i modify the unity C# code like this and try to capture “The smallest example” RPC

var payload = “{“custom_rpc_func”: “custom_rpc_func_id”}”;
var rpcid = “custom_rpc_func”;
var custom_rpc_funcinfo = await client.RpcAsync(session,rpcid, payload);
Debug.LogFormat(“custom_rpc_func info:{0}”, custom_rpc_funcinfo);

Then…unliy console flow error,

ApiResponseException: RPC function not found

So,what i did wrong ? and how do I change these to capture the RPC function
Thanks for your patient.

isn’t it like the clients look for the RPC function and these lua and go script provid it.

@LoserLHM Yes to have code execute on the server be called by game clients you register RPCs from your server logic and you call it by RPC function ID with the client.

Now after the lua script running, my unity client is going to look for the rpc right?

Your game client code will then call the server to execute the logic against the RPC function. In the example code your logic from the game client should look like:

var payload = "{}"; // some JSON object.
var rpcid = "custom_rpc_func_id";
var custom_rpc_funcinfo = await client.RpcAsync(session, rpcid, payload);
Debug.LogFormat("custom_rpc_func info: {0}", custom_rpc_funcinfo);

You had used "custom_rpc_func" which was not the ID we registered the function against in the Lua code. In the Lua code you’ll see it was registered with "custom_rpc_func_id". Hope this helps.

The lua rpc is successfully run. thanks a lot for the help.
Now I’m trying to do the same rpc with golang.
First,I’m using example and try to compiled it but it did not work.


is there any guides or directions?

What does your module setup look like? Can you share your go.mod file and any Go-related environment variables?

Typically we’ve seen this error when attempting to build locally with Go modules disabled, which is not how you should build to load into Nakama. If you search around the forums you’ll find various posts with example go.mod and module setups (like this.)

Thanks for the reply
I copied the example go

package main

import (
“context”
“database/sql”

github.com/heroiclabs/nakama-common/runtime
)

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
logger.Info(“module loaded”)
return nil
}

and put it in the $HOME/plugin_code and compile it by

docker run --rm -w “/builder” -v “${PWD}:/builder” heroiclabs/nakama-pluginbuilder:2.10.0 build -buildmode=plugin -trimpath -o ./modules/plugin_code.so

Then put the plugin_code.so into the Desktop\workfile\modules which is my nakama root file
my lua scripta successfully launched here before.

go version
go version go1.13.8 windows/amd64

nakama | {“level”:“error”,“ts”:“2020-02-22T20:10:15.399Z”,“msg”:“Could not open Go module”,“path”:"/nakama/data/modules/MinimalExample.so",“error”:“plugin.Open(”/nakama/data/modules/MinimalExample"): plugin was built with a different version of package internal/cpu",“stacktrace”:“github.com/heroiclabs/nakama/v2/server.openGoModule\n\tgithub.com/heroiclabs/nakama/v2@/server/runtime_go.go:1973\ngithub.com/heroiclabs/nakama/v2/server.NewRuntimeProviderGo\n\tgithub.com/heroiclabs/nakama/v2@/server/runtime_go.go:1887\ngithub.com/heroiclabs/nakama/v2/server.NewRuntime\n\tgithub.com/heroiclabs/nakama/v2@/server/runtime.go:453\nmain.main\n\tmain.go:130\nruntime.main\n\truntime/proc.go:203”}

However,the above error shows up on docker console.

BTW i do try

docker run --rm -w “/builder” -v “${PWD}:/builder” heroiclabs/nakama-pluginbuilder:2.9.1 build -buildmode=plugin -trimpath -o ./modules/plugin_code.so

I have no direction for now

I’m just following the guide of i’m using windsows so i’m install go1.14.windows-amd64.msi

and trying to do a compile and modulo loading in to my nakama server

i share my files here

but to use golang for plugin do I have to use binary set up instead of Docker ?

Oh I figured that out is the nakama verion and plugin Builder problem.
now i am using nakama:2.11.0 and

docker run --rm -w “/builder” -v “${PWD}:/builder” heroiclabs/nakama-pluginbuilder:2.10.0 build -buildmode=plugin -trimpath -o ./modules/plugin_code.so

that work out thz