Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Passing a table to C Func and back to Lua code — Gideros Forum

Passing a table to C Func and back to Lua code

GregBUGGregBUG Guru
edited February 2012 in Plugins
(feel free to move this topic if isn't in the correct category)

ok. i start to play with plugins...
this is the lua code...
with some var, a table...

call a C function "tntengine.alphamorph(color, ferro, ciccio)" this do some on the parametres passed and modify directly some filed of the table... then release 3 values...
this example works fine but...
require "tntparticlesengine"
 
color = 1000
ferro = 2000
ciccio = {Nome = "ciao",
	xPos = 10,
	yPos = 20,
	Color = 1.212,
	linee = {},
	spazi = 23
}
 
a,b,c =tntengine.alphamorph(color, ferro, ciccio)
 
print("-----")
print(a)
print(b)
print(c)
print(ciccio.xPos)
print(ciccio.yPos)
print("-----")
 
#include "lua.h"
#include "lauxlib.h"
#include "gideros.h"
 
static int alpha_morph(lua_State *L) {
    double color   = lua_tonumber(L, -3);
    double ferro    = lua_tonumber(L, -2);
 
    lua_pushstring(L, "Color");
    lua_gettable(L, -2);
    double spazi = lua_tonumber(L, -1);
   lua_pop(L, 1);
 
    color++;
    ferro--;
 
    lua_pushstring(L, "xPos");
    lua_pushnumber(L, 256);
    lua_settable(L, -3);
 
    lua_pushstring(L, "yPos");
    lua_pushnumber(L, 156);
    lua_settable(L, -3);
 
    lua_pushnumber(L, color);
    lua_pushnumber(L, ferro);
    lua_pushnumber(L, spazi);
    return 3;
}
 
static const struct luaL_Reg tntEngineFuncs[] = {
    { "alphamorph",	alpha_morph },
    { NULL, NULL }
};
 
LUALIB_API int luaopen_tntparticlesengine(lua_State *L) {
    luaL_register(L, "tntengine", tntEngineFuncs);  
    return 1;
}
stack index of parametres passed in the func are:

for 1^st "color" is -3 for "ferro" is -2

but why for the table "ciccio" is -2 and not -1 ?

i miss something... how work exactly the stack with simple parametres and tables ?

i pass an object how can access to it ?

there is a better way to change directly a value in the table than this :
    lua_pushstring(L, "xPos");
    lua_pushnumber(L, 256);
    lua_settable(L, -3);
what means really " lua_settable(L, -3);"

thanks.

and sorry for my continuous questions ...
but the possibility of using the plugin really interests me ...
...when I have more experience I hope to give something back to communities...

Gianluca.
TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
www.tntengine.com

Comments

  •  lua_pushstring(L, "Color");
        lua_gettable(L, -2);
    -1, -2, -3 etc is the top of the "stack". You push things on the stack, and pop them off.

    When you first go in, the table ciccio is presumably at -1, then you push the string "Color", so "Color is at -1 and ciccio is at -2 now.

    Have you read the Ultimate Guide, Chapter 8?
    http://giderosmobile.com/DevCenter/index.php?title=Ultimate_Guide_to_Gideros_Studio

    8.2 has a table indicating what 1,2,3 and -1,-2,-3 are.

    It is a very complex subject, and I am quite happy to acknowledge that I may not have presented it completely the best. However, it will be very useful to test it on an actual unsuspecting user :), so that we can make the Guide the best.

    The code in the guide is a bit fragmented, but you can download a working plugin with the Gideros project, and this has a few examples of various things you can do.

    http://www.giderosmobile.com/forum/discussion/433/sample-iphone-plugin

    I did write it for iOS though. Perhaps I should put in my signature that I know nothing about Android (insert dunce emoticon here :))

    Likes: atilim, GregBUG

    +1 -1 (+2 / -0 )Share on Facebook
  • yes, Caroline. i feel so stupid some times! (or often? ;) )
    I read the manual but in a hurry and at night... better to make things very quietly ...

    you have presented this subject very clearly!
    but it's my desire to learn that I care... and the fact that I do not understand English well ... (my mother language is Italian) and use google translator!

    PS: your example (and that one posted by MikeHart) is very good but i don't understand well Objective C. :(

    BTW, now the "stack" is clear... i need to make some practice with it! now!...

    ps: I'll probably still have some "stupid" questions...
    I warned you! :)


    thanks.

    ciao!
    Gianluca.
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited February 2012
    Hey don't forget me :)

    To access the parameters that are passed to a C function, it's better to use positive indices. I mean instead of
        double color   = lua_tonumber(L, -3);
        double ferro    = lua_tonumber(L, -2);
    use
        double color   = lua_tonumber(L, 1);
        double ferro    = lua_tonumber(L, 2);
    Also, it's better to use luaL_checknumber instead of lua_tonumber so that if the function parameter cannot be converted to number, Lua gives an understandable error.

    http://pgl.yoyo.org/luai/i/lua_tonumber
    http://pgl.yoyo.org/luai/i/luaL_checknumber

    Also using lua_setfield and lua_getfield is easier for changing a field of a table. I mean, instead of
        lua_pushstring(L, "xPos");
        lua_pushnumber(L, 256);
        lua_settable(L, -3);
    you can write:
        lua_pushnumber(L, 256);
        lua_setfield(L, -2, "xPos");
    Both ways are the same.

    http://pgl.yoyo.org/luai/i/lua_setfield
    http://pgl.yoyo.org/luai/i/lua_settable

    best,
  • atilimatilim Maintainer
    edited February 2012
    Also similar to lua_setfield/lua_getfield, there are lua_rawgeti and lua_rawseti to easily change the fields of a table with integer keys:

    http://pgl.yoyo.org/luai/i/lua_rawgeti
    http://pgl.yoyo.org/luai/i/lua_rawseti
  • To access the parameters that are passed to a C function, it's better to use positive indices.
    Why "better"? I tend to find working backwards from the top easier than relying on what's on the bottom.

    (But then I am backwards about many things.)

    Your other points are good ones. I always forget to use the checknumber instead of tonumber.

  • Hey don't forget me :)
    Never! You're always in my mind! :D

    thanks for the tips.!

    Likes: atilim

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    Because if someone passes more parameters than you've expected, using negative indices returns wrong parameters. I mean if the function is called as:
    a,b,c =tntengine.alphamorph(color, ferro, ciccio, 10, 20, 30)
    using negative indices refers to 10, 20, 30 instead of color, ferro, ciccio.



  • Hey don't forget me :)
    Never! You're always in my mind! :D
    Mine too - the Ultimate Guide personified :).
  • atilimatilim Maintainer
    I usually use positive indices while accessing function parameters and negative indices for all the other cases.
  • Because if someone passes more parameters than you've expected, using negative indices returns wrong parameters.
    Good point - I've always been careful that I've sent the right parameters, but I guess that if a plugin is for redistribution, then it should be as robust as possible. Thanks.

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.