首页 > 解决方案 > What are the functions that look like table in Lua?

问题描述

I'm a beginner in Lua and I just found out some functions look like table (e.g. function love.update(dt)) in Lua.

I wonder how these functions work. For example, does update function belong to table love? If so, should one construct this table somewhere beforehand?

Lastly, I would like to ask how to call these kind of functions from C++. (Please show me an example how to call love.update() from C++)

标签: c++functionlualua-table

解决方案


问题一:更新功能属于表爱吗?
是的。“function love.update(dt)”等于“love.update = function (dt)”。

问题 2:是否应该事先在某处构建此表?
是的。

问题 3:如何从 C++ 中调用这些函数?
我假设可以从全局访问“爱”。

void call_love_update (lua_State* l) {
    lua_getglobal(l, "love");
    lua_getfield(l, -1, "update");
    lua_pushnumber(l, 0.016);
    lua_call(l, 1, LUA_MULTRET);
}

推荐阅读