Learning Lua

edited May 2013 in Tech Support
Hey folks. I am trying to pick up some Lua knowledge so I'm not completely helpless where scripting is concerned. Thanks to Google, I have found a few good resources and I thought it would be nice to share them here so other scripting newbies can find help.

Firstly-
- Programming in Lua (http://www.lua.org/pil/1.html)

Also, the Lua site itself has a ton of information

The Lua Users Wiki also hosts links to other resources

Recently translated into English, this site has a good 'walkthrough' style introduction to Lua, though the author refers to it as LUA for some reason

LuaForge hosts a ton of projects that seem interesting and/or useful

The Lua Reference Manual

Other useful things:

LuaBinaries (http://luabinaries.sourceforge.net/) has windows environment and a ton of other useful things included

LuaEdit (http://luaedit.sourceforge.net/) seems like a feature-rich, robust editor for Lua

I personally am a huge fan of Notepad++ for Windows (http://notepad-plus-plus.org/), which has support for Lua that can be further extended with language files/plugins

I hope someone else finds this to be useful. If anyone knows of other good resources, I would encourage them to share! :) Also, a good tip I received - the best way to learn is by doing. An easy way to do that is to grab binaries from the LuaBinaries project and just start writing some scripts. good luck have fun!
"Under all that we think, lives all that we believe, like the ultimate veil of our spirits." --Antonio Machado
"The belief that becomes truth for me is that which allows me the best use of my strength, the best means of putting my virtues into action." --Andre Gide
"It is not enough to have a good mind; the main thing is to use it well."  --Rene Descartes

Comments

  • http://trylua.org/ is a nice way to try Lua codes in your web browser.
  • Thanks! Useful little thing, tho your 'run lua from the command line' script in Mudlet is pretty awesome!
    "Under all that we think, lives all that we believe, like the ultimate veil of our spirits." --Antonio Machado
    "The belief that becomes truth for me is that which allows me the best use of my strength, the best means of putting my virtues into action." --Andre Gide
    "It is not enough to have a good mind; the main thing is to use it well."  --Rene Descartes
  • NizarisNizaris The Holy City of Mhaldor
    I personally recommend reading either the PIL or Lua tutorial like a novel: cover to cover. I further recommend reading the Mudlet documentation like a cook book: a little here, a little there, and only as you need it.
    image
  • I did just that! PIL was actually surprisingly easy to read- I expected heavy, dry technical information but that wasn't the case at all. I just ordered the 2nd edition, so hopefully that will arrive soon so I can learn about the new(er) features in the language.

    The Mudlet Cookbook - ha! Necessity has demanded that I peruse the manual in exactly the manner you have described. Else I wouldn't know how to use the darned thing (mudlet)!

    thanks for your input!
    "Under all that we think, lives all that we believe, like the ultimate veil of our spirits." --Antonio Machado
    "The belief that becomes truth for me is that which allows me the best use of my strength, the best means of putting my virtues into action." --Andre Gide
    "It is not enough to have a good mind; the main thing is to use it well."  --Rene Descartes
  • edited June 2013
    PIL is amazing. Just going to add to that recommendation.

    Also, Notepad++ is a lifesaver. For sharing, I highly recommend pastebin.com.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • edited June 2013
    Also, this is huge: read other scripts. There's lots of little things that you won't find in most tutorials or such. Off the top of my head:

    y and "yes" or "no"

    If Y is true, returns 'yes', if false, returns 'no'. You can use that in a return, variable setting, or even in a text string. I often do things like... (just an example)

    print ("System enabled: " .. (system.enabled and "yes" or "no"))

    And I ran into this concept by noticing it in someone else's script.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • edited June 2013
    That's Lua's idiomatic ternary operator, similar to C''s `cond ? if_true : if_false`. One difference in Lua: be careful that if_true can't be false or nil. You'll get the wrong results if it is!

    Example: `(42 == 42) ? false : 84` evaluates to 84, not false.
  • @Trevize - can you explain the "yes" and "no" thing a little more? I don't follow but it sounds useful
  • edited June 2013
    It's just shorthand for an if else control structure. It's called a ternary operator because it works on 3 parameters.

    z ? x : y
    or in the case of lua I guess... z and x or y
    where z, x, y are values and "and", "or", "?" ":" are syntax denoting the ternary call.

    z would be some boolean expression that can be evaluated to true or false, x and y are just two values.
    The ternary operator evalues the expression and returns x when true and y when false.

    for example 'true and 1 or 2' would return  1 because true is evaluated as true thus it picks the x to return.

    so one might use it like this:
    target_limb_damage = target_limb_damage + (am_I_using_my_high_damage_rapier() and 5 or 3)

    where the value in the parentheses is either 5 or 3 depending on whether or not the function am_I_using_my_high_damage_rapier() evaluates to true or false.

    If you don't understand this, that's alright. You don't get any power from this notation it just saves you line space that you might otherwise use for an if else (which is why it's also sometimes called an inlineifstatement).

    check out the wikipedia page if you're interested in more: https://en.wikipedia.org/wiki/%3F:

                   Party right, party hard,

                                            Sing and dance, perfect bard.

                                                                     Prefarar loop, accentato whore,

                                                                                             Buy a new rapier, get nerfed some more.

  • Azi said:
    @Trevize - can you explain the "yes" and "no" thing a little more? I don't follow but it sounds useful
    To explain why it works... Lua evaluates 'and' and 'or' like so:

    'and' looks at the left and right. If the left is false, it returns the left. If it's true, it returns the right side.

    'or' looks at the left and right. If the left is true, it returns the left. If it's false, it returns the right side.

    Keep this in mind, then keep in mind that anything other than false or nil is considered true.

    Let's break it down in action...

    true and "yes" or "no"

    1) true and "yes" looks at the left - it's true, so it returns what's on the right. "yes". 2) We now have "yes" or "no". since "yes" is true, it returns that. We're left with "yes".

    false and "yes" or "no"

    1) false and "yes" looks at the left - it's false , so it returns false. 2) We now have false or "no". since false is false, it returns what's on the right. "no". We're left with the "no".

    As stated above, it does what what normally take an if statement, which means five lines dropped to one, and if you surround it in parentheses, you can even place it inline like my first example, saving even more space.
    Current scripts: GoldTracker 1.2, mData 1.1
    Site: https://github.com/trevize-achaea/scripts/releases
    Thread: http://forums.achaea.com/discussion/4064/trevizes-scripts
    Latest update: 9/26/2015 better character name handling in GoldTracker, separation of script and settings, addition of gold report and gold distribute aliases.
  • so it is syntactic sugar for if x then "y" else "z" ...?
  • Azi said:
    so it is syntactic sugar for if x then "y" else "z" ...?
    Basically, yes. Though I'm not sure I'd really call it "syntactic sugar", since the syntax and behaviour of the boolean operations is not there just for this use; this is just a clever use of them to make things a little cleaner.
  • I wouldn't call it a "ternary operator" though. A ternary operator would be a single operator that takes three arguments. The ?: operator in C is a ternary operator. The "and or" thing in Lua isn't, because it's actually just using two independent binary operators after each other, the same way "a + b * c" is just using the binary operators "+" and "*" after each other.
  • It's a clever device that emulates a ternary operator in Lua. If Lua -did- have a ?: operator, that would be syntactic sugar for this and-or device - plus a little extra to handle the edge case I described.
Sign In or Register to comment.