Page 1 of 2

Questions on Using Lua in my Engine

Posted: Sun May 02, 2010 4:03 pm
by LeonBlade
Hey everyone,

I have a few concerns with how I would do this kind of integration in my engine.
My engine currently consists of a few things, there's the main Game class where all the magic happens.
Then there are other classes as well like Graphics etc.

My game has an instance of Graphics for example.
Now, I have a separate class for Lua called LuaInterp.
What I want to know is, if I have an instance of Graphics and also LuaInterp on my Game class,
how do I reference the Graphics from LuaInterp.

Also, I'm currently creating static functions for the lua scripts, example:

Code: Select all

lua_register(L, "CreateSprite", CreateSprite_Lua);

Code: Select all

int CreateSprite_Lua(lua_State *L)
{	
	// code goes here
}
EDIT: Also, how should I set up my function registers? I don't like using static functions like this...

Inside that function CreateScript_Lua I want to be able to reference it's Game parent and then the Graphics child so I can do the following:

Code: Select all

// grab the sprite image you're trying to load
std::string filename = lua_tostring(L, 1);
	
// create a Drawable
Drawable sprite;
	
// set the image to the one requested from Lua
// using ImageBank
sprite.SetImage( graphics.imageBank.GetImage(filename) );
	
// set the new id
int sprite_id = graphics.spriteStack.size();
	
// add to the sprite_stack
graphics.spriteStack[sprite_id] = sprite;

lua_pushnumber(L, sprite_id);
Yes I know the way I'm storing the images in the bank isn't the best

Can someone help me out here with this?
Thanks a lot.

Re: Questions on Using Lua in my Engine

Posted: Sun May 02, 2010 4:35 pm
by short
LeonBlade wrote:Hey everyone,

I have a few concerns with how I would do this kind of integration in my engine.
My engine currently consists of a few things, there's the main Game class where all the magic happens.
Then there are other classes as well like Graphics etc.

My game has an instance of Graphics for example.
Now, I have a separate class for Lua called LuaInterp.
What I want to know is, if I have an instance of Graphics and also LuaInterp on my Game class,
how do I reference the Graphics from LuaInterp.
Inside my Engine.h:

Code: Select all

#define ENGINE Engine::get_Instance() // this line is at the top of Engine.h

static inline Engine &get_Instance(){static Engine instance;return instance;} // return instance of Engine
I can use this in the following way (for example my main.cpp class):

Code: Select all

if(ENGINE.initialize(800, 600, 32, false, "PARTICLE Engine") )
    {
        ENGINE.run();
    }
or in Particle.cpp:

Code: Select all

glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluOrtho2D(0.0, ENGINE.get_Window_Width(), 0.0, ENGINE.get_Window_Height()); // setup 2d screen view
The only thing required is at the top of all my headers I put:

Code: Select all

#ifndef _ENGINE_H_
#include "Engine.h"
#endif
You can adapt this to your game class and have your graphics and LuaInterp include your game class.

Re: Questions on Using Lua in my Engine

Posted: Sun May 02, 2010 4:38 pm
by short
Inside that function CreateScript_Lua I want to be able to reference it's Game parent and then the Graphics child so I can do the following:
For this, you could modify what I did and do something like ENGINE.getGraphicsChild().graphicsMethod(); // graphicsMethod being some function of Graphics.h

Re: Questions on Using Lua in my Engine

Posted: Sun May 02, 2010 5:41 pm
by lotios611
[quote="LeonBlade"]
Also, I'm currently creating static functions for the lua scripts, example:

Code: Select all

lua_register(L, "CreateSprite", CreateSprite_Lua);

Code: Select all

int CreateSprite_Lua(lua_State *L)
{	
	// code goes here
}
Why don't you use toLua++? It should be able to handle anything you want.

Re: Questions on Using Lua in my Engine

