Sign in to follow this  
Deliverance

Ways to improve my current design

Recommended Posts

I've been looking for a way to improve my current design using lua scripting and i'm having troubles balancing what goes into script code and what goes into C/C++ code. My current design is based on states. Basically i have this class:
class CGameState
{
public:	
	virtual void Init()			    = 0;
		
	virtual void Cleanup()		    = 0;
	
	virtual void Render()		    = 0;
		
	virtual void Update( float dt ) = 0;

	virtual void Pause()            = 0;

	virtual void Resume()           = 0;

	virtual void KeyPress(int key, int state, int x, int y) = 0;
																			
	virtual void MousePress(int button, int state, int x, int y) = 0;
	
	virtual void MouseMove(int x, int y) = 0;

protected:
	CGameState() { }
};


For every screen in the game i create a state. For example, for the MainMenu i write something like this:
class CMainMenu: public CGameState
{
     ...
}


Also for the actual game, i create a state named CGame. Every state has control over all other states existent. Each state can push or pop other states using the CGameEngine object,and all the states go in a vector managed by a CGameEngine object. I've been using this design until now and of course besides using these states i also created various other specific classes to help me. As mentioned in the beginning of the post i'm looking for a way to move game specific code into script code using lua. I never used lua until now and the only knowledge i have is from reading some introductory articles about lua. So here's how i thought i can do: Every state has associated a script that implements all the functions of the CGameState abstract class. So a state would be created with one single line call like this: CGameEngine::CreateState(stateName, "somename.lua"). This could work very well, but now what about the other code specific to the game, how do you decouple the code that goes into C++ code and the one that goes into C code? What do you think of my current design, how can i improve it to make good use of lua?

Share this post


Link to post
Share on other sites
From my experience, be careful in giving too much control to Lua. I find that it quickly becomes very difficult to manage everything if you do. In my opinion, Lua scripts should describe functionality for a c++ function and should hold as little ownership over objects as possible.

Functional logic that could do with tweaking and value parameters that could do with tweaking is typical stuff I'd put inside a lua function.

Take for instance the algorithm for calculating battle damage when hit by a bullet

c++:
Player::OnDamage(float dmgValue)
{
//1) Do funky lua state pushing and send dmgValue, along with some player specific data, off to the lua function OnDamageL
//2) Fetch result
//3) Update Player variables
}

lua:
function OnDamageL(dmgValue, entityHP, entityAP)
-- Do funky damage algorithm
return result
end


I'd be very careful in giving too much control over to Lua, but I'm sure there are other opinions around.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this