• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Calling lua function in C++

zxmatzx

Advanced OT User
Joined
Dec 1, 2010
Messages
311
Solutions
27
Reaction score
154
Location
Brazil
GitHub
Mateuso8
Hello,
I'm working on a mount system, and I need to access the information that is in Mounts.xml several times. Instead of accessing the XML file, I made a global table in LUA and load all the information from the XML file there through a function when the server opens(onStartup). I need when I use /reload mounts, the table is re-created, to update the information if they have changed.
I think this way is faster, i realy don't know...

How do I call a function in LUA, in libs folder made by me, by the reload command of the mounts?

Thanks.
 
Solution
Then i will have to implement this calling by source... I will continue looking a way to do that...
Thanks for helping.
Follow this repo and move your reload command to Lua, then you can call your function in the talkaction.
Which distribution are you using? Because if the reload command is in c++ this will be tricky. Can you see if the /reload command is in talkactions.xml?
 
Which distribution are you using? Because if the reload command is in c++ this will be tricky. Can you see if the /reload command is in talkactions.xml?
TFS 1.2, the command is in C++. In mounts.cpp have the reload command, i dont know how to call my lua function in lib folder.
Thanks.
 
You could create a command just to update your table.

If you can not query your table, it is because you are not importing it into your files. To do this you should add it next to these tables:

Yeah, i already have it in LUA. I want to call in C++, when i use reload command.

data/lib/myFunctions.lua:
Lua:
myTable = {}

function updateMyTable()
    myTable = Game.loadXmlInfos()
end

data/globalevents/scripts/startup.lua
Lua:
function onStartup()
--do my startup stuffs
updateMyTable()
end

Game.loadXmlInfos is a C++ function, that read some XML files(mounts.xml, vocations.xml) grab the data and put on a table. Then, if i change the xml files, when server is running and reload, i can't access the new infos, because this function is only called one time, in onStartup. I need to call inside my C++ reload command too.
Thanks.

Bump
I already found my reload command on sources, just need know how to call my custom LUA function.
 
Bump
Please some help... Just a tip, source file, custom system in community that call a lua function from sources, i can study the code and learn by myself. Realy needing this to finish my code...
 
Thanks for answer.
I readed all this, and looked in my source files, seaching for some references and found it! I will study more and try finish my code. I found this link with usefull info about subject too. Realy thanks!


--EDIT:
After read all i got the problems:

From @Stigma link, i don't know how to get Lua State when my reload command on Mounts.cpp is called. I looked other files that do that, and all these have scriptInterface and i don't figured out how to declare and use this on Mounts...

From the link that i found, he create a new LuaState like:
C++:
lua_State *L = luaL_newstate();
and if i create a new LuaState i can't access my function because it is new and don't have nothing(functions, variables).

I found this Create new Callbacks here, learned something, but in the exemple, the files have scriptInterface too and don't teach how to use...

Looking luascript.cpp i saw some functions like: callFunction, pushFunction. I don't know if i could use that, don't found any information about usage...
Thanks for all answers but im still needing help...
 
Last edited:
Then i will have to implement this calling by source... I will continue looking a way to do that...
Thanks for helping.
Follow this repo and move your reload command to Lua, then you can call your function in the talkaction.
 
Solution
Follow this repo and move your reload command to Lua, then you can call your function in the talkaction.
Realy thanks, that helped me a lot! U are the best!

--Edit
After changing my source files, compile and add this code to talkactions/scripts/reload.lua
Code:
    if reloadType.name == "mounts" or reloadType.name == "global.lua" then
        loadMountsInfos()
    end
I was able to do what I wanted. Thanks a lot for the help.
 
Last edited:
Back
Top