Ruby in RMMV Explanation

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
I just wanted to share a neat thing I learned this late/early morning. You can use the node js in the Chromium RPG Maker MV game wrapper to put Ruby rb scripts in your games/apps.

Here is a quick working example that puts a game message. This one is over simplified, but the possibilities are limitless.

The first script block is javascript, but I promise this is more about the Ruby. (more explanation in comments)
PHP:
// the MV javascript
var runMyRubyScript = function(){
   // I can't get the global "_dirname" to work on my mac
   var pn = document.location.pathname;
   var _dir = pn.match(/^\/\w:\//) !== null ?
       unescape(pn.replace(/^\/|index\.html$/g, '')) // windows path
     :
       unescape(pn.replace(/index\.html$/, '')); // mac path
   // I also need to combine my dir with node path on Mac
   var path = require('path');
   var myRubyScript = path.join(_dir, "js/plugins/myRubyScript.rb");
   var proc = require('child_process');
   var env = Object.create(process.env);
       env.myRubyVariable = "Hello! I'm from a Ruby script!";
   // this is an ENV variable that can be read in your rb
   var inspect = proc.spawn("ruby", [myRubyScript], {env:env});
   inspect.stderr.on('data', function(data) {
       console.log(data.toString());
       // this reads from the ruby errors if any
   });
   inspect.stdout.sync = true;
   // this ^ keeps it from returning the buffer array
   inspect.stdout.on('data', function(data) {
       $gameMessage.add(data.toString());
   });
};
PHP:
# myRubyScript.rb

STDOUT.sync = true
$stdout.sync = true
# this ^ keeps the stdout from being a bunch of numbers,
# I'm not sure which one to use, so I use both.
# Perhaps one is wrong, but it seems to work either way.

myMessage = ENV["myRubyVariable"]
# this ^ is the environment variable set from the node js
# global process.env

puts myMessage
Neat!


Here are links to the references I found on Google that I learned all this from.
Side note: It's my understanding that node only runs in the offline wrapper (I think...), so it won't work in browsers or phones, but there are other ways to process Ruby for online games in the same concept, like PHP proc_open for example.

EDIT: I can't get this to work in Windows.
EDIT EDIT: I had to install Ruby and check the box to run RB scripts, to get this to work in Windows.
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

I think I've got my hero designs pretty much finialised ( still got some tweaks. ) In the end it'll be the four outfit colour variants with four different skintones.

I'm a bit conflicted. One the one hand, it's quite visually cool to make bodies of water that the player can interact with by going into it. However obscuring the player character from a gameplay perspective is pretty bad. Hmm... There's the option to make the area around the player visible, but then that kind of defeats the purpose of submerging them. :unsure:
Stream will be live shortly with some Darkest Dungeon! Feel free to drop by!
Another two plugins done! One being an on map grid inventory.
busy with my newborn child, can't even touch pc at all ;_;

Forum statistics

Threads
104,295
Messages
1,005,349
Members
135,811
Latest member
napax
Top