keywords: Lua, Personal Ordinary Notes

Documents

Quick Start

LUA 在 C++ 中快速创建 LUA 中带元表的自定义数据类型变量
https://blog.twofei.com/698/

Lua使用实记
http://www.luzexi.com/2015/01/21/Lua%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%AE%B0

Libraries List

Debug

Lua IDE/Debugger Plugin for IntelliJ IDEA
https://github.com/EmmyLua/IntelliJ-EmmyLua

Remote debugger for Lua.
https://github.com/pkulchenko/MobDebug

lua debug and code tools for VS Code
https://github.com/Tencent/LuaPanda

This is the issues tracking, roadmap, docs src repo of the x-studio IDE.
https://github.com/simdsoft/x-studio.github.io

Binding

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it’s great!
https://github.com/ThePhD/sol2

C++ to LUA binding tool in a single header file.
https://github.com/gengyong/luaaa

luabind
https://github.com/luabind/luabind

Lua C++ wrapper, an easy way to add lua scripting capabilities to an existing C++ project.
https://github.com/jordanvrtanoski/luacpp

JIT

An ongoing attempt to re-engineer LuaJIT from scratch
https://github.com/luajit-remake/luajit-remake

Common APIs

How to get the first item of dictionary
local a = {}
a["test"] = 5
a["asdjh"] = "something"

local key, value = next(a, nil)
print(key, value)

Origin:
https://devforum.roblox.com/t/find-the-first-item-of-a-dictionary/637131/3

How to reload lua scripts on the fly

If (a) the Lua scripts are in modules, and (b) the modules don’t affect globals or tables outside the scope of the module, you can use package.loaded.????? = nil to cause require to reload the module:

> require "lsqlite3"
> =sqlite3.version
function: 0x10010df50
> sqlite3.version = "33"
> return sqlite3.version
33
> require "lsqlite3"
> return sqlite3.version
33
> package.loaded.lsqlite3 = nil
> return sqlite3.version
33
> require "lsqlite3"
> return sqlite3.version
function: 0x10010c2a0

Similarly, if the non-module scripts are well behaved in that they (a) only define a single table, and (b) don’t affect globals or other tables, then simply reloading the script will work as well.

Origin: What is a way to reload lua scripts during run-time?
https://stackoverflow.com/a/2812556/1645289

How to run lua script file at runtime

Lua reload example
https://github.com/dawnarc/lua_examples/tree/main/02.reload

Constructor Overload

Expected:

apples()
{
    taste="yum";
    amount = 0;
}
apples(string taste, int num)
{
    taste=taste;
    amount=num;
}

Solution:

function apples(taste, num)
  taste = taste or "yum"
  amount = num or 0
  -- ...
end
How to return multiple arguments from Lua

Example: main.c

static int myFunc(lua_State *state)
{
    lua_pushnumber(state, 1);
    lua_pushnumber(state, 2);
    lua_pushnumber(state, 3);

    return 3;
}

main.lua:

local a,b,c = myFunc()

return several parameter from lua C function
https://stackoverflow.com/a/19544634/1645289

Issues

LNK2019: unresolved external symbol luaL_newstate

Error on building:

error LNK2019: unresolved external symbol "void __cdecl lua_close(struct lua_State *)" (?lua_close@@YAXPEAUlua_State@@@Z) referenced in function main
error LNK2019: unresolved external symbol "char const * __cdecl lua_tolstring(struct lua_State *,int,unsigned __int64 *)" (?lua_tolstring@@YAPEBDPEAUlua_State@@HPEA_K@Z) referenced in function main
error LNK2019: unresolved external symbol "int __cdecl lua_pcallk(struct lua_State *,int,int,int,__int64,int (__cdecl*)(struct lua_State *,int,__int64))" (?lua_pcallk@@YAHPEAUlua_State@@HHH_JP6AH0H1@Z@Z) referenced in function main
error LNK2019: unresolved external symbol "int __cdecl luaL_loadfilex(struct lua_State *,char const *,char const *)" (?luaL_loadfilex@@YAHPEAUlua_State@@PEBD1@Z) referenced in function main
error LNK2019: unresolved external symbol "struct lua_State * __cdecl luaL_newstate(void)" (?luaL_newstate@@YAPEAUlua_State@@XZ) referenced in function main
error LNK2019: unresolved external symbol "void __cdecl luaL_openlibs(struct lua_State *)" (?luaL_openlibs@@YAXPEAUlua_State@@@Z) referenced in function main

Solution:

You have choices:
Change the file extension of this file to .c instead of .cpp, so that it runs through the C compiler instead of the C++ compiler (caveat: depending on compiler)

or
Add extern “C” around the includes like this:

extern "C"
{
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
}

or

#include "lua.hpp"

Origin:
https://stackoverflow.com/a/23030232/1645289


“Every form of addiction is bad, no matter whether the narcotic be alcohol, morphine or idealism.” ― Carl Gustav Jung