Horde3D

Next-Generation Graphics Engine
It is currently 28.03.2024, 11:06

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: 01.05.2009, 11:45 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
I'm trying to call C functions from Lua like this :
Code:
class app
{
   NodeHandle envRoot;

   // setting envRoot

   init()
   {
      // init lua
      lua_register(Lua, "getNodeHandle", getNodeHandle);
   }
   
   static int getNodeHandle (lua_State *L)
   {
      if (lua_tostring(L,-1) == "Model")
         if (Horde3D::findNodes(envRoot, lua_tostring(L, -2), SceneNodeTypes::Model) == 1)
            lua_pushnumber(L, Horde3D::getNodeFindResult(0));
      return 1;
   }
};

But I'm getting the following error :

error C2597: illegal reference to non-static member 'app::envRoot'


Last edited by Siavash on 02.05.2009, 03:31, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 13:10 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
You can't access member variables of a class from a static context without an instance of that class.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 13:53 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
But how to solve this problem ?! I'm going to have a look at my old C++ book :)


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 13:55 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
One solution would be to make all class members static. But in this case you could also use a namespace instead, because a class having only static members does not make really sense, does it?


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 14:07 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Whats the advantage of using namespaces or making all of the functions static?


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 14:31 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
I'm not an expert regarding compilers, so I can't tell you what will be the difference on the generated code, but if you make all members static, I think the result will be nearly the same as if you are using namespaces.

It's more a matter of style/design. If you create a class, one would expect that there could be several instances of this class with all the things classes provide in the context of object orientated programming (like inheritance, etc.). If you are making all members static you don't have those capabilities so IMHO it does not make too much sense to put all those functions into a class.

If you want several instances of a class and use it within lua, you could also pass a pointer of the instance to lua and get it from the stack when calling a lua function. Then you can cast it in your C-lua function and use it. But that's all depending on what you are actually try to do with lua and C++.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 14:46 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Volker wrote:
It's more a matter of style/design. If you create a class, one would expect that there could be several instances of this class with all the things classes provide in the context of object orientated programming (like inheritance, etc.). If you are making all members static you don't have those capabilities so IMHO it does not make too much sense to put all those functions into a class.
I'm using only one instance of app class at whole game. The second way looks amazing too.

EDIT : I've tried to define the envRoot as "static NodeHandle envRoot" but this causes some Linker problems. I want to call app member functions from Lua.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 16:16 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Fixed that problem by changing "static int getNodeHandle()" to "int getNodeHandle()", but I'm getting another error :
Code:
error C3867: 'app::getNodeHandle': function call missing argument list; use '&app::getNodeHandle' to create a pointer to member

And by changing 'app::getNodeHandle' to '&app::getNodeHandle', I'm getting another error :
Code:
error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'int (__thiscall app::* )(lua_State *)' to 'lua_CFunction'
        There is no context in which this conversion is possible


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 17:11 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
Because you can't cast class member functions to C functions. Since the methods of a class are bound to the instance of the class, the compiler can't know the address of the function at compile time.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 17:13 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Isn't there anyway to do this? Perhaps you have experienced similar problems when coding the Horde3D Editor. IMHO this will make the Lua too useless when we can't call C functions from Lua easily.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 17:17 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
You always need the instance of the class if you want to call pointers of class methods. To solve the lua problem you might want to take a look at the lua bindings within the editor. Doing a google search on lua and c++ might help you also.

Quote:
IMHO this will make the Lua too useless when we can't call C functions from Lua easily.

You can call C functions from Lua very easily, the problem is that you don't have C functions but C++ class methods.


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 17:26 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Volker wrote:
You always need the instance of the class if you want to call pointers of class methods. To solve the lua problem you might want to take a look at the lua bindings within the editor. Doing a google search on lua and c++ might help you also.
I've googled a lot about this problem, but couldn't find anything useful. I'll have a look at bindings. Thanks for help :wink:


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 17:34 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
Maybe this helps: http://loadcode.blogspot.com/2007/02/wrapping-c-classes-in-lua.html


Top
 Profile  
Reply with quote  
PostPosted: 01.05.2009, 18:01 
Offline

Joined: 21.08.2008, 11:44
Posts: 354
Indeed an interesting article :D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group