BlackHamm3rJack Posted June 12, 2016 Share Posted June 12, 2016 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 Link to comment
Discord Moderators AlexTMjugador Posted June 12, 2016 Discord Moderators Share Posted June 12, 2016 You need to pass the processor function in the event trigger, like you pass data, because I think that in your event handler MTA: SA tries to access to a value which is no longer in the environment. Link to comment
BlackHamm3rJack Posted June 13, 2016 Author Share Posted June 13, 2016 Yes I thought about that but MTA does not allow to send function objects as data in the event trigger functions... Link to comment
Discord Moderators AlexTMjugador Posted June 13, 2016 Discord Moderators Share Posted June 13, 2016 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now