Questions on Using Lua in my Engine

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Questions on Using Lua in my Engine

Post 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.
There's no place like ~/
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Questions on Using Lua in my Engine

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Questions on Using Lua in my Engine

Post 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
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Questions on Using Lua in my Engine

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Questions on Using Lua in my Engine

Post 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.
There's no place like ~/
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: Questions on Using Lua in my Engine

Post 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.
Follow me on twitter!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Questions on Using Lua in my Engine

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Questions on Using Lua in my Engine

Post by short »

have you had any luck leonblade?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Questions on Using Lua in my Engine

Post 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...
There's no place like ~/
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Questions on Using Lua in my Engine

Post by Live-Dimension »

I thought toLua++ had no such requirements?
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Questions on Using Lua in my Engine

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Questions on Using Lua in my Engine

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Questions on Using Lua in my Engine

Post 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...
There's no place like ~/
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Questions on Using Lua in my Engine

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Questions on Using Lua in my Engine

Post 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.
There's no place like ~/
Post Reply