"Intercepted a disabled resource" when attempting before hook

I’m just getting started on adding some server logic. I’m hoping to execute a function when the client tries to write to storage. I have a Lua module as follows:

local nk = require("nakama")

local function my_test_function()
  nk.logger_info("Executing my_test_function")

nk.register_req_before(my_test_function, "WriteStorageObjects")

However when I perform a storage operation from the client, it produces an error. This is in the server log:

{"level":"warn","ts":"2019-08-28T20:42:58.974Z","msg":"Intercepted a disabled resource.","resource":"/nakama.api.Nakama/WriteStorageObjects","uid":"ENaIUqvmaD"}

Apologies in advance if I have overlooked something silly - new to all of this!

I’m running Nakama using Docker on Windows 10.

OK, I got it to work - I changed it as follows:

local nk = require("nakama")

local function my_test_function(context, payload)
  nk.logger_info("Executing my_test_function")
  return payload -- important!

nk.register_req_before(my_test_function, "WriteStorageObjects")

Going to leave that “–important!” comment in there, to help me remember where I went wrong.

So my guess is the “register_req_before” function requires a function with the “context” and “payload” arguments?

If there is a resource that could help me understand this better I’d greatly appreciate it. I know I need to learn more about Lua and should learn to read the Nakama source code - I could probably figure out how this works there.

Hi @petergordon87. I can explain what happens.

You can think of the before and after hooks in the game server as lifecycle events on requests sent to the server.

In your example you wanted to hook into the "WriteStorageObjects" request to the server and process it with Lua. The server will then pass the payload and a context to the function you’ve registered with the runtime and you’re expected to pass forwards the payload (client request input) forwards to the continuation of the execution flow.

client request -> before hook -> WriteStorageObjects operation -> client response -> after hook

Hope this helps?

Thanks, yea that makes things clear. Can I ask you another question if you’re not too busy? I should then have enough to work with on my own, promise!

In the docs the I’m looking at the example for writing to storage from a module, and I see the following in the Lua example:

{collection = "save", key = "save1", user_id = user_id, value = {}}

This works for me, and the value when viewed through the admin console is “{}”, as one would expect.

Can you give a valid example of a non-empty value? Almost any JSON format I’ve tried causes problems, i.e. with and without escaped quotation marks.

I’m pretty much just included the example code from the docs in “my_test_function” in my original post, but instead including a non-empty value to no success.

Sure. No worries. The function we provide in the nakama module for the Lua runtime to write storage objects is handy because it will take a Lua table as the value field and handle the JSON encode for it to be stored in the database.

For your code to write a value that will become JSON just use a regular Lua table. i.e.

local storage_object = {
    collection = "save",
    key = "save1",
    user_id = user_id,
    value = {
        some = {
            nested = "JSON"
local new_objects = { storage_object }

In the code above I’ve broken down the inputs to the function call to make it clearer:

  • nk.storage_write takes a Lua table “array” as an argument.
  • This table array "new_objects" is a batch of storage objects you want to store in a single transaction.
  • Each "storage_object" must include the required fields: collection, key, etc. and the value is a Lua table which will be converted to JSON.

Excellent, thank you so much for your quick and clear response. I had no idea about Lua tables!