Author Topic: Adding Scripting language to your game(C++)  (Read 10548 times)

Malice

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
    • Email
Adding Scripting language to your game(C++)
« on: April 06, 2015, 10:14:57 PM »
Hello!
First of all, I'd like to say hi - It's my first post in here. Hi! :)

Now let's cut to the chase - I'm writing some simple rougelike in C++. I'm using SDL for graphics. It's not even started properly, but I'd like to make some decisions about scripting.
I'd like to add some things like AI or quests as external source that will be read on fly - either at the beginning of game, at startup, or later. I don't know yet. In some prototype I've wrote before, I used xml for items and NPC lines. It worked nicely, now I'd probably use json, but it turned out ok. It could stay that way.
However, I have no idea how to implement scripting for, let's say, quests. I want to give quest - "If you kill 10 rats, quest is finished.". How to do it? I can't call function from C++ that way, I can't just paste the code, because by the time it's already compiled(in Lisp it wouldn't be that hard, eh?* :)).

I don't want to write wrapper for everything, it would be too much work. I want to have some ability to nicely work with code from outside. Is there some easy way to do it? I'd be grateful for sharing your experience or pieces of code(preferably C++, but can be some different language if there isn't much language-related magic going on). I've tried searching on my own, but people mostly write some general statements("Use scripting for X") or write too simple examples("hello world" in Lua isn't helping). I've completed some Lua tutorial, so I kind-of have some idea about how to use Lua with C++, but I have no idea about how to use Lua with C++ with OOP, and get all this to work together nicely without making my code look like some tasty, tangled spaghetti.

Thanks in advance!

*Yes. I like Lisp. Great language!

tuturto

  • Rogueliker
  • ***
  • Posts: 259
  • Karma: +0/-0
    • View Profile
    • pyherc
Re: Adding Scripting language to your game(C++)
« Reply #1 on: April 07, 2015, 08:14:08 AM »
There's interesting article at http://carloscarrasco.com/embedding-scheme-for-a-game-mission-scripting-dsl.html that touches the subject briefly. However, it doesn't go that much in detail how to bridge C++ and Scheme are bridged and how to generated foreign function interface easily. CFFI (https://common-lisp.net/project/cffi/) doesn't look too bad, you would need to define each and every C++ function you need to call though. Are there really that many, that it would be too unwieldy?

Yes, lisp is really great language.
Everyone you will ever meet knows something you don't.
 - Bill Nye

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Adding Scripting language to your game(C++)
« Reply #2 on: April 07, 2015, 11:11:16 AM »
Yes, lisp is really great language.

Then why do you even need C++ or other languages? Just script everything with Lisp and it'll be fine.

Malice

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
    • Email
Re: Adding Scripting language to your game(C++)
« Reply #3 on: April 07, 2015, 03:54:43 PM »
Then why do you even need C++ or other languages? Just script everything with Lisp and it'll be fine.
I'm doing this as University project. Normally I'd try with Common Lisp, but it looks like for most folks in there, Lisp is something exotic.

My main concern is about how to wrap all these together. I'm using kind of state machine for my game. There's main game engine(something like QT's "app", I guess). Each state implements HandleEvents, Update and Draw.If I wanted to call any code from Common Lisp, I'd have to write wrapper for whole game in Lisp. I just don't have any idea about how to get these two to work together.

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: Adding Scripting language to your game(C++)
« Reply #4 on: April 07, 2015, 03:58:47 PM »
If you are working with QT, than you have javascript and bindings.

mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: Adding Scripting language to your game(C++)
« Reply #5 on: April 07, 2015, 06:32:50 PM »
Basically, if you want to link it as a native executable, you include a library corresponding to whatever scripting language you want, then you register various functions and data structures to correspond to functions, objects, and types in that language using the API exposed by the library. There are usually tools for automating this registering process if you're not trying to do something fancy.

The other way to go would be to implement your game engine/logic as a library and write an extension for whatever scripting language. Again, this involves registering functions and data structures and tools for automating the process exist. Then you write scripts to start the game and script content in that language.

Some languages have particularly straightforward ways to do the automation stage, for example Cython does this kind of thing pretty cleanly if you want to script with python.

Malice

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
    • Email
Re: Adding Scripting language to your game(C++)
« Reply #6 on: April 07, 2015, 11:14:17 PM »
Some languages have particularly straightforward ways to do the automation stage, for example Cython does this kind of thing pretty cleanly if you want to script with python.
I'll look into Cython. Python is nice language. I've just never coupled two languages to create code that will work(other than simple hello, world), and given how my program works, it looked like I'd have to run everything from scripting lanugage(because the only thing that gets called in main is Game.Run()).

@Xecutor - I'm not, I gave QT as an example of what I'm doing. All the libs so far are SDL.