Skip to main content
gamedev.net
🔒 Locked

Lua and C++

Started by Maega Dec 19, 2002 at 1:04 PM 8 replies 850+ views
Original Post
Maega
Maega
Hey guys, I have a class for controls. One of the members of this class is for a Lua function that will be called when that control is accessed. Does anyone know how to bind a Lua function to a C++ class? I know how to bind a function to Lua, just not the other way around .
krumms
krumms
Look on the LUA home page for Luna.

I've never used it, but it sounds like what you're after.

Tom

[edited by - krumms on December 19, 2002 9:43:52 PM]
refrain_from_stupidity( &me );
krumms
krumms
Ah wait maybe not - misread the question.

You''ll most likely have to fool around with the stack - exactly how depends on what you''re trying to do.

Tom
refrain_from_stupidity( &me );
Maega
Maega
Had me all excited =P

What Im trying to do is allow the scripter to define an action for a button (which is created inside a C++ class). So, upon creation of the button, a Lua function is passed to the button, so it knows what to execute when it''s clicked.

Now.. getting it to call the function is what Im stuck on
krumms
krumms
hmm what you''re saying doesn''t quite make sense, but I think I get what you''re talking about.

So when you CLICK the button, you want an Lua function to be called?

I''ve done this before, it''s not too hard so far as I can remember *sound of shuffling through manuals*

Okays here''s what you''ve got to do:

1. Compile your Lua script using lua_dofile (after initialising an lua_State of course)
2. Call the lua function. lua_call does this. You''ll need to push any arguments onto the lua stack before doing this. The first parameter is the name of the function you wish to call. If your Lua function takes not arguments, however, pass 0 as the second parameter. You''ll need to tell it how many results will be on the stack after the call too.

And that''s it. Easy huh?

Tom L
refrain_from_stupidity( &me );
Maega
Maega
Ok.. so basically all I need to do is pass the name of the function to the C++ class and use lua_call to call the function.

That is easy.. don''t know why I didn''t think of it..
krumms
krumms
http://www.lua.org/manual/manual.html

For future reference.

Tom L
refrain_from_stupidity( &me );
krumms
krumms
Be careful when saying "passing to a C++ class" - you can''t really pass anything to a C++ object, and strictly speaking you certainly can''t pass anything to a class!

What you can do is pass things to C++ methods, or functions. Make sure you distinguish, it will be confusing to understand your questions/statements otherwise.

Tom L
refrain_from_stupidity( &me );
Maega
Maega
Well.. sorry

Im tired =P
krumms
krumms
No worries. Happy hacking
refrain_from_stupidity( &me );

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.