Sign in to follow this  
anylo

[LUA, C++] Managing game states

Recommended Posts

Hi, here's yet another stupid question from a (Lua) newbie: I recently found this article about managing game states with C++. Is it a) possible to derive(?) those states from GameState with Lua b) if it is, how it's done? EDIT: Stupid url EDIT2: "Stupid url" means "Darn, I cannot get the syntax right for the url. [Edited by - anylo on May 24, 2007 4:24:28 PM]

Share this post


Link to post
Share on other sites
Here's example how game states can be handled when using Ogre 3D library.

I was wondering that is it possible to create abstract game state main class with C++ and derive those different states (intro, options, main manu etc) with Lua (using tolua(++))? I haven't use scripting before so I really would like to know is it even possible or should I stick to C++ when creating those states and inject Lua into something more specific parts.

Share this post


Link to post
Share on other sites
Well, let's simplify more:

class Shape {
public:
virtual double getArea() = 0;
};


Can I derive square/circle etc. from Shape with Lua?

Share this post


Link to post
Share on other sites
Yes, you can, as to how depends on what C++-Lua glue you are using.

Of course, the other question is why? Why not handle all the state logic in lua itself?

I had a (slighly hacked up I admit) system running where the Java (in this case, it was an assignment) basically called a Lua 'update' function which called the correct lua state internally also handled state transitions.

*digs out Lua code*

-- Calls the current update state
function update()
local instance = currentstate.instance
local func = currentstate.checkStateFunc
if func(instance)
then
currentstate = instance.nextstate
instance = currentstate.instance
end
func = currentstate.updateFunc
func(instance,sys:currentTimeMillis())
end

-- Calls the current render state
function render()
local instance = currentstate.instance
local func = currentstate.renderFunc
func(instance)
end



OK, it's not the best system in the world (there is a whole mess of dependancy between each 'state' table which could do with cleaning up via a proper system) but it worked [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
Yes, you can, as to how depends on what C++-Lua glue you are using.


Probably tolua(++) (if I understood what you meant).

Quote:
Of course, the other question is why? Why not handle all the state logic in lua itself?


Good question. Does anyone else have good examples about that (I don't mean that phantom's example was bad but it's always nice to is everybody doing it same way).

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