PDA

View Full Version : Update Handler issue.



drahkar
January 9th, 2010, 20:18
Ok, I can't seem to narrow down what's wrong with this bit of code. It works the first time it inits, but when it calls using onUpdate it throws a 'attempting to set a nil value as a handler function'.



function onInit()
speciessource = window.getDatabaseNode().getChild("details.species");
speciestype = speciessource.getValue();
speciessource.onUpdate = update(speciestype, stat[1]);
update(speciestype, stat[1]);
end


Any thoughts?

ddavison
January 9th, 2010, 20:24
speciestype is nil when onUpdate fires later since it is defined within onInit(). You will want to check for nil values and re-initialize that value before you use it.

drahkar
January 9th, 2010, 20:30
Ah! Ok. That makes sense.

ddavison
January 9th, 2010, 20:30
You can also declare variable local to a script block instead of local to a function. That way you don't have to pass them to a function.



local locationlist = nil;
local currentnode = nil;


Play around with printing the values of them to the console and verify that the value is getting set and is as expected before your code tries to call it or do something with it.