Function exists() does not work correctly with array elements.
-
Found another interesting behaviour, although this specific behaviour is undocumented -
exists()
does not work on vectors, so say for example callingexists(global.vector[12])
simply returns the value ofglobal.vector[12]
if it exists and throws an invalid index error ifglobal.vector[12]
does not exist.The obvious way around this is to check the indexes but it would be good if this behaviour was at least documented, and at best either implemented, or made so calling
exists
on a member of a vector generates an error rather than essentially being a no-op. Finding this was rather confusing as I was calling it on a vector of booleans. -
Thanks for pointing out the limitation in exists() as far as it comes to arrays. As you say the work around at this point is to use:
#global.vector
to check the array lengththat does not get around the returning of the value from the array, rather than a boolean I tested it with this example:
global testArray = {1,2,3,4} echo exists(global.testArray[2]) 3
this is clearly not a boolean response!
please would you raise a bug report for this and reference this thread.
-
@NineMile thanks for reporting this. I've fixed it in the 3.5-dev source code. I wasn't able to reproduce a problem with the second example you have in your issue report.
-
@dc42 I realise now I wasn't actually checking the
tools
array like that, I was checking a global vector storing additional tool details{ vector(limits.tools, {radius, {deflection-x, deflection-y}}) }
which is linked to the defined tool, so the existing fix should cover all of my usages. Thanks!