Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call the function / Library of Candlespace + by Lua

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

For this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Test.cpp file

/ * Lua calls Clua_State + function / library (function stack method) * / # includeusing namespace std;#include/* when we need to call Cmax Candle + function in Lua, all functions must satisfy the following function signature: typedef int (* lua_CFunction) (lua_State * L); in other words, all functions must accept a lua_State as an argument and return an integer value. Because this function takes the Lua stack as an argument, it can read any number and type of parameters from the stack. The return value of this function indicates how many return values are pushed into the Lua stack when the function returns. (because Lua's function can return multiple values) * / static int math_abs (lua_State * L) {lua_pushnumber (L, abs ((int) luaL_checknumber (L, 1); / / get the passed parameter return 1;} static int math_cos (lua_State * L) {lua_pushnumber (L, cos ((double) luaL_checknumber (L, 1)); return 1 } static int math_sin (lua_State * L) {lua_pushnumber (L, sin ((double) luaL_checknumber (L, 1)); return 1;} static int ShowMessage (lua_State * L) {lua_pushnumber (L, 1000); printf ("show message and push 1000\ n"); return-1 } / / Register function void regist_function (lua_State * L) {/ / set a global function name that can be called by lua lua_pushcfunction (L, ShowMessage); lua_setglobal (L, "showmessage"); / / c call lua lua_getglobal (L, "SHOWMESSAGE"); lua_pcall (L, 0,0); printf ("get the showmessage pushed value% f\ n", lua_tonumber (L,-1)) / / # define lua_register (L, (f)) (lua_pushcfunction (L, (f)), lua_setglobal (L, (n) / / lua_register is defined as above, all lua_pushcfunction (L, ShowMessage); lua_setglobal (L, "showmessage"); lua_register (L, "showmessage", ShowMessage); lua_register (L, "cos", math_cos); / / Test lua_getglobal (L, "COS") Lua_pushnumber (L0.5); if (0! = lua_pcall (L, 1,1,0)) {printf ("cpp call lua function failed\ n");} printf ("cos (0.5) =% f\ n", lua_tonumber (L,-1)); lua_pop (L, 1) } / / registry function void regist_lib (lua_State * L) {static const luaL_reg mathlib [] = {{"abs", math_abs}, {"cos", math_cos}, {"sin", math_sin}, {NULL, NULL}}; luaL_register (L, "DY_MATH", mathlib); / / Test double sinv = 303.1415926hand 180.0 Lua_getglobal (L, "SIN"); lua_pushnumber (L, sinv); if (0! = lua_pcall (L, 1,1,0)) {printf ("cpp call lua function failed\ n");} printf ("sin (% f) =% f\ n", sinv, lua_tonumber (L,-1)); lua_pop (L, 1);} int main () {lua_State * L = luaL_newstate () LuaL_openlibs (L); char * luapath= "LuaCallCTest.lua"; luaL_dofile (L, luapath); regist_function (L); regist_lib (L); lua_close (L); system ("pause"); return 0;}

LuaCallCTest.lua file

-- region LuaCallCTest.luafunction COS (a) print ("called COS in lua script")-- lua calls return cos (a) endfunction SIN (a) print ("called SIN in lua script")-- lua calls return DY_MATH.sin (a) endfunction SHOWMESSAGE () showmessage () end--end region

Result

This is the answer to the question about how to call the Lua function / library. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report