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.
eRuby stands for embedded Ruby. It's a tool that embeds fragments of Ruby code in other files such as HTML files similar to ASP, JSP and PHP. eRuby allows Ruby code to be embedded within (delimited by) a pair of <% and %> delimiters.
So far we've looked at using Ruby to create HTML output, but we can turn the problem inside out; we can actually embed Ruby in an HTML document. There are a number of packages that allow you to embed Ruby statements in some other sort of a document, especially in an HTML page.
The original Ruby interpreter is often referred to as Matz's Ruby Interpreter or MRI. This implementation is written in C and uses its own Ruby-specific virtual machine. The standardized and retired Ruby 1.8 implementation was written in C, as a single-pass interpreted language.
During the parsing stage, Ruby transforms the text into something called an abstract syntax tree, or AST. The abstract syntax tree is a representation of your program in memory. You might say that programming languages in general are just more user-friendly ways of describing abstract syntax trees.
Here's an example including error handling.
#include <iostream>
#include <ruby.h>
using namespace std;
int main(void)
{
ruby_init();
ruby_init_loadpath();
int status;
rb_load_protect(rb_str_new2("./test.rb"), 0, &status);
if (status) {
VALUE rbError = rb_funcall(rb_gv_get("$!"), rb_intern("message"), 0);
cerr << StringValuePtr(rbError) << endl;
};
ruby_finalize();
return status;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With