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.
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:
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.
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
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.
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.)
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.