🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

LUA Question

Started by
6 comments, last by TristanDBS 20 years, 5 months ago
Well, I finally got LUA working, but it''s version 4.01. It can still do classes though by the following example: http://lua-users.org/wiki/SimpleCppBinding This is great that I can make C++ classes and use them in LUA, but how can I do the following: Make an instance of a C++ class in C++ Pass that instance to LUA Modify it in LUA Is it as easy something like just setting a global? So, far I can register C++ functions in LUA, vice versa and modify/set globals. This is my first time working with LUA so I may be overlooking something and I''ve actually just started improving in C++ over the past few days. So, any help is appreciated. Thanks.
Advertisement
I''m not sure what exactly you''re having a problem with. Is it the act of creating an object in C++ and exposing it to Lua that is the issue? If so, then you probably just need to look into the C API for adding objects to the stack, or the docs addressing modifying the table of globals, both of which are in the reference manual.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
I''ll look into that. I found an example of doing what I need, but it''s for version 5.0. So, now I just need to get a compilied version of 5.0.
Maybe this linke will help? Lua: technical note 5

Dave Dak Lozar Loeser

"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."
--anonymous
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
These excellent tutorials also included the libraries for 4.01 and 5.0 now:

http://tonyandpaige.com/tutorials/

I found an example for passing a refernece of a C++ class to LUA, but it''s not working properly. So, I''ll explain what I''m try to do in more detail. Basically, I can do this in VB.

Dim obj As clsNPC

Script.Execute("TestNPCFunc", obj)

This isn''t correct code, but I think you get the idea. It calls TestNPCFunc and passes obj as a parameter. Then I can get and set properties in the script. This is what I need to be able to do in LUA. Any suggestions?

And just to make sure I''m clear, I do not want to make an instance of a C++ class in LUA. I want to pass a created object to LUA and modify it. Thanks.
quote: Original post by TristanDBS
I found an example for passing a refernece of a C++ class to LUA, but it''s not working properly.

Could you be more specific and say in what way it doesn''t work?

quote: So, I''ll explain what I''m try to do in more detail. Basically, I can do this in VB.

Dim obj As clsNPC

Script.Execute("TestNPCFunc", obj)

So, given an object that already exists in your C++ code, you want to then call a Lua function that will perform some operations upon it, right? Ok, in that case, the stuff I mentioned previously is pretty much what you need, and knowing what exactly isn''t working would be useful.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Ok I finally found an example close to what I'm trying to acomplish.

http://lua-users.org/wiki/BindingWithMembersAndMethods

This allows me to use my struct, which I don't mind instead of class, in LUA. Again, this is mainly what I find examples on, but this is the best. I'm still not certain how I can pass an object into LUA and modify it though, since all this example shows is how to create and modify them in LUA. I need to create it in C/C++ and modify in both LUA and C/C++. I figure I can pass a pointer to the object I wish to modify, but how can I create the object in LUA from a pointer. The only way I can think of is to pass each variable in the struct and recreate it in LUA with the sample code, but then I have to pass it all back as well, which obviously isn't the best way. I'm sorry if I sound like a complete newbie, but I've honestly been searching all over off and on for a few days trying to find how to do this. To further explain what I want, I've written a little example:

C Code:
struct pos {    int x;    int y;};int main() {    // Lua init and stuff...    pos objTest;    // C function to execute PosUpdate in LUA.    // This is what I don't know yet.    // I've search for pushing an object on the stack    //   and found nothing.    // This also needs to update the C object in main    // so I'm sure I need returns too.    executePosUpdate(objTest);    cout << objTest.x << endl;} 


LUA Script:
quote:
function PosUpdate(obj)
obj.x = 1
obj.y = 1
end


Again, sorry for repeating myself and continuing this topic, but I've been search and trying to figure this out a lot and have had no luck. Thanks for atleast still relpying.

[edited by - TristanDBS on January 20, 2004 7:35:27 PM]
But did you look into what I said about putting things on the Lua stack? Or about setting members of the globals table?

This topic is closed to new replies.

Advertisement