Lua calls to booleans defined different ways

ExpiredReign

Deity
Joined
Jan 3, 2013
Messages
2,450
Location
Tasmania
I'm trying to understand how booleans are parsed (I think that is the term) by Lua when they are defined a number of different ways.

eg.
In CIV5Buildings.xml the defining of a number of tables are laid out and most of the booleans are done like:
Code:
<Column name="ArtInfoRandomVariation" type="boolean" default="false"/>
but some are defined as:
Code:
<Column name="RequiresUniquePlayers" type="bool"/>

Now when you examine these tables with SQLiteSpy all the booleans defined in the first manner are 0 or false, unless later modified to be 1 or true. Whereas the booleans defined in the second manner are empty or null, unless changed to 1 or true.

What I want to know is, if you have Lua code that makes a check of a boolean, expecting it to be 0 or false, and it finds it as null, as the second table example shows it, won't that produce an error?

Secondly, I have found that the Notifications table, as examined with SQLiteSpy, shows the booleans defined there as "false" or "1". If someone wrote Lua code that asked if a value was false this way:
Code:
if someValue < 1 then...
(don't pick on the poor code)
Would it fail because the value was stored as "false"? Or are these values recognized as booleans no matter which manner they are stored?
 
In Lua, when evaluated as a condition, only false and nil are false, everything else is true (unlike some other languages where 0 and/or the empty string are also considered as false) - see http://lua-users.org/wiki/ExpressionsTutorial

Sqllite has no boolean datatype (all booleans are stored as ints - either 0 or 1) - see http://www.sqlite.org/datatype3.html

The Civ5 game core DLL does the conversion for you, so if you access GameInfo.Table[index].BooleanColumn it will give you a Lua boolean value (and not an int). The only time you need to know/care what's in the database is if you directly access the column value via DB.Query()

Bottom line, if the XML files say its a boolean and you don't use direct database lookups, treat it as a boolean in Lua.
 
Excellent. I knew you would have the answer. :goodjob:

I think I read you teach at a university? I hope the students there appreciate your insight as much as I do. Thank you very much.
 
I think I read you teach at a university?
Not me, although I have worked for several universities and given training courses in various IT subjects, but I have been writing code for more years than I care to remember (any other ex-Fortran4 coders here?) :eek:
 
Well that doesn't matter, you are here helping plenty of us understand the coding process. For that I thank you.
 
Back
Top Bottom