Did you ever wanted to use Ruby and C# together in the .Net environment? With IronRuby it’s possible, and fortunately I thought of sharing this quickstart guide with you.  I assume you’re using .Net 4.0. I’m using SharpDevelop but the steps should be the same for Visual Studio.

Here are the quick steps to install IronRuby inside .Net

  1. Download the IronRuby msi package and install it.
  2. Open your favorite IDE. Create a new Console solution.
  3. Add the IronRuby reference to your project: (Project > Add References > IronRuby) When it’s done, you’ll see IronRuby in your reference folder.
  4. You’ll also need other references such: Microsoft.Scripting and Microsoft.CSharp. If you’re using a version lesser than .Net 4.0, perhaps you’ll need Microsoft.Scripting.Core as well.

We’re now ready to code our first Hello World program using IronRuby inside our ‘program.cs’ file (it was automatically created for us when we created our console solution). I’ll try to write as few lines as possible to keep the startup tutorial very simple:

using System;
using IronRuby;
namespace IronRuby_Test_01
{
class Program
{
public static void Main(string[] args)
{
var engine = Ruby.CreateEngine();
engine.Execute(“def hello_class; puts ‘Hello World’; end”);
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);
}
}
}

I assume you already have basic knowledge of C# and .Net (and Ruby!), so to make a long story very short, we’ve implicitly created a variable called engine that contain the ruby engine, then we used it to execute lines of code in which a ruby class was written on the fly and it’s executed. P.S: It’s important not to forget that C# is a static typed language and Ruby is dynamic and weakly typed language.

Another alternative:

For better clarity, our ruby code can be executed from a ruby file. For example, let’s create a ruby file named Hello_Class_File.rb and paste the following code inside it:

class Hello_Class
puts ‘Hello World’
end

And now, in our C# file:

using System;
using IronRuby;
namespace  IronRuby_Test_02
{
class Program
{
public static void Main(string[] args)
{
var engine = Ruby.CreateEngine();
engine.ExecuteFile(“Hello_Class_File.rb”);
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);
}
}
}

Another Example:

So, how would we assign a ruby object to a C# object? First let’s bring changes to our ruby file:

class Hello_Class
attr_reader :str
def initialize(name)
@str = “Hello world, from “ + name + ” !!”
end
end

Now let’s bring a few minor changes to our C# file:

using System;
using IronRuby;
using IronRuby.Runtime;
namespace IronRuby_Test_03
{
class Program
{
public static void Main(string[] args)
{
var engine = Ruby.CreateEngine();
engine.ExecuteFile(“Hello_Class_File.rb”);
dynamic ruby = engine.Runtime.Globals;
string my_name = “Elder”;
dynamic cs_hello = ruby.Hello_Class.@new(my_name);
Console.WriteLine(cs_hello.str);
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);
}
}
}

So that’s it, we can use C# and Ruby together. I’ll think of future tutorials. For example, one really bad thing right now is the complete lack of Intellisense with IronRuby.

*** Sorry for the bad code formatting, I’ll have to install a proper wordpress plugin when I find some more free time!



One major issue I noticed in RM communities is how coders overdo it when it’s time to add a new feature in the scripting section of the game engine. The explanation for this is surely because very few coders had spent enough time to make a proper dissection of the default code written by Enterbrain, which I must admit is a real mess and is terribly overwhelming to decipher. However when you get used to it you can make quick changes without ending with hundreds of extra lines of code.

I have seen several scripts that allow to skip the opening screen during play-time and all of them require way too much changes in the default code, therefore they’re a bit confusing to install and increase the risk of compatibility issues with other future scripts. So my #1 rule is to keep things as much simple as possible.  So here’s the simplest way to do it.

Go in the scripting section of the engine, select the  SceneManager module and find:

#————————————————————————–
# * Get First Scene Class
#————————————————————————–
def self.first_scene_class
$BTEST ? Scene_Battle : Scene_Title
end

Replace it with:

#————————————————————————–
# * Get First Scene Class
#————————————————————————–
def self.first_scene_class
DataManager.setup_new_game
$game_map.autoplay
$BTEST ? Scene_Battle : Scene_Map
end

 

See? Only three lines of code had to be changed to make this miracle happen!



Gamezopher Reboot

22 Aug 2012 In: Spotlight

Important notice. Gamezopher will soon be completely rebooted with new contents. This blog will no longer focus on my personal game developments or my personal life. Other blogs will gradually be customized to serve those purposes. Discussing game development in general, my ongoing game projects, my personal life and miscellaneous topics under the same blog wasn’t a good idea anyway.

Gamezopher was first imagined around 2005 and was meant to be a website on game development and game technologies in general. It’s about time I give the name its true meaning. It doesn’t mean it will become a large website, I greatly lack of time in my life, but at least I can share some parts of my knowledge and experiences with the crowd here. I have already written several articles on diverse subjects (such RPG Maker tutorials), I was just waiting a good time to publish them.

Please find my other blogs [here].



Gamezoph·ing

1-) To reason in the manner of a gamezopher. 2-) Love of videogames and wisdom.

Elder Prince, game creator of polymathic endeavors: game design, programming, mathematics, computer science, engineering, electronics, writing, music composition, marketing, and life experiences.


Twitter

StatCounter