Jump to content
  • Advertisement
Sign in to follow this  

Ways to improve my current design

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
	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;

	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

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

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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!