toLua++ problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

toLua++ problem

Gary Popov
Hi there, I'm screwing around with tolua++ and generating some code binds and so on. I have a small function call my_func() and it does almost nothing. This function is declared in a header, 'test.h'

---------------------------------------------------------
//tolua_begin
void my_func(int blah);
//tolua_end

---------------------------------------------------------
using tolua++, I generate a .cpp file for this --
---------------------------------------------------------
/*
** Lua binding: test
** Generated automatically by tolua++-1.0.92 on 09/06/06 00:26:55.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua++.h"

/* Exported function */
TOLUA_API int  tolua_test_open (lua_State* tolua_S);

#include "test.h"

/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
}

/* function: my_func */
#ifndef TOLUA_DISABLE_tolua_test_my_func00
static int tolua_test_my_func00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isnumber(tolua_S,1,0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,2,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  int blah = ((int)  tolua_tonumber(tolua_S,1,0));
  {
   my_func(blah);
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'my_func'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_test_open (lua_State* tolua_S)
{
 tolua_open(tolua_S);
 tolua_reg_types(tolua_S);
 tolua_module(tolua_S,NULL,0);
 tolua_beginmodule(tolua_S,NULL);
  tolua_function(tolua_S,"my_func",tolua_test_my_func00);
 tolua_endmodule(tolua_S);
 return 1;
}


#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
 TOLUA_API int luaopen_test (lua_State* tolua_S) {
 return tolua_test_open(tolua_S);
};
#endif

---------------------------------------------------------
and then after a bit of poking around, I have a main.cpp file
---------------------------------------------------------
#include <iostream>
#include <lua.hpp>
#include "test.h"
using namespace std;

extern "C"
{
    int  tolua_test_open (lua_State*);
}

void my_func(int blah)
{
    cout<<"blah is "<<blah<<endl;
}

int main()
{
    lua_State* L = lua_open();
    luaL_openlibs(L);
    tolua_test_open(L);
    luaL_dofile(L,"test.lua");
}

---------------------------------------------------------

It looks all fine and dandy, doesn't it? Well, I go to compile this in MSVC8.0 and you know what I get? An ugly "unresolved external symbol _my_func referenced in function _tolua_test_my_func00" linker error. What's causing it is this line (which, if I comment out, lets the program compile successfully)

  tolua_function(tolua_S,"my_func",tolua_test_my_func00);

What am I supposed to do make the program compile succesfully?

Also, another side question, let's say I have a game API that's exposed to lua and have a number of scripts which all have a number of functions.. something like OnStart(), where the lua script uses the API to create some objects.. and then another function OnUpdate() where the lua script uses the API to manipulate the object around. How do I call the right onstart/onupdate function? I have a list of the script file names... but how do I call a specific function defined in one file? I should probably use OO for this, huh? How would you go about doing that though?

Reply | Threaded
Open this post in threaded view
|

Re: toLua++ problem

Fabian Peña
Put in your test.h

#ifdef __cplusplus
extern "C" {
#endif
void my_func(int blah);
#ifdef __cplusplus
}
#endif



And for calling to a globals functions that being loaded from your
scripts use some like this

by example in lua have

function OnInitialUpdate(str)
    print("Hello",str);
   
end

Call it from C
 
    lua_getglobal(L,"OnInitialUpdate");
    if(lua_type(L,-1) == LUA_TFUNCTION)
    {
       lua_pushstring(L,"Function Argument");      //OnInitial update
have 1 argument
       lua_call(L,1,0);   //Call function with 1 argument and not expect
any result
    }else
       lua_pop(L,1);   //pop any other type , by example nil if
OnInitialUpdate non exist;

And of course read PIL

Sorry by any errors , and poor english too.


Reply | Threaded
Open this post in threaded view
|

Re: toLua++ problem

Sam Roberts-2
On Wed, Sep 06, 2006 at 09:34:11AM -0300, Fabian Peña wrote:
> And for calling to a globals functions that being loaded from your
> scripts use some like this
> 
> by example in lua have
> 
> function OnInitialUpdate(str)
>     print("Hello",str);
>    
> end
> 
> Call it from C
>  
>     lua_getglobal(L,"OnInitialUpdate");

If you have a number of scripts, all defining OnInitialUpdate(), I think
you have to set the "global environment" (getfenv/setfenv) before
loading and calling the script/block, so that the functions don't all
get assigned to the same global variable.

Just a hint, maybe somebody who has done this can be more specific.

Cheers,
Sam