Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading Game Data with Lua  (Read 13042 times)

0 Members and 1 Guest are viewing this topic.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Loading Game Data with Lua
« on: June 09, 2014, 10:22:00 am »
Hello,

I have the SFML Game Development Book and it says somewhere:
"Nowadays, it is very common to load gameplay information from external resources [...]
One possibility that has recently gained popularity consists of using script languages, most notably Lua [...]"

I am very interested in trying this, I actually already was before reading this, for loading some data like tables or something and also using it to code some game logic.

However, I have no clue what I need to get started, I guess I need a Lua interpreter, which I have now, but which libaries do I need? Is "Lua binding for C++" the correct term? If I know what I need, I can search for tutorials, documentation and stuff online and do it myself, but I am kind of missing a starting point.

Hope someone can help me

Kind Regards,
Raincode

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading Game Data with Lua
« Reply #1 on: June 09, 2014, 10:55:46 am »
SFML doesn't provide tutorials for Lua, but there might be some files on the Wiki.

Otherwise, you can use www.lua.org as a starting point. I have made good experience with the LuaBridge binding.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loading Game Data with Lua
« Reply #2 on: June 09, 2014, 11:04:46 am »
 Embedding Lua is simple (as it ought to be since that is what it was designed for).
It's basically just a matter of including lua.h, calling lua_open() and a few other details.
Google for "embedding Lua in C++" and you'll find lots of material to show you how to.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Game Data with Lua
« Reply #3 on: June 09, 2014, 11:22:03 am »
Hi,

thank you very much for your replies.

I think I'll be able to manage it. I am aware, that this isn't exactly the right forum, but since I read it in the book, I thought someone must know something.

LuaBridge seems promising, I'll try starting with that.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading Game Data with Lua
« Reply #4 on: June 09, 2014, 06:37:09 pm »
I am aware, that this isn't exactly the right forum, but since I read it in the book, I thought someone must know something.
I wrote the quoted passage in the book, and what I had in mind is more or less what I wrote in the last post ;)

Note that there are many possibilities to use Lua in C++. Either you use directly the C library, or one of the various C++ bindings such as LuaBridge. There's a list of C++ bindings with really many entries. That wiki is quite good in general for various Lua-related things.

I've initially used LuaBind, it's nice but really heavy, with the Boost dependency and huge compile times. So I've looked for a few alternatives and so far, LuaBridge was the binding with the best trade-off between flexibility and complexity.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Loading Game Data with Lua
« Reply #5 on: June 09, 2014, 06:41:35 pm »
The only thing that would concern me with LuaBridge is that there hasn't been anything significant changed since Oct 2013, and there are quite a few issues and pull requests with little to no activity. The maintainer appears to be fairly active elsewhere.
Follow me on Twitter, why don'tcha? @select_this

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Loading Game Data with Lua
« Reply #6 on: June 09, 2014, 09:36:07 pm »
A new, very nice binding library is Selene. I've found it very easy to use. It's pretty easy to navigate the source and add your own improvements as well.
« Last Edit: June 10, 2014, 01:02:55 pm by dabbertorres »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading Game Data with Lua
« Reply #7 on: June 10, 2014, 09:14:52 am »
A new recent, very nice binding library is Selene.
This looks really promising. C++11 would allow to simplify a lot of things.

I definitely have to look closer at Selene, and possibly also a few other C++11 Lua bindings. It seems like that list changed a bit since I last read it. Thanks for the hint!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Game Data with Lua
« Reply #8 on: June 10, 2014, 11:16:53 am »
Yes definately thank you, I will check this Selene library then, because I just came here to complain about various compilation errors, simply by including the LuaBridge library :D

edit:

huum, so does the selene library...
such as:
error: expected ';' at end of member declaration
     Selector&& operator[](const char *name) && {
                                           ^
error: expected unqualified-id before '{' token
     Selector&& operator[](const char *name) && {
                                                ^
« Last Edit: June 10, 2014, 11:21:06 am by Raincode »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading Game Data with Lua
« Reply #9 on: June 10, 2014, 11:28:51 am »
These are rvalue reference qualifiers. Apparently your compiler does not support all the needed C++11 features.

Out of interest, what compilation errors did you get at LuaBridge?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Game Data with Lua
« Reply #10 on: June 10, 2014, 05:57:15 pm »
If that is the case, which gcc version is required at least to support these features? I believe I have  GCC 4.8.1

The error with LuaBridge:
error: too many arguments to function 'const char* lua_pushstring(lua_State*, const char*)'
     lua_pushstring (L, str.c_str(), str.size());
                                               ^

some warnings:
warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
   inline void* const getPointer ()
                                  ^

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loading Game Data with Lua
« Reply #11 on: June 10, 2014, 06:18:58 pm »
Are you passing -std=c++11 to the compiler to enable C++11 support?

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Game Data with Lua
« Reply #12 on: June 10, 2014, 06:39:43 pm »
Definately, since not doing so gives another error. Additionally, the following program compiles and runs without problems:

int main()
{   
    std::vector<int> nums(1);
    nums[0] = 99;
    for (auto i : nums) std::cout << i;
}


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading Game Data with Lua
« Reply #13 on: June 10, 2014, 08:19:09 pm »
The LuaBridge error is fixed by this pull request. It's a bit unfortunate that Vinnie Falco is not active anymore, I also have one PR and one issue pending...

I'm not sure if and when the rvalue qualifiers have been implemented for g++, but you can easily research that. Maybe you need to simplify the code a bit (or contact the Selene author).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Loading Game Data with Lua
« Reply #14 on: June 10, 2014, 08:24:01 pm »
Where is that error coming from? pushstring takes string, pushlstring takes string and size. Why would there be error like that in LuaBridge? Even Lua 5.1 and 5.0 have these functions like that, I think even 4.0 has and that was 14 years ago...
Cleared up by Nexus link.

The warning is saying that the function returns a const void pointer (NOT pointer to const void) which makes no sense, because you can simply copy it into another variable and have non const pointer to non const void. The 'const' there literally does nothing. It's as if the * was misplaced: http://www.stroustrup.com/bs_faq2.html#constplacement

You'd be better off without using a binding for now, many of them do weird things like automatically create metatables and so on, this is really bad if you don't understand what the Lua logic behind that is. It's too much of a headache to try to learn all three - language, C API and binding API all at once.

Selene is good, clear, small and easy but still misses many things, it's better for loading data than real logic, I have to admit, data loading is extremely simple in it. It also is doing the 'magic' and taking away a lot of power you'd get learning C API. Using Selene's API you couldn't write a function to take advantage of Lua errors or overloading on amount/type of arguments passed without the C API.

Just use raw C API for a while, it's really not hard to use, and try to not mix the languages a lot, minimizing the amount of Lua <-> C++ action is a Good Thing according to both Lua and LuaJIT users and developers. Not only you'd not understand what the library binding Lua is giving you, you'd also probably not need it, it's better to take small steps and convert interesting bits of logic to Lua than suddenly attempting to use it in a C++ program like it's Unreal Script and coding every single bit of code in it. That is possible (Don't Starve uses idiotic amounts of Lua code for everything version 5.1 and no JIT I think which opens room for mods and maybe LuaJIT could combat the speed of Unreal Script, definitely not the tools, when used like that in an engine) but then you could just as well use existing Lua engine instead (like LOVE).

Just learn most of the raw C API (it's simple once you get into it more) and how C references, userdata, types and (meta)tables work (by work I mean how to use them in both languages, not how they're implemented in VM itself which is in C) and you're pretty good off.
Back to C++ gamedev with SFML in May 2023

 

anything