开发者

How to check if parameter passed to Lua is of user defined type?

I am implementing scripting for my Ogre3d based application using Lua and I have encountered a problem with checking whether a parameter fed into function is of particular type - Ogre::SceneNode*. Anybody knows how can I do it ?

There are some basic Lua functions doing this for built in types like int or string e.g.

if(lua_isnumber(L,1)) {...}

but I do not k开发者_Go百科now how to do it with user defined types.


If you arrange for each of your userdata of a particular type to share a metatable, then you can use luaL_checkudata to confirm their type. This is typically how a library tags and identifies the data it creates.

Here are some functions that create and check userdata using this technique:

static decContext *ldn_check_context (lua_State *L, int index)
{
    decContext *dc = (decContext *)luaL_checkudata (L, index, dn_context_meta);
    if (dc == NULL) luaL_argerror (L, index, "decNumber bad context");
    return dc; /* leaves context on Lua stack */
}

static decContext *ldn_make_context (lua_State *L)
{
    decContext *dc = (decContext *)lua_newuserdata(L, sizeof(decContext));
    luaL_getmetatable (L, dn_context_meta);
    lua_setmetatable (L, -2); /* set metatable */
    return dc;  /* leaves context on Lua stack */
}

The metatable was created with

const char *dn_context_meta = "decNumber_CoNTeXT_MeTA";
luaL_newmetatable (L, dn_context_meta);


I guess lua_isuserdata(L, yourParam) ?

Would be logical.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