Tuesday, June 14, 2011

Lua, a hello world example

I'm still working on the smuf which now I'm simply calling it stardust...  I'll give more detail about stardust later.  But for now I'll going to show something quite useful for designing game play for your game.  C++ is a very versatile and robust language...  But for building a simple prototype for game play its to verbose, and the compile type would take forever just to test simple game play.  And hell most game designers would give you hell if they would have to use c++ language...

So here comes lua,  lua is a simple scripting langage that is designed to be embed into an c or c++ application.  Being a scripting language its slower than c++ or c but for prototyping game play that doesn't really matter...  Your objective is to try to get the game play going.  And after you have an idea of how the game play works then you'll start writing a highly optimized version in c or c++ languages.  And then move onto your next project.

Now there are some differences in how lua look and feel.  Here a simple comparacent.

lua:
-- Simple Comment
--[[
   Multi line comment
]]

print("Hello, World")

if true then
print("Statement is true")
else
print("Statement is false")
end

c++:
// Simple Comment
/*
   Multi line comment
*/
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
   cout << "Hello World" << endl;
  
   if(true) {
     cout << "if statement is true" << endl;
   } else {
     cout << "if statement is false" << endl;
   }
   return 0
}

As you see lua syntax is simpler than c++ but I still prefer c++ syntax though.  To a designer lua would be easier to read and another useful feature of lua over c++ is dynamic compilation...  Unlike c++ which is a static compiled language, lua is a virtual machine language.  This means it can be compiled down to the virtual machine instruction, or you can just read the script file its self which will then be compiled dynamically down to the vm instruction.  I'm not really sure if there is a speed benefit of compiling it or not though.

Ok now how to get lua and c++ to play together.  This is actually really simple though...  Here is a example.

test.cpp:
#include <lua.hpp> // This simplifies the include stuff for c++
#include <iostream>

using namespace std;


int main(int argc, char** argv) {

 // This creates a new lua state
 lua_State* state = luaL_newstate();

// This open all lua system libraries which
 luaL_openlibs(state);

// This will read a file.  It returns 0 if no error some other value if there is a //error
 int err = luaL_dofile(state, "test.lua");

 if(err) {
   // This prints the error message
   cout << lua_tostring(state, -1) << endl;
 }

//This de-allocates the
 lua_close(state);

 return 0;
}

test.lua:

-- This is a simple file

print("Hello, World") -- just a typical hello world print

To compile this example you'll ether need visual studios binaries or the lua source code.  To get the source code go to


http://www.lua.org/ftp/

It contains all the archive versions of the source code.  The version you'll want is 5.1 or higher to test this crappy example.  For binaries you're on your own there.  I'll post more on this subject later.