Jump to content

Function passed as argument becomes nil


Recommended Posts

I was creating a script that was based on a sort of HTTP client/server model where the client issues a request to the server, the server then elaborates with a particular function and then returns the result (that corresponds to a client-side event) with the elaborated result. So i have created a function that registers the request server-side, by adding an event and an appropriate handler. The function requires an argument that, given the data as a table, it produces another table with the result. The code follows:

  
local nameReq = "[freeroam]:%s:req_%s" 
local nameRes = "[freeroam]:%s:res_%s" 
  
function register_newEvent(name, res, processor) 
  local req = string.format(nameReq, getResourceName(res), name) 
  local res = string.format(nameRes, getResourceName(res), name) 
  
  if not addEvent(req, true) then 
    return false 
  end 
  if not addEventHandler(req, root, function (data) 
    triggerClientEvent(res, source, processor(data)) 
  end) then 
    return false 
  end 
  
  return true 
end 

As you can see, the function object is called "processor" and when called, if i do the following:

outputServerLog(tostring(processor)) 

it outputs "nil", in fact it gives me an error when i call it in the response event trigger. Another thing i have to specify is that it works resource-local, but when i try to do it from another resource, using the exported functions, the error comes up. The way i call it is the following (server-side):

  
function onResourceStart(res) 
  local exp = exports["req-res"] 
  
  exp:register_newEvent("getAvailableTranslations", res, function (data) 
    return { database_getAvailableTranslations() } 
  end) 
  exp:register_newEvent("getTranslation", res, function (data) 
    return { database_getTranslation(data.language, data.name) } 
  end) 
  exp:register_newEvent("getLanguageImage", res, function (data) 
    return { database_getLanguageImage(data.language) } 
  end) 
  exp:register_newEvent("getLanguageFullName", res, function (data) 
    return { database_getLanguageFullName(data.language) } 
  end) 
end 
  
addEventHandler("onResourceStart", resourceRoot, onResourceStart) 
  

I even tried storing the functions in the script instead of creating them as anonymous functions, but i got the same result. The error i get is: "attempt to call upvale 'processor' (a nil value)". Any solutions? I'm available to give further explanations. Thanks in advance.

Update: It looks like that when you call an exported function, if you pass a function object as a parameter, then it will become nil, i hope there are workarounds for this :cry:

Link to comment
  • Discord Moderators

Use a table visible to the handler function (that is, declared in the same scope as register_newEvent) to store in it the processor functions for each event, and then let the event handler read the function from the table. You need to keep an eye in that table not growing very large though, but I don't think that's a problem.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...