The "Argh is an idiot about LUA" thread.
Posted: 27 Aug 2007, 03:26
Ok, here's my thread, where I will happily confess my ignorance and hopefully cure it. I have sat down with the LUA references, and am now hacking away. Hopefully, this thread will be useful to somebody, but at the very least, you folks who already know this stuff can chuckle at my lack of expertise 
Now, this series of posts, or bloglike chatter, is intended mainly for people, like myself, who may have some experience with BOS or other C-like languages, but find LUA baffling at first.
If you've never, ever, programmed anything before, I strongly urge you to check out the official LUA references, which I am also making use of very heavily here, as well as liberally stealing from (working) Spring LUA stuff. I'm not going to cover a lot of topics that you should know about, and I'm going to skip a lot, so you won't see it here. Just bear that in mind!
***********************************************************
***********************************************************
***********************************************************
LUA presents some challenges for me... the primary one being how to work with Functions and Tables- two of the main structural concepts behind LUA.
LUA is extremely flexible about the structure of Tables, which can be used as arrays. Most languages that I know of want arrays to be "typed" fairly strictly- LUA apparently doesn't care. This is unfamiliar ground.
For people who've never worked with Tables ever before, a Table is, basically, a list of items, stored in an exact order:
They can be multiple types- you can even store the resulting values from Functions within a Table.
"Why, gosh, that's great, Argh... but why do we care about this?"
Well, the main reason to care, in the context of LUA, is that you can use Tables within and in between Functions, to move variable values around in an easy way, or to grab a lot of data from multiple sources and then present it to the user. Think of it as a way to organize your data for later use, basically. Most LUA Functions written thus far for Spring include a lot of Tables.
Now, let's start putting that Table to some sort of practical use.
Let's say that we've created our Table, t. Now we want to get the value of the third item.
Ok, so I can write code like this:
Which would result in the value of the third item in the Table, t, being printed.
In fact, just to get this to sink in, here is "hello world" in LUA, using a Table:
... results in "hello world". Get it?
However! You can't just do that, in Spring! Nope, you've gotta specify what method you're going to use to print things. Here's a "hello world" example, using the Spring Messagebox:
Obviously, we don't want to use that, for all situations- i.e., most of the time, we want this information described in a Tooltip. Therefore, we want to use stuff like:
Coupled with a formal declaration of a "button" within the LUA UI:
As you can see, we need to use Spring "hooks" to communicate with Spring, in the ways that we want to. Now... the hard part (at least, for me): writing Functions, so that we can actually make stuff happen!

Now, this series of posts, or bloglike chatter, is intended mainly for people, like myself, who may have some experience with BOS or other C-like languages, but find LUA baffling at first.
If you've never, ever, programmed anything before, I strongly urge you to check out the official LUA references, which I am also making use of very heavily here, as well as liberally stealing from (working) Spring LUA stuff. I'm not going to cover a lot of topics that you should know about, and I'm going to skip a lot, so you won't see it here. Just bear that in mind!
***********************************************************
***********************************************************
***********************************************************
LUA presents some challenges for me... the primary one being how to work with Functions and Tables- two of the main structural concepts behind LUA.
LUA is extremely flexible about the structure of Tables, which can be used as arrays. Most languages that I know of want arrays to be "typed" fairly strictly- LUA apparently doesn't care. This is unfamiliar ground.
For people who've never worked with Tables ever before, a Table is, basically, a list of items, stored in an exact order:
Code: Select all
t = { 1,1,2,3,5,8,13 }
"Why, gosh, that's great, Argh... but why do we care about this?"
Well, the main reason to care, in the context of LUA, is that you can use Tables within and in between Functions, to move variable values around in an easy way, or to grab a lot of data from multiple sources and then present it to the user. Think of it as a way to organize your data for later use, basically. Most LUA Functions written thus far for Spring include a lot of Tables.
Now, let's start putting that Table to some sort of practical use.
Let's say that we've created our Table, t. Now we want to get the value of the third item.
Ok, so I can write code like this:
Code: Select all
print( t[3] )
In fact, just to get this to sink in, here is "hello world" in LUA, using a Table:
Code: Select all
t = {"goodbye cruel world","I prefer ham sandwiches","hello world"}
print t[3]
However! You can't just do that, in Spring! Nope, you've gotta specify what method you're going to use to print things. Here's a "hello world" example, using the Spring Messagebox:
Code: Select all
Spring.Echo('hello world')
Code: Select all
MyNewbieCmdDesc.tooltip = ("hello world")
Code: Select all
local MyNewbieCmdDesc = {
id = CMD_MYNEWBIE,
type = CMDTYPE.ICON,
name = 'My Newbie Code Project',
cursor = 'Newbie Code!', -- add with LuaUI?
action = 'MyNewbie',
}