Calendar

September 2010
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
27282930  

Lua / Luabind

The following code shows the small demo I created to test if the luabind-0.8.1 library worked for me.

main.cpp

extern "C"
{
	#include <lua.h>
	#include <lualib.h>
	#include <lauxlib.h>
}

#include <luabind/luabind.hpp>

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Unit
{
private:
	string name;
	int maxHealth;
	int health;

public:
	Unit() :
	name("unit"),
	maxHealth(10),
	health(10)
	{
	}

	Unit(const Unit& unit) :
	name(name),
	maxHealth(maxHealth),
	health(health)
	{
	}

	const char* getName() const
	{
		return name.c_str();
	}

	void setName(const string& name)
	{
		this->name = name;
	}

	int getHealth() const
	{
		return health;
	}

	void setHealth(int health)
	{
		this->health = health;
	}

};

class UnitManager
{
private:
	map<string, Unit*> unitMap;

public:
	virtual ~UnitManager()
	{
		map<string, Unit*>::iterator i = unitMap.begin();
		while (i != unitMap.end())
		{
			delete (*i++).second;
		}
	}

	void createUnit(const string& name)
	{
		Unit* pUnit = new Unit();
		pUnit->setName(name);
		pUnit->setHealth(10);

		unitMap.insert(pair<string, Unit*>(
			pUnit->getName(),
			pUnit));
	}

	void deleteUnit(const string& name)
	{
		map<string, Unit*>::iterator i = unitMap.find(name);
		if (i != unitMap.end())
		{
			delete (*i).second;
			unitMap.erase(name);
		}
	}

	Unit& getUnit(const string& name)
	{
		map<string, Unit*>::iterator i = unitMap.find(name);
		if (i == unitMap.end())
		{
			throw exception();
		}
		return (*((*i).second));
	}

	const vector<string> getKeys() const
	{
		vector<string> keys;
		map<string, Unit*>::const_iterator i = unitMap.begin();
		while (i != unitMap.end())
		{
			keys.push_back((*i).first);
			i++;
		}
		return keys;
	}

	void printUnits()
	{
		const vector<string> keys = getKeys();
		for (int i = 0; i < keys.size(); i++)
		{
			cout << keys[i] << endl;
		}
	}

};

int main()
{
	lua_State *pState = lua_open();
	if(pState == NULL)
	{
		std::cout << "Error initializing " << LUA_RELEASE << std::endl;
		return -1;
	}

	luaL_openlibs(pState);
	luabind::open(pState);

	luabind::module(pState)
	[
		luabind::class_<UnitManager>("UnitManager")
			.def(luabind::constructor<>())
			.def("createUnit", &UnitManager::createUnit)
			.def("deleteUnit", &UnitManager::deleteUnit)
			.def("printUnits", &UnitManager::printUnits)
			.def("getUnit", &UnitManager::getUnit)
	];

	luabind::module(pState)
	[
		luabind::class_<Unit>("Unit")
			.def(luabind::constructor<>())
			.def("getName", &Unit::getName)
			.def("getHealth", &Unit::getHealth)
			.def("setHealth", &Unit::setHealth)
	];

	luaL_dofile(pState, "script.lua");

	lua_close(pState);

	return 0;
}

script.lua

um = UnitManager();
um:createUnit('jack');
um:createUnit('john');
um:createUnit('jane');

print('Unit names:');
um:printUnits();

u = um:getUnit('jack');
print('Jack\'s health: ' .. u:getHealth());
u:setHealth(3);

u = um:getUnit('john');
print('John\'s health: ' .. u:getHealth());

u = um:getUnit('jack');
print('Jack\'s health: ' .. u:getHealth());

output

Unit names:
jack
jane
john
Jack's health: 10
John's health: 10
Jack's health: 3

further reading

2 comments to Lua / Luabind

  • Holy Beef Roast, it’s a Post! :o :p

    But seriously, that’s pretty nice. So LUA is a good scripting language?

  • Just tried Lua out as an introduction to scripting. It’s known as a powerfull, lightweight ‘all purpose’ scripting language. Very easy to use as you can see in the post. I plan on using it in my ’secret’ game project in combination with the luabind c++ extention. Thanks for comment!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Spam Protection by WP-SpamFree