Calling lua function in C++

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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.
 

zetibia

Intermediate OT User
Joined
Jun 23, 2013
Messages
105
Best answers
0
Reaction score
15
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?
 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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.
 

zetibia

Intermediate OT User
Joined
Jun 23, 2013
Messages
105
Best answers
0
Reaction score
15
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:

 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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.
 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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...
 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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:
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
Bump
If i create a talkaction /reload too, will cause conflict?
 

Delusion

Divine Intellect
Support Team
Joined
Feb 14, 2015
Messages
5,050
Best answers
473
Reaction score
2,682
It will overwrite the current reload talkaction written in the source.
 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
It will overwrite the current reload talkaction written in the source.
Then i will have to implement this calling by source... I will continue looking a way to do that...
Thanks for helping.
 

Delusion

Divine Intellect
Support Team
Joined
Feb 14, 2015
Messages
5,050
Best answers
473
Reaction score
2,682
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.
 
OP
Z

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
155
Best answers
5
Reaction score
39
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:
Top