1. Jim,

    In BagBuddy you use the following:

    BagBuddy_ItemTimes[itemID] = BagBuddy_ItemTimes[itemID] or time()

    I understand (I think) that what you are doing is a logical test trick to assign BagBuddy_ItemTimes[itemID] either its previous value if not nil (the test before the or), or time() if the previous value was nil (the test after the or).

    What I'm wondering is where in the book you first describe this kind of test/assignment? I would like to read more and see if there are any other tricks you describe, but in flipping back through the book, I just can't seem to find the initial description.

    Thanks.

    edit: typo

  2. Jim,

    In BagBuddy you use the following:

    BagBuddy_ItemTimes[itemID] = BagBuddy_ItemTimes[itemID] or time()

    I understand (I think) that what you are doing is a logical test trick to assign BagBuddy_ItemTimes[itemID] either its previous value if not nil (the test before the or), or time() if the previous value was nil (the test after the or).

    What I'm wondering is where in the book you first describe this kind of test/assignment? I would like to read more and see if there are any other tricks you describe, but in flipping back through the book, I just can't seem to find the initial description.

    There may not be anything specifically said about it, but I believe you'll find it around page 33 in the second edition, which covers the use of the boolean and and or operators.

    You'll normally see two forms of this:

     blah = blah or "some default"
     foo = flag and "monkey" or "banana"
    

    The first will set blah to blah, as long as it's not a false or nil value, otherwise it will set it to "some default". foo will become "monkey" as long as flag is true, otherwise it will become "banana".

    This is called short-circuit evaluation of boolean expressions, and its a nice optimisation that has some nice side effects for developers.

  3. "Short-circuit"...right. Been a long time since I heard that expression. I've been out of school for a lot of years, and forgot about that. Seems to work quite nicely in Lua. Very cool.

    Maybe if there's a 3rd Edition of the book you could show some of these specific uses in regards to checking if variables contain values, etc. in a more direct fashion. Or maybe even just throw some sugar at it. But thanks for the description in your post above--nice and clear.

    Slightly off-topic, I also had a hard time figuring out just what _G[.....] was. I found a brief note of it in the book prior to its use in BagBuddy (sorry, but I don't have the book handy right now for a page number) and then usage of it in BagBuddy, but I had to go to another source to really read about how the global environment worked (and realize that _G._G==_G). Though, again, I may have missed it earlier in the book. Perhaps a few pages on manipulating the global environment would be handy.

    Anyway, your book is the "bible" on this subject. I've had the 2nd Edition two weeks and already its wearing out!

    Thanks again.

    edit - grammar