Embedding a Ruby interpreter in a C++ app

前端 未结 5 1079
半阙折子戏
半阙折子戏 2020-12-08 08:56

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. her

相关标签:
5条回答
  • 2020-12-08 09:02

    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.

    0 讨论(0)
  • 2020-12-08 09:02

    I think what your going to want to do is use Ruby's threads. I've done a fair bit of digging through the Ruby threading code and I know that (where pthreads is available) sleep is implemented in a non-blocking fashion using pthread_cond_timedwait. This unblocks the interpreter so that other threads can continue execution.

    Your own code is going to have to respect Ruby's threads when it comes to interacting with the Ruby interpreter. They've still got a Global VM Lock in Ruby which means you should be careful about modifying anything in the interpreter without having the lock yourself.

    0 讨论(0)
  • 2020-12-08 09:08

    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.

    0 讨论(0)
  • 2020-12-08 09:13

    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.

    0 讨论(0)
  • 2020-12-08 09:19

    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;
    }
    
    0 讨论(0)
提交回复
热议问题