PDA

View Full Version : NetRemote.GetVariable() and lua '=='



wtf
February 9th, 2005, 12:13 PM
I don't remember seeing this mentioned here or in the documentation, but NetRemote.GetVariable() appears to always return a string type. And since lua doesn't apply coercion during equality tests, you have to be careful when manipulating numbers returned using GetVariable().

x = NetRemote.GetVariable(..)
y = NetRemote.GetVariable(..)

x==y is true/false as expected, but if you then do:

y=y+1
x==y is always false since y was silently converted to an integer

So the safe solution is to always do:
x = NetRemote.GetVariable() + 0
if you are using the variables as integers.

I ran into this while creating my NetRemote jukebox, when detecting if I am at the lowest tree level to display a different message and add to playlist if selected.

Promixis
February 10th, 2005, 12:57 PM
Hi,

You can also use the tonumber/tostring functions.

danward79
February 10th, 2005, 01:07 PM
Hi,

I got caught out by this the other day.


function StoreModifiedTimer (TimerIndex)
if DoesXMLFileExsist (XMLStorageFile) == 0 then CreateXMLFile (XMLStorageFile) end
if not XMLDom then CreateXMLDom () end

node = XMLDom:selectSingleNode ("//LightTimers")
if node then
if node.hasChildNodes then
local childnodes = node.firstChild
if TimerIndex > node.childNodes.length then
StoreLightTimers ()
else
while TimerIndex ~= tonumber(childnodes:getAttribute("ID")) do
childnodes = childnodes.nextSibling
end
for attrib, value in LightTimers.Timer[TimerIndex] do
local Elem = XMLDom:selectSingleNode ("//Timer/"..attrib)
Elem.text = value
end
XMLDom:save(XMLStorageFile)
CloseXMLDom ()
end
else
print ("Node has no data")
end
else
StoreLightTimers ()
end
end

The "While" loop did not work, until I used tonumber.

I am sure I read somewere that lua should convert the string to a number automatically.

Oh well

Rob H
February 10th, 2005, 01:10 PM
I am sure I read somewere that lua should convert the string to a number automatically.

Not for comparisons

danward79
February 10th, 2005, 03:14 PM
Ah, that is not very good!