我正在创建一个基于 Lua 脚本中调用的函数播放音乐的应用程序。我称之为音乐。问题是我需要一个不需要括号的函数。像这样:
play note("A")
这是我的完整代码:
#include <iostream>
#include <string>
extern "C"
{
#include "../lua/include/lua.h"
#include "../lua/include/lauxlib.h"
#include "../lua/include/lualib.h"
}
/*
Message codes
M8I5H: Information
MSKIE: Syntax error
M3UET: Unknown error
*/
enum class MUSICA_NOTE_ENUM
{
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
};
int musica_play(lua_State *L)
{
// Not added yet
return 1;
}
int musica_note(lua_State *L)
{
std::string note = lua_tostring(L, 1);
lua_pushnumber(L, (lua_Number)((MUSICA_NOTE_ENUM)(note.at(0) & 31)));
return 1;
}
int main(){
std::string music = R"(a = 75
play note("A")
play note("B")
play note("C")
-- play melody(melody_piono_tune)
)";
std::string m = "play note(\"A\")";
lua_State *L = luaL_newstate();
lua_register(L, "note", musica_note);
lua_register(L, "play", musica_play);
int r = luaL_dostring(L, m.c_str());
if(r == LUA_OK)
{
}else{
printf("[Line: %d, File: %s, MessageCode: MSKIE] MUSICA: There was a problem interperting the file:\n%s\n\n", __LINE__, __FILE__, lua_tostring(L, -1));
}
}
如何使播放功能无括号?
先感谢您