pugnator
pugnator

Reputation: 715

Get table as auto argument when calling C function from the same table field

I have several global integer variables, like A0, A1, A2 in Lua script. They are declared on C side. Each of them contains unique numeric value.
In a script user manipulates device pins using this aliases:

set_pin_bool(A0, 1) 

And this calls corresponding C function. I think it is too C-like and not very handy. The better is to call methods like A0.hi(), A0.low(), A0.set(1) etc.
So I tried to declare A0 and others as tables in C(this is just a struct):

lua_newtable(this->luactx);     
lua_pushstring(this->luactx, "val");
lua_pushinteger(this->luactx, value);
lua_rawset(this->luactx, -3);
lua_setglobal ( this->luactx, "A0" );

I can create a filed like hi and using lua_pushcfunction register it. But when I call A0.hi(), on C side I won't be able to access a table it was called from to get another fields. And as I googled there is no way to get something like self from C. Is there any way to accomplish it? I don't want to pass table itself as argument, like A0.hi(A0)
There may be a lot of aliases and they can be named differently.
Or may be there are different approaches to the same goal?

Upvotes: 3

Views: 125

Answers (1)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26549

Call your hi function like this:

A0:hi()

This is equivalent to:

A0.hi(A0)

Upvotes: 4

Related Questions