• entries
135
130
• views
52679

# LUA

180 views

So, I didn't do much progress with the event system yet, but for fun, I decided to plug in LUA real quick to see what I could come up with, and here's what the output looks like:

Unit Test: UnitTest_EventSystemEvent System Test - OnStartGOOD_SHIP: I'm hit! (health=90)EVIL_SHIP: I'm hit! (health=90)GOOD_SHIP: I'm hit! (health=80)EVIL_SHIP: I'm hit! (health=80)EVIL_SHIP: I'm hit! (health=70)GOOD_SHIP: I'm hit! (health=70)GOOD_SHIP: I'm hit! (health=60)EVIL_SHIP: I'm hit! (health=60)EVIL_SHIP: I'm hit! (health=50)EVIL_SHIP: I'm hit! (health=40)ship_evil: dominating!GOOD_SHIP: I'm hit! (health=50)EVIL_SHIP: I'm hit! (health=30)EVIL_SHIP: I'm hit! (health=20)EVIL_SHIP: I'm hit! (health=10)ship_evil: dominating!GOOD_SHIP: I'm hit! (health=40)GOOD_SHIP: I'm hit! (health=30)GOOD_SHIP: I'm hit! (health=20)ship_good: dominating!GOOD_SHIP: I'm hit! (health=10)GOOD_SHIP: I'm hit! (health=0)GOOD_SHIP: *BOOM*!!EVIL_SHIP: I WIN. BITCHThe winner is   ship_goodEvent System Test - DisplayEvent System Test - Stop

So yea, it's pretty much the same, and for the most part it is, except this is the LUA that's driving this:

winner = "none"ship_good_health = 100ship_evil_health = 100good_streak = 0evil_streak = 0while 1 do	dice = math.random(100)		if dice > 50 then			ship_good_health = ship_good_fire()		evil_streak = evil_streak + 1		good_streak = 0	else				ship_evil_health = ship_evil_fire()		good_streak = good_streak + 1		evil_streak = 0	end		if evil_streak > 2 then		evil_streak = 0		print("ship_evil: dominating!")	end	if good_streak > 2 then		good_streak = 0		print("ship_good: dominating!")	end--[[	print("ship_good_health: ", ship_good_health ) --]]--[[	print("ship_evil_health: ", ship_evil_health ) --]]	if ship_good_health <= 0 then		winner = "ship_evil"		break	end		if ship_evil_health <= 0 then		winner = "ship_good"		break	endendprint("The winner is ", winner)

Not the most elegant thing you've seen, but hey, it was just for fun... and what the hell is up with those LUA comments? that looks real ugly to me... oh well.

I guess you're wondering what the code side of things looks like... it's not that great either, but here it is:

int ship_good_fire(lua_State *L){	ship_good.m_evFire.Execute();	int health = ship_evil.health;		lua_pushnumber(L, health);	return 1;}

This is the main:

		// initialize Lua		L = lua_open();		// load Lua base libraries		lua_baselibopen(L);		luaopen_math(L);		// register our function 		lua_register(L, "ship_good_fire", ship_good_fire);		lua_register(L, "ship_evil_fire", ship_evil_fire);		// run the script		lua_dofile(L, "ships.lua");		// cleanup Lua		lua_close(L);

The rest is handled by the event system. This is all hacked in, so don't think too much of it, I'm just trying to learn how LUA can be used, and I'm still figuring out how to do a better event system.

I really wish LUA played nicer with C++, I'd love to just be able to do this:

lua_register(L, "ship_fire", SpaceShip, SpaceShip::Fire);

or something like that, I've looked into some of the libraries some people have made that better integrate C++ with LUA, but they all seem so cumbersome.

Yay Lua....

You could have the function call ShipFire() along with one boolean parameter to determine whether it's a good ship or a bad ship.

oh yea, all this is throw away code, I just wrote it as I went, so I'm sure there are at least 10 things I could've done better or differently.

I like what you did, how are you going to support C++ function calls and variables? I tried using tolua to support entire classes in LUA, but I couldn't get it to work last night.

## Create an account

Register a new account