Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Integrating LUA
 Post Posted: April 5th, 2017, 4:51 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2827
Location: Oklahoma, United States
The current project I'm working on has really taken me to areas I've wanted to explore, but never had a reason to in the past. Currently, I'm trying to integrate LUA into the project and it's kind of difficult to wrap my head around it, but it has been fascinating really.

LUA itself is a scripting language. The advantage of using Lua or other scripting languages with your C/C++ programs is you can write your application control logic in C++ and for things like AI for instance as a script. Once you compile your main program, you can change the behavior of your AI without having to recompile. That's just the most popular example for using scripts, but you can use them for anything you'd like. Another example would be to use them as a configuration utility. Since the lua api does all the file parsing for you, you can setup a file with all your settings then just use the api to read them in.

Using the api however has been more than a little confusing to me. You interact with the Lua api through it's pseudo stack. Say you have a variable in your config file called 'width'. To get the value, you must tell the api to look up the variable, push it's value on the stack, get the value, then pop the value off the stack to keep the stack clean and from filling up.

In this way, it's as if your C++ application has become the CPU or assembly interpreter...can't make a good comparison lol.

I haven't made it to calling Lua functions yet, but I've looked over a few tutorials and the reference manual and it's basically the same thing;
  • find the function by name
  • push it on the stack
  • push any and all parameters onto the stack
  • call the function
  • pop the function off the stack
  • pop the params off the stack
  • get any return values ( lua can return multiple values )
  • pop return values off the stack

I'm definitely writing some helper classes for all this, but it's turning out to be a slow process at the moment having to learn and code.

Since I'm using C++, I'm also having to learn some foreign C++ stuff as well, like variadic templates which will help with the function calling, since functions can have multiple arguments or no arguments.

The trouble I have with variadic templates is the unpacking. I've come up with a solution since all the arguments to the functions are sent from C++ to the Lua api as strings ( char * really ) I pass a reference to an std::vector<std::string> and just keep adding ( push_back ) each recursive iteration of a helper function. So far this has worked, but I'm open to better solutions.

Has anyone else looked into or has imtegrated Lua or another scripting language into their C++ projects?
Have any hints or advice on the topic?

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 6th, 2017, 6:30 am 
 

Joined: October 24th, 2016, 12:23 pm
Posts: 49
I've worked on various scripting language integrations (lua, python and mono/c#)

I suggest reading lots of tutorials, for ease of integration I'd suggest using Python instead lots of good libraries to help (e.g. boost, pybind11, etc)

Feel free to hit me up if your running into problems.


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 6th, 2017, 8:03 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2827
Location: Oklahoma, United States
Thanks for the offer. I'm sure I'll need to call on you at some point.

I chose Lua because I'm familiar with the syntax already, and don't feel like learning yet another language right now. The current project I'm working on will have me learning networking, data management, probably multi-threading, scripting, and creating a user interface for a multi-user dungeon. There's more down the road, but for now, trying to learn these things just to get started is going to keep me busy enough, so I thought I'd stick to something I'm familiar with.

I'm sure later on, I'll want to have the server use PHP, SQL and maybe javascript, especially if we want it to be hosted somewhere other than our own server and tie payments to player accounts. I think I have a long road ahead of me being the sole programmer.

The person I'm working with is basically designing the game and will be providing all the assets and material for the scripts. I think he wants to learn some of the programming to help out, but doesn't have the time right now.

Anyway, just got to rambling, thanks again for the offer.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 6th, 2017, 1:01 pm 
 

Joined: October 24th, 2016, 12:23 pm
Posts: 49
Additionally with most/all of those lists of things I've also worked with so feel free to also hit me up if your learning those.

Always useful to have someone doing the assets and design, lets you focus on the programming side.


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 19th, 2017, 3:52 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2827
Location: Oklahoma, United States
Hey reductor, I have a question for ya.

Q) How much does the C/C++ side of the application need to know about the script?

I want to use the script as a source for random chat messages that get sent to everyone in a room based on the NPC "saying" said message. If I wanted to start off with a few messages for each NPC, is there a way to expand the list of NPCs and messages to choose from for each NPC without having to make the C++ side aware of the new additions?

I could make an NPC table as a template for creating a table for the list of NPCs. Any suggestions?

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 19th, 2017, 3:53 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2827
Location: Oklahoma, United States
I guess random isn't exactly correct, it would be more like walking into a room and overhearing a conversation between two or more people.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Integrating LUA
 Post Posted: April 23rd, 2017, 11:11 am 
 

Joined: October 24th, 2016, 12:23 pm
Posts: 49
albinopapa wrote:
Q) How much does the C/C++ side of the application need to know about the script?


As much as you want it to know about it, generally for scripting you want to abstract this way through some sort of interface. This isn't unique to just scripting but with any part of the code you should always think how much doe the reader need to know about this.

albinopapa wrote:
I want to use the script as a source for random chat messages that get sent to everyone in a room based on the NPC "saying" said message. If I wanted to start off with a few messages for each NPC, is there a way to expand the list of NPCs and messages to choose from for each NPC without having to make the C++ side aware of the new additions?

I could make an NPC table as a template for creating a table for the list of NPCs. Any suggestions?


This is entirely possible, as you already mentioned one approach would be to store the data within a table in Lua then read the table out with the data you want and use it as needed.


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 7 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: