RPG Maker Forums

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.

Latest Threads

Latest Profile Posts

Frostorm wrote on Featherbrain's profile.
Hey, so what species are your raptors? Any of these?
... so here's my main characters running around inside "Headspace", a place people use as a safe place away from anxious/panic related thinking.
Stream will be live shortly! I will be doing some music tonight! Feel free to drop by!
Made transition effects for going inside or outside using zoom, pixi filter, and a shutter effect
I have gathered enough feedback from a few selected people. But it is still available if you want to sign up https://forums.rpgmakerweb.com/index.php?threads/looking-for-testers-a-closed-tech-demo.130774/

Forum statistics

Threads
105,992
Messages
1,018,189
Members
137,771
Latest member
evoque
Top