heroiclabs / nakama-defold

Defold client for Nakama server.
https://heroiclabs.com
Apache License 2.0
74 stars 12 forks source link

RPC Calls not working #31

Closed dlannan-kakutai closed 1 year ago

dlannan-kakutai commented 2 years ago

After referring to a number of examples and documents here: https://heroiclabs.com/docs/runtime-code-basics/#rpc-example https://heroiclabs.com/docs/gameplay-multiplayer-server-multiplayer/#manually And api references, I was going a little crazy trying to determine why my payload was always and empty string. I havent tracked it in the code (nor am I going to spend time doing so right now) but there is no need to use it. All the payload and information in lua modules can be extracted from the first param - context. Heres a quick example for people having problems getting rpc to work:

local function my_test_rpc(context)

    local payload = context.query_params.payload
    local data = nk.json_decode(payload[1])
    print("data.name = ", data.name)
end

nk.register_rpc(my_test_rpc, "test_rpc")

To call it from the client I use something like this:

local json = require "nakama.util.json"
local myobj = { name = "David" }
local jsonbody = json.encode(myobj)
nakama.rpc_func2( self.nk_client, "test_rpc", jsonbody, nil, function(result)
    print("done")
end)

If you have any problems please let me know. The above is untested, but its very close to what Im using.

uncleNight commented 2 years ago

local data = nk.json_decode(payload[1])

Why do you expect a payload to be a list? It is only what you send in your RPC call - meaning, an entire JSON object.

Also, the payload is the second argument to any RPC function, you're not supposed to fetch it from the context, it's documented with an example here

Try this instead:

local function my_test_rpc(context, payload)
    local data = nk.json_decode(payload)
    print("data.name = ", data.name)
end

nk.register_rpc(my_test_rpc, "test_rpc")
dlannan-fmad commented 2 years ago

Oh. soz. Thanks for the response. Im an idiot.

britzl commented 1 year ago

Seems like this is solved? Closing.