Page 1 of 1

Dev Chat: 10/21/2014 - LUA Functions explained

Posted: Tue Oct 21, 2014 5:22 pm
by John Adams
Jabantiz from EQ2Emulator was kind enough to talk with Volt today about EQ2's LUA function implementation, since we are planning to follow that design for ease of development across both projects.

[quote][12:47] <@Volt> Do you have time for a question?
[12:47] <@Jabantiz> yea, got about an hour before i need to go afk
[12:48] <@Volt> Great. I will probably log out before that, it is close to 10PM here.
[12:49] <@Volt> I think I am confusing myself with passing variables in LUA ..
[12:49] <@Volt> Will try an example
[12:50] <@Volt> if C++ calls this in LUA:
[12:50] <@Volt> function foo(bar)
[12:50] <@Volt> and LUA puts a value in bar, will C++ be able to use that value once control is returned to C++?
[12:50] <@Volt> (if that made sense)
[12:51] <@Jabantiz> yes
[12:51] <@Jabantiz> assuming bar is a light user data
[12:51] <@Volt> light user data? as bool, integer, and such?
[12:52] <@Jabantiz> as Spawn, NPC, Dialog, data that the devs (user) add support for
[12:53] <@Jabantiz> so any custom data is probably light user data
[12:53] <@Volt> Do we need to explicitly add support somewhere in the LUA .h/.cpp files?
[12:53] <@Volt> ah
[12:54] <@Jabantiz> yea
[12:54] <@Volt> Doh I tricked myself. I was so sure this was not possible that it didn't even occur to me to try it out.
[12:54] <@Jabantiz> updating vgo svn so i can look at your guys code to better explain it
[12:54] <@Volt> Thanks for being patient!
[12:54] <@Jabantiz> np
[12:56] <@Jabantiz> luainterface.h
[12:56] <@Jabantiz> class LuaUserData {
[12:57] <@Jabantiz> class LuaUserData
[12:57] <@Jabantiz> is the base class for light user data
[12:57] <@Volt> I see it
[12:57] <@Volt> ok, and there are child classes
[12:57] <@Jabantiz> below it you will see the classes for spawns and chuncks
[12:58] <@Jabantiz> and finally below that is
[12:58] <@Jabantiz> namespace VGEmuLua{
[12:58] <@Jabantiz> you would need to add a Set and Get for the new types
[12:59] <@Jabantiz> for example
[12:59] <@Jabantiz> void SetLuaChunk(lua_State* state, shared_ptr<ChunkServer> chunk);
[12:59] <@Jabantiz> ok not sure you got that as irc spit an error back at me
[12:59] <@Jabantiz> GetLuaChunk(lua_State* state, int index);
[12:59] <@Jabantiz> SetLuaChunk(lua_State* state, shared_ptr<ChunkServer> chunk);
[12:59] <@Volt> nope didn't get the last one
[13:02] <@Jabantiz> so to add support for dialog data type in lua, just assuming the c++ class is Dialog, the base class, LuaUserData, you would need to add virtual bool IsDialog() { return false; }
[13:03] <@Jabantiz> then you need to make the child class for it, so you can either manually type it out or copy and paste one of the other childs and change it to dialog
[13:05] <@Volt> gotcha
[13:05] <@Jabantiz> and then the same for the Set and Get in the name space at the bottom
[13:06] <@Jabantiz> and that would be it
[13:06] <@Jabantiz> then you can use lua_interface->GetDialog(state) in your lua functions
[13:07] <@Volt> in my LuaFunctions, right?
[13:07] <@Jabantiz> is there a Dialog class on current svn? if so I could probably code it up really quick so you can see an example
[13:08] <@Jabantiz> luaInterface
[13:08] <@Volt> No it is not there yet. I still keep on experimenting with different approaches.
[13:08] <@Jabantiz> oh, unless you meant my last example of lua_interface->GetDialog() then yea it would be used in LuaFunctions
[13:09] <@Volt> yes I did. thanks just wanted to be really sure that is what you meant.
[13:09] <@Jabantiz> do you have eq2emu code by chance?
[13:10] <@Volt> nope
[13:10] <@Volt> sounds it is about time I get that
[13:10] <@Jabantiz> implementations are almost identical
[13:10] <@Volt> do EQ2 have entity types?
[13:10] <@Jabantiz> and we have added a bunch more types over there
[13:10] <@Jabantiz> Spawn is all we use in lua
[13:11] <@Jabantiz> the very base entity type
[13:11] <@Volt> In Vanguard, each spawn has an attribute called entity type
[13:11] <@Volt> it is a 32 bit bitmask, where a "1" in any position means something special
[13:12] <@Jabantiz> yea eq2 is just spawns
[13:12] <@Jabantiz> we have special packets for signs and widgets but they are just spawns as well
[13:12] <@Jabantiz> ... nterface.h
[13:12] <@Jabantiz> forgot john had a web svn thing
[13:13] <@Jabantiz> user is anonymous
[13:13] <@Jabantiz> leave password blank
[13:14] <@Volt> got it
[13:14] <@Volt> ok, I see you have effect-flags
[13:18] <@Volt> and I see for example Quest (or LUAQuestWrapper) being a LUAUserData
[13:19] <@Jabantiz> yea
[13:21] <@Volt> about entity types, I was about to tell you that VG use these also to define different roles to NPC's, so an NPC can have bits set defining it to be a Merchant or a Banker or a Trainer. Or all three at once.
[13:23] <@Jabantiz> yea we have something like that
[13:23] <@Volt> oh
[13:23] <@Jabantiz> not sure where or if it is sent to the client (was implemented and working long before i came around so never really had to deal with it)
[13:24] <@Jabantiz> still a lot of dark corners in the eq2emu source for me
[13:24] <@Volt> I see. In VG it is sent to the client in the bitstreams, and the client makes some decision on its own based on those entity types.
[13:24] <@Volt> Meaning the server code will need to play along the same lines.
[13:25] <@Volt> Just thought it would be good if you knew a bit about it.
[13:26] <@Volt> correction: Just thought it would be good if you were aware we have this in VG.
[13:26] <@Jabantiz> yea, what special things does the client do?
[13:27] <@Jabantiz> just adds the dialog for banking for example?
[13:28] <@Volt> For example: The client changes the mouse icon to a merchants icon when hovering over a merchant. While the server will need to add a specific packet to have the option for the merchants option to be included in the dialog.
[13:29] <@Jabantiz> ok that should be easy
[13:29] <@Volt> Yeah it is straight forward once you know about it.
[13:30] <@Jabantiz> does it take a dialog spot?
[13:30] <@Volt> Not one of those five, no
[13:30] <@Jabantiz> yea, super easy then
[13:30] <@Volt> So you can have five options, and one each of the other nine. And a bunch of quests. And ...
[13:31] <@Jabantiz> if you do it like we do (eq2) and have local dialog = CreateDialog()
[13:31] <@Jabantiz> the CreateDialog function can set all that up
[13:34] <@Volt> In my latest prototype (on my machine), I use one LUA function to add custom components to the initial dialog. And then another LUA function to handle any selected option. The plan being to add LUA functions for Merchant options, maybe Trainer options, for sure Quest options.
[13:35] <@Volt> But then I looked at the EQ2 code sample, and I see you set it up in the first function.
[13:35] <@Volt> So I am pretty much at the point where I ponder which way to go.
[13:36] <@Volt> viewtopic.php?f=4&t=930#p7536
[13:36] <@Jabantiz> yea at this point it is pretty much personal preference
[13:37] <@Volt> If it boils down to that, I should go EQ2 way since that will make it easier for those who are involved in both projects.
[13:39] <@Jabantiz> which do you think would be easier for noobs to scripting
[13:39] <@Jabantiz> that would probably be the best choice
[13:40] <@Volt> Well said
[13:40] <@Jabantiz> as most content devs will come in with no knowldege of scripting
[13:41] <@Volt> In my prototype I don't need to store any data server side which I kind of like. (But I will need to pass it back and forth during the call)
[13:42] <@Volt> The content devs, the will work directly in the LUA file, right?
[13:42] <@Volt> *they
[13:43] <@Jabantiz> yea
[13:43] <@Jabantiz> in a web editor
[13:43] <@Volt> gotcha. I think I remember you making a syntax highlighting editor?
[13:43] <@Jabantiz> yea
[13:44] <@Volt> very nice
[13:44] <@Jabantiz> i use that to copy and paste into the web editor for eq2
[13:45] <@Volt> ops time flies
[13:45] <@Volt> one semi related question if you have time, and if you know:
[13:46] <@Jabantiz> sure
[13:46] <@Volt> Scenario: A PC walks up to an NPC that has a quest available, and an icon is "lit" above the NPC's head.
[13:46] <@Volt> Any idea where in the code the check and decision to light that icon is made?
[13:47] <@Jabantiz> in eq2 we have a lua function we put in the spawn script init function, ProvidesQuest(Spawn, Quest ID)
[13:47] <@Volt> oh gotcha
[13:48] <@Jabantiz> we attach that id to the npc and check the quest in the packet code to see if we send the value for the feather
[13:48] <@Jabantiz> should mention it is a vector of id's we check
[13:49] <@Volt> hm, the icon should be lit only if the character fulfills some pre-reqs
[13:50] <@Jabantiz> yea we check that too
[13:51] <@Volt> do you know where the pre-reqs are defined?
[13:51] <@Jabantiz> we have 2 choices in eq2emu
[13:51] <@Jabantiz> db and lua
[13:52] <@Jabantiz> lua it would be in the quest lua script in the init function
[13:52] <@Jabantiz> db we have a quest_details table that handles prereq's and rewards
[13:53] <@Volt> so maybe more standard types of prereq's in the db, and then you can put more special prereq's in LUA?
[13:53] <@Jabantiz> both handle all
[13:53] <@Volt> kk
[13:53] <@Jabantiz> was early dev when it was debate between lua or db is better
[13:53] <@Jabantiz> so we ended up with both
[13:54] <@Volt> lol I see
[13:54] <@Volt> what would you have chosen if you could go back in time?
[13:54] <@Jabantiz> don't know to be honest
[13:54] <@Volt> kk
[13:55] <@Jabantiz> both function about the same
[13:55] <@Volt> I will need to head for bed. And I am sure you are late for something as well. Thank you so much, this was a brilliant chat.
[13:55] <@Jabantiz> same amount of effort to add support for new types or rewards or prereq's
[13:55] <@Jabantiz> np, will talk to you later, good night
Thanks, Jab!