lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Flávio Alberto once stated:
> Excuse-me, my last e-mail was sent in HTML mode, set in simple text mode...
> 
> Hello all
> I'm a C/C++ programmer and new to Lua, I'm need a requirement on my
> application, I need that a lua script access a previous created global
> table and inside this table I need to have more three tables,  these
> table creation was made in C system using this code :
> 
> 
> static int l_index(lua_State* L)
> {
>   cout << __FILE__ << " : " << __FUNCTION__ << endl;
>   return 1;
> }

  Here you are returning nothing on the Lua stack.  As written, this should
be 

	static int l_index(lua_State* L)
	{
	  cout << __FILE__ << " : " <<__FUNCTION__ << endl;
	  return 0;
	}

> static int l_newindex(lua_State* L)
> {
>     cout << __FILE__ << " : " << __FUNCTION__ << endl;
>   return 1;
> }

  Lua does not expect any returns for the __newindex metamethod, so you
should "return 0" here as well.

  [ snip ]

> When running the folowing script :
> 
> print(">> type(mastertable)="..type(mastertable))
> print(">> type(mastertable.table1)="..type(mastertable.table1))
> print(">> type(mastertable.table2)="..type(mastertable.table2))
> print(">> type(mastertable.table3)="..type(mastertable.table3))
> 
> mastertable.table1.a = 7
> mastertable.table1.b = {}
> 
> print(">> mastertable.table1.a="..mastertable.table1.a)
> 
> 
> I get the following messages on terminal :
> >> type(mastertable)=table
> >> type(mastertable.table1)=table
> >> type(mastertable.table2)=table
> >> type(mastertable.table3)=table
> >> mastertable.table1.a=7
> 
> I look that my C code created the tables OK, but l_index()
> l_newindex() were not called.
> Whats the problem with the registration of these calls ? What is the
> correct way to do this ?

  You are not setting the metatable on the tables you created.   

  -spc