Now that we've seen a bit of Lua, let's try it out! First, download the lua project that can be found here:
Get the project compiling and running (this should be easy, just open up project in Visual Studio, compile, and run). Type in a few lua commands into the command prompt, and make sure everything seems to be working. Then, create a .lua file and try a dofile command. If you use a relative path instead of an absolute path for the filename (which you should!) then the system will look in the current working directory for the .lua file -- which is the directory that the executable file is in (if you double-click on the executable), or the directory that the project is in (if you run from within Visual Studio) You can, of course, change the working directory that the debugger uses under the project settings in Visual Studio. I've placed a test.lua file in the project directory that has hello world, you should be able to run it with
dofile("test.lua")
Everything working? Then on to the exercises!
Create a .lua file for a complex number class that behaves as follows:
> c1 = Complex.create(1, 2) > c1.print(c1) 1 + 2i > c1:print() 1 + 2i > print(c1.real(c1)) 1 > print(c1:complex()) 2 > c2 = Complex.create(5,6) > c3 = c1.add(c1, c2) > c3:print() 6 + 8i > c3 = c1:mult(c2) > c3.print(c3)
Once your complex number class works, extend it using metatables to use the +, -, *, / operators
> c1 = Complex.create(10,5) > c2 = Complex.create(3,4) > c3 = c1 + c2 > c4 = c1 / c2 > c3:print() 13 + 9i > c4:print() 2 - i
Note the following metamethod names:
__mult for * __add for + __sub for - __div for / __index for unknown indicies
Once your Complex class works with metatables, write an iterator function that iterates over the (two) values in a complex number:
> c1 = Complex.create(7, 11) > for v in complexItr(c) do print v end 7 11
Once the simple iterator works, write an iterator function that iterates over the (two) values in a complex number, noting which is the real part and which is the imaginary part
> c1 = Complex.create(5,9) > for k, v in complexItr2(c1) do print k,v end real 5 imaginary 9
Next time, we'll take a look at integrating Lua into Ogre