2
\$\begingroup\$

I appreciate the answer to this question might be a little too broad for SE, but hopefully someone can point me in the right direction.

I'm developing a small scripting language for use in my game engine, and naturally in order to do really useful things with it, it needs to hook into actual C++ functions in my game.

For example, say I have an Inventory class, with a public function, checkItem(itemID) that is designed to check whether a certain item is in the current inventory or not.

I'd love to be able to define a scripting function that calls the built-in checkItem function for its value during some script's lifetime. Is there a standard / simple-to-implement procedure for doing this? Something that has a similar effect to LuaBind, for example?

I can of course add instructions to my scripting VM that call the functions internally, but this will make the VM a bit of a mess and require implementing separate operations for each function I wish to call.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'm always curious to know why on earth do people create their own scripting language while there are a lot of mature and well integrated languages out there? (Python, Lua, ...) \$\endgroup\$
    – Vaillancourt
    Nov 18, 2015 at 13:48
  • 5
    \$\begingroup\$ I'm always curious to know why on earth people such as yourself seem bothered by such things. How about because sometimes reinventing the wheel is a great way to learn something new? Or because my scripting language is built for my game, or maybe I just want to (fancy that, eh?). I hope that satisfies your curiosity. Do you also question people who look for a non-Boost way of doing things in C++? \$\endgroup\$
    – Triforcer
    Nov 18, 2015 at 14:03
  • 1
    \$\begingroup\$ Please forgive me, I did not intend to be rude :/ Your comment satisfies my curiosity! \$\endgroup\$
    – Vaillancourt
    Nov 18, 2015 at 14:06
  • \$\begingroup\$ If I was you, I'd use Lua. \$\endgroup\$
    – Lucien
    Nov 18, 2015 at 15:49

2 Answers 2

3
\$\begingroup\$

There is no one way to bind an engine with a scripting language, nor is there really an easy way or a best way, it's all very dependent on your architecture, scope and ambitions, I can think of two basic strategies to accomplish what you want.

The first option you already suggested: Calling functions from the VM, this is is the simplest and easiest solution if you can keep it small! It can quickly spiral into unsustainable spaghetti as you haphazardly add more and more functionality, but with proper forethought and planning it might be all you need

Secondly you could try to implement a message passing system within your engine where the VM would just generate a message from input and send it out to the proper object. This is a more robust solution as it decouples your VM from the engine and far easier to grow and shrink. But it would add a small performance and memory hit, and it could be a lot of work to implement, maybe even to much.

The hidden third option is to use a mix of the two, binding for perfomance critical sections and messaging for less demanding part, of course this would be an even more complex thing to design and implement well.

Whatever path you may choose prepare yourself, creating a language is like raising a child, it will be more difficult and frustrating and time-stealing than you can even imagine and you will bang your head into the nearest hard surface many, many times! But if you persevere and keep at it and show lots of love, it will be a very rewarding experience that you will always cherish and no matter what the world thinks, in your eyes she will always be unique and special.

\$\endgroup\$
0
\$\begingroup\$

It might be worth using an existing scripting language unless you really really REALLY want to do this yourself.

You will need to write an interpreter for your scripting language and a library to expose your game engine API to the interpreter. Writing the interpreter and library is a fair amount of work in and of themselves but there is more, you also need to create a language. At this point things get pretty tricky and theory comes into play. Take a look at the specification for Python, or C specification for some idea of the things you will need to consider.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you, but I'm not afraid of the work. I've already written a parser and compiler for a high-level object-oriented language and would love the exercise. Maybe I'm a masochist :) \$\endgroup\$
    – Triforcer
    Nov 18, 2015 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .