views:

923

answers:

4

I'm hoping to use Ruby as a scripting language for my game engine. I've found the usual articles describing how to call Ruby classes from C++ code and vice versa (e.g. here) but I can't quite see how to do what I want with that way of working...

My engine currently uses a little language I wrote myself with Flex and Bison, and a little stack based virtual machine. Scripts don't always run right through from start to finish, for instance they sometimes includes commands like "sleep for 2 seconds" or "wait until character has finished walking", so the scheduler keeps tabs on the status of each script and an instruction pointer, and knows when to resume them, and so on.

So it seems that I really need some kind of embedded Ruby interpreter that I can exercise a certain degree of control over, rather than simply calling Ruby methods. Or am I just being obtuse and missing something?

I'm working in Microsoft Visual C++, so ideally any solution would compile nice and easily in that.

+3  A: 

You're on the right track. The key is to do something similar to the section on Embedding Concepts in the link you posted. In simple terms it's little more than:

ruby_init();
ruby_script("some_script");

You may need to copy over all the #ifdef stuff from main.c to get everything working. From then it's a matter of building an API to your C++ functions you want to expose, and depending on your design, multi-threading the thing.

Pesto
This still doesn't seem to address the question of how you get one of those scripts to go into sleep mode, for example - the scheduling side of things.
andygeers
+2  A: 

You could always re-design the way the scripts work to suit the script engine rather than trying to get the engine to work with the original scripting paradigms.

So, if you had this:

proc:
  action 1
  action 2
  sleep a bit
  action 3
end

which would require the script to be suspended on the sleep line, do this:

proc
  action1
  action2
  set timer (time, callback_proc)
end

callback_proc
  action3
end

which lets the scripting engine finish each method neatly. It wouldn't need many changes to the hosting side - each version requires some form of event system which can restart the script engine.

Skizz

Skizz
+2  A: 

There is a guide about how to embed ruby into a C++ application. That may be helpful. Otherwise go to the Ruby documentation. The Embed Ruby in C article may be helpful, too.

lothar
A: 

Would lua not be a better option its really lightweight and is generally the norm for game scripting, take a look at the free lua book, I'm currently developing a 2D game using c++/sdl and its the best option for game scripting that I have found.

Jamie