I have a problem, i need to call a function with variable parameters that i get from a webservice... so for example:
I have a function:
function MyPrint(param1, param2)
print(param1, param2)
end
I save this function in an array of callbacks:
callback[1] = MyPrint
I get parameters from server:
params = "[2, 88]" --> JSON from server
I do:
params = json.decode(params)
-- so
-- params[1] = 2
-- params[2] = 88
I tried to pass this parameters to my callback as:
pcall(callback[1], unpack(params))
I got 2 and 88 on MyPrint function...
But if server sends "[null, 88]", I got nil on both values... I have readed that unpack function has problem with null values... but then... how can I call callback[1] with some null values?
Is there a way to pass an array of parameters directly to function without unpack it?
EDIT: I created MyPrint as an example... but really I don't know how many params needs the callback function, i only have a list of functions and needs to call them with a variable number of parameters that i got from server.
paramsbefore unpacking? Also, see herejson.decodeshould create additional fieldnin the table returned to store the number of arguments. Thenyour_function(unpack(params,1,params.n))would solve the problem.print(params.n)--> niljson.decodeignoring the null parameter thats why your example works... it pass the null directly without parsing the null with json.decodejson.decodecreate anfield or equivalent then there is a loss of data. You can't very well deal of a json object without know how many elements there are. To move on, you'll have to find a way to carry over the number of elements.