Posted: Sun May 02, 2010 5:45 pm
by LeonBlade
I just started getting into Lua, so I really don't know much about any other libraries.
I'll look into it in a few.

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 10:53 am
by Milch
@LeonBlade
If you want a scripting language only for testing purposes, try AngelScript (google it).
Its a C/C++ style scripting language - and if build in correctly you can just copy&past the code from the script in the actual code.

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 10:55 am
by Falco Girgis
Milch wrote:@LeonBlade
If you want a scripting language only for testing purposes, try AngelScript (google it).
Its a C/C++ style scripting language - and if build in correctly you can just copy&past the code from the script in the actual code.
Same as with Lua. You can execute Lua scripts as simple strings in the engine.

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 12:29 pm
by short
have you had any luck leonblade?

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 5:43 pm
by LeonBlade
No... I'm trying to get both toLua++ and luabind installed on here (my Mac), and I'm having an ass of a time.
Both of them have to use stupid shit like scons or boost to build them. Why the fuck can't they just use makefiles :nono:

At any rate, I'll give it another go later perhaps...

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 6:49 pm
by Live-Dimension
I thought toLua++ had no such requirements?

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 7:19 pm
by Falco Girgis
LeonBlade wrote:No... I'm trying to get both toLua++ and luabind installed on here (my Mac), and I'm having an ass of a time.
Both of them have to use stupid shit like scons or boost to build them. Why the fuck can't they just use makefiles :nono:

At any rate, I'll give it another go later perhaps...
Whoah, what? Dude, I built toLua++ (for Windows) with Visual Studio 2008. If you're on a Mac, you could even just drag that shit and drop it into your XCode project. I'm sure LuaBind is the same. You don't need a makefile.

Re: Questions on Using Lua in my Engine

Posted: Mon May 03, 2010 7:28 pm
by avansc
Hi, well i dont know about the other one, but tolua++, well it CAN be a bitch to build it, however if you read it gets easier ;).

the easiest way is as falco, and infact the site suggests, just, just make a excode project, and then add 2 targets, one is the exe and the other will be the lib, i cant remember which one, but you need to NOT link one file to one of the targets. anyways, its pretty simple.

edit : http://lua-users.org/wiki/CompilingToluappWithoutScons
there, if you cant get it build not, then inno.

edit2:

quick tut for getting it installed on OSX, with scons.

step 1, download scons, http://www.scons.org/, extract, then run this command,
sudo python setup.py install
this will install scons and it will place it in the bin directories for you, you need to use SUDO.

step 2, download the lua source http://www.lua.org/ftp/lua-5.1.4.tar.gz, extract and run these commands.
make macosx
sudo make install
this will build and install everything where it needs to go.
just for safty, i installed lua via macports as well.

step 3, download and extract tolua++, then edit the config_posix.py file,
change
LIBS = ['lua', 'lualib', 'm']
to
LIBS = ['lua', 'liblua', 'm']

save, then just save, run there commands now. in the scons dir ofcourse
scons
sudo scons install

and voila, tolua++ should be build now. i think you can pass args to scons to get dynamic libs, as this method only build the static oness.

Re: Questions on Using Lua in my Engine

Posted: Tue May 04, 2010 5:03 am
by LeonBlade
Thanks a ton man, I got it working now... I just have no fucking idea what I'm doing haha.
Someone got a good link or reference on how the shit works?

I like, tried to get it to work, but like... I don't know I'm doing it wrong I think...

Re: Questions on Using Lua in my Engine

Posted: Tue May 04, 2010 1:54 pm
by avansc
Anytime mate, scons produced a error that pointed me in the right direction, it could not found lualib. anyways, glad it work, sorry im not to keen on tolua++ and lua for that matter, i spose falco and the lot are your best bet in that area.

btw, i like the new avatar.

Re: Questions on Using Lua in my Engine

Posted: Tue May 04, 2010 2:29 pm
by LeonBlade
Yeah, it means a lot man, I wasn't sure if I was going to get anywhere :nono:
I'll leave this open for anyone that wants to provide some good resources for toLua++

The site I found was confusing really... so I'd like a better explanation.