Log in

View Full Version : Trouble with client-side database timings - is there a simpler solution ?



phantomwhale
May 9th, 2013, 13:42
During testing of SW 3.4, a timing bug reappeared where player's bennies sometimes didn't update.

I tracked the bug down to my removal of a curious bit of code I'd written. This code essentially did this:



childNode = nodeSource.createChild(sChild, sChildType)
childNode.onUpdate = fUpdateFn
fUpdateFn()


Except sometimes it seemed that on a client (where the node would never be created, as it's not owned by any of the clients) that the node was NOT yet created. This meant childNode would be nil, and the updates would never happen beyond the first one.

The workaround was to call an all-in-one function that either created the node, registered the update callback and then fired it off once OR it created a callback function that wrapped all that functionality, and fired THAT callback function whenever a new child is added to the main node - essentially ensuring the update handler eventually gets attached to the child node one it's finally created.



function updateOnChildNode(nodeSource, sChild, sChildType, fUpdateFn)
if not nodeSource or not sChild then
return nil
end

local childNode
if not nodeSource.isReadOnly() then
if sChildType then
childNode = nodeSource.createChild(sChild, sChildType)
else
childNode = nodeSource.createChild(sChild)
end
childNode.onUpdate = fUpdateFn
fUpdateFn()
else
local f = function()
if nodeSource.getChild(sChild) then
nodeSource.getChild(sChild).onUpdate = fUpdateFn
fUpdateFn()
return true
end
end
if not f() then
nodeSource.onChildAdded = f
end
end
end


The real question here is... could this potentially all be simplified down ? Perhaps I should more be focusing on the code around this, and why the client node isn't yet created when the method is called (during onLogin, as part of the characterlist on the desktop). But the above seems... convoluted, although it does seem to fix the problem for now !