The below code works:
local randomPick = {
currentPick = 'N/A',
pickNode = function(self)
randomPick = node.random(1, table.getn(availableNodes));
self.currentPick = availableNodes[randomPick];
return self.currentPick
end
};
local sentF = function(port, ip, data)
print('Sent info to ' .. randomPick.currentPick);
end
But If I assign the values after I declare randomPick, I get an error:
local randomPick = {};
randomPick.currentPick = 'N/A';
randomPick.pickNode = function(self)
randomPick = node.random(1, table.getn(availableNodes));
self.currentPick = availableNodes[randomPick];
return self.currentPick
end
local sentF = function(port, ip, data)
print('Sent info to ' .. randomPick.currentPick);
end
This does not work and throws out this error. Why is the function picking up an empty randomPick when I assign values to those 2 members?
PANIC: unprotected error in call to Lua API (attempt to index upvalue '?' (a number value))
sentF, it should work either way: ideone.com/0MduyQ Probably you're getting the function in question called before you get randomPick filled. Or maybe there's a silly typo. - Vlad