Advertisement

proper use of lua...

Started by October 16, 2003 05:40 PM
4 comments, last by no one 21 years, 1 month ago
Hiya I have a question about lua... but first, heres what I want to do. ex script file... newobject1 = AddMeshObject("rock.3ds"); pos = vector3(-200,20,-10); SetPosition(newobject1, pos); etc... how do you register classes with lua? And also, ive tried to register class functions like so... class functions: CSomeClass::Foo(lua_State *L) and then calling lua register like so... CSomeClass sc; lua_register(g_lstate, "Foo",sc.Foo); Its says that It cannot register it basically because its not a c function. Hmmm... Thanks for any feedback on this issue.
"Make it a habit to be loyal to the activities that serve the highest part of yourself."
It is correct, that isn''t a C-function its a C++ function (a member function at that!)

search for "Luna" or "Lunar" for a C++ template that does what you want.
What is Barrel Drop?
Who are HTS Games?
Advertisement
You might try using tolua. There are other tools out there for binding C/C++ code for calling from Lua, but tolua is the one I''m familiar with. It''s purpose is to handle the dirty work of registering functions and classes to Lua, so that you don''t have to.

Basically, in tolua you create a "stripped down" version of any class header file you wish to expose to Lua, containing the class declaration and any member functions you wish exported. You can export class definitions, functions, constants, enumerations and #defines, derived classes, etc... and tolua will generate a source code file to be compiled and linked with your application, that contains all of the bindings and registering required, so that you don''t have to slog through a lot of the grunt work.

tolua binds the constructors and destructor as well, so in the case of your example, it would be perfectly viable to write Lua code as:

newobject1=AddMeshObject("rock.3ds")
pos=vector3:new(-200, 20, -10)
SetPosition(newobject1, pos)

as long as the functions AddMeshObject() and SetPosition() were declared in the "stripped down" header fed to tolua, as well as the class/struct vector3. Take a look at tolua here for more information.

I have found tolua to be extremely useful in implementing the scripting for my game, and I use it for all sorts of things, from game configuration to scripting of items, skills, enemies, etc...Any time a treasure chest in Golem dumps treasure, or an enemy shoots a fireball, it is thanks to the work tolua has saved me.

Good luck.


Josh
vertexnormal AT linuxmail DOT org

Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.
Well, stay away from the touristy luaus...they give away cheap mai tais and the food isn''t very good. Look for the smaller luaus recommended by the locals. I heard Maui has good ones.

Oh wait...lua.

;-)

--
Dave Mikesell Software & Consulting
Cool thanks alot guys!

I was worried that I would have to make 5000000 c functions
for binding my classes with lua. I never heard of the toolkits
mentioned.

The last post, #3..stay away from lua? I wasn'r sure if thats
what he ment.

Thanks again, hopefully ill have some screens of my game engine
up in a few weeks. I had another question now, I was planning on using interface classes for my object classes in which would be used to set variables for the objects.

ex:
class CObject...   vector3 position;};class CObjectMovement...   void SetPosition(CObject *obj, vector3 pos)   {     obj->position = pos;   }Ok, now if I make a "striped down" version of the classincluding variables and functions etc, should I just setvariables like this:script file:newobject1.position = vector3(-200,20,-10);newobject1.blendstyle = EBS_TRANSPARENT;etc...that vs:pos = vector3(-200,20,-10);SetPosition(newobject1, pos);SetBlendStyle(newobject1,EBS_TRANSPARENT);etc...

Hmmm...

Thanks alot.



[edited by - no one on October 16, 2003 8:08:43 PM]
"Make it a habit to be loyal to the activities that serve the highest part of yourself."
Alrighty, Just read a bit about tolua and downloaded it.
It looks really cool and easy to use. What I got from reading
the document was that with using tolua you could basically
export all of your engine classes if you wanted and then
say make the game code in script. This seems like the approach
that Epic used with the unreal engines. (except they didn't use
lua).

Man, now I have a ton of ideas and im even thinking that ill
have to make a design change. Very cool stuff!

[edited by - no one on October 16, 2003 8:32:53 PM]
"Make it a habit to be loyal to the activities that serve the highest part of yourself."

This topic is closed to new replies.

Advertisement