Xna+IronRuby+RubyNewb=headache

Hi
I’m tiring to run a whole XNA game from IronRuby but I’m having some
trouble translating the code from c# to ruby. That might be because I’m
really new at Ruby.
here is what I wrote so far
Game = Microsoft::Xna::Framework::Game
GraphicsDeviceManager = Microsoft::Xna::Framework::GraphicsDeviceManager
Graphics=Microsoft::Xna::Framework::Graphics
class Game1 < Game

def Game1
graphics = GraphicsDeviceManager.new this
end

def Initialize

super
#is this ok?
#in C# i have base.Initialize()
end
#is this ok?
#in C# i have protected override void Draw(GameTime gameTime)
def Draw
Game::GraphicsDevice.Clear Graphics::Color.CornflowerBlue #i don’t think
this is is how to access the colour struct
super gameTime
#is this ok?
#in C# i have base.Draw(gameTime)
end
end

game = Game1.new
game.run

ps:allso posted on the Rubyforge page:
http://rubyforge.org/forum/forum.php?thread_id=29605&forum_id=17160

On 15/11/2008, at 6:00 AM, Gabriel R. wrote:

Hi
I’m tiring to run a whole XNA game from IronRuby but I’m having some
trouble translating the code from c# to ruby. That might be because
I’m
really new at Ruby.
here is what I wrote so far
Game = Microsoft::Xna::Framework::Game
GraphicsDeviceManager =
Microsoft::Xna::Framework::GraphicsDeviceManager
Graphics=Microsoft::Xna::Framework::Graphics

You can avoid all this by writing

include Microsoft::Xna::Framework

it’s more-or-less the equivalent of using namespace

class Game1 < Game

def Game1
graphics = GraphicsDeviceManager.new this
end

Ruby uses ‘self’ instead of ‘this’, and constructors are always def
initialize, not def ClassName

def Initialize

super
#is this ok?
#in C# i have base.Initialize()
end

Yep super should be fine

#is this ok?
#in C# i have protected override void Draw(GameTime gameTime)
def Draw

In ruby you should have def draw(game_time)

Game::GraphicsDevice.Clear Graphics::Color.CornflowerBlue #i don’t
think
this is is how to access the colour struct

Not sure off the top of my head but I think it should be
Color::CornflowerBlue

super gameTime

That should be fine. Normally in ruby you don’t need to pass arguments
to calls to super, but I’m not sure how IronRuby handles this when
interopping with the CLR…

#is this ok?
#in C# i have base.Draw(gameTime)
end
end

game = Game1.new
game.run

Last 2 lines look fine :slight_smile:

I’d really suggest you stop and step back a bit, and learn some of the
basics of the ruby programming language.
Also note that C# uses UpperCamelCase for everything, whereas ruby
uses lower_snake_case for everything (except class names)

IronRuby will translate these for you, so if you have a C# method
called MoveAllZig(), then ruby code to call that will be move_all_zig

Good luck!

Everything works great
Made a little c# loader app to hide all of the require and include
statements.
Orion E. wrote:

… and constructors are always def initialize, not def ClassName
There is a problem in the fact that Xna has a function that is called
every time the graphics device is lost(ex:alt+tab) and then returned
witch is called Initialize() this function is basically responsible for
initializing the main GraphisDevice object.
Is there a way to call this function?

Thanks mate I’ll put you suggestions to the test in the morning.
Oh and I’m sort of using this as a pretext to learn some Ruby,
also been playing with Hackety Hack but I don’t think I can larn the
ruby way
of OOP form it so I’m going for trial and error and forum.
Thanks again.

I’m not entirely sure I understand the problem, but “Initialize” is the
one CLR method name that we don’t mangle. Is this a Ruby class that
you’ve derived from an XNA type or just the XNA type directly?


Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

Curt H. wrote:
Hi

Is this a Ruby class that you’ve derived from an XNA type or just the XNA type > directly?
Well making a game using Xna basically meas inheriting the Game class
present in Xna and implementing it’s methods so yes it’s a ruby class
that gets derived from the Microsoft.Xna.Framework.Game class.
The structure required by your main game class is as follows:

-the constructor witch in c# is identified by the class name and in ruby
as initialize
-the graphic initialization/reinitialization method identified by the
name “Initialize”, this is the one that is making problems.
-methods for both loading and unloading content
-an update method and a draw method witch make up the actual game loop.

So again my question is how can I call the graphics initialization
function “Initialize”, from Ruby, when it has the same name as a ruby
constructor.


Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

There shouldn’t be a conflict in recent versions of IronRuby.
“Initialize” != “initialize” now.

Note that “initialize” (lower-case) doesn’t manifest itself as a CLR
constructor for the derived class. The derived Ruby class is currently
represented by a CLR type that has a single ctor taking RubyClass. We
are working on improving .NET interop here.

Tomas

Here is the source
Can anyone figure why it won’t work?

It would probably be useful if you could describe more specifically what
isn’t working.

Curt H. wrote:

There shouldn’t be a conflict in recent versions of IronRuby.
“Initialize” != “initialize” now.
Ok so I used the Upper cased Initialize and I get this exception:
" wrong number or type of arguments for `Initialize’ "
but Initialize doesn’t accept any parameters.

Note there might be something wrong with the code I wrote if someone
thinks that I will make the source available

I’ve already posted about this issue weeks ago. I was playing iwth XNA
too.

The IronRuby it’s not passing correctly the parameters for the super
class methods.

2008/12/15 Gabriel R. [email protected]:

Dudu Baião wrote:

The IronRuby it’s not passing correctly the parameters for the super
class methods.

That is a bummer :frowning:
Hopeful IronRuby will improve over time.

Guess I’ll try with IronPython

As you say “Initialize” is different from “initialize”
so I have the the constructor:

def initialize
graphics = GraphicsDeviceManager.new self
super
end

witch works
and then i have the Initialize procedure

def Initialize
super
end

witch manages the actual 3d viewport, it is called every time the 3d
viewport needs reinitialization(like an ALT+TAB) when I try to run the
script I get:
" wrong number or type of arguments for `Initialize’ "
the thing is Initialize doesn’t have any parameters, is it still being
treated as a constructor?

IronRuby is still in “alpha”. CLR interop is a place we know is still
particularly weak. IronPython just released version 2.0, and is
considerably more mature. If your goal is to have a fairly polished
dynamic language that runs on .NET, then IronPython is probably a better
choice. If you like Ruby and don’t mind a few hiccups, then your
willingness to file problem reports for issues that you find will be of
great help to us.

Things are likely to be a bit slow over Christmas, but I’ll see if I can
get this fixed before the end of the year.

Thanks Curt! I’ll be waiting for. :slight_smile:

2008/12/16 Curt H. [email protected]:

Curt H. wrote:


If you like Ruby and don’t mind a few hiccups, then your
willingness to file problem reports for issues that you find will be of
great help to us.
Ruby is great, all though I’m new to the language I have grown attached
to it.
I’ll try to give my feedback to the best of my ability because it
deserves it.

Things are likely to be a bit slow over Christmas, but I’ll see if I can
get this fixed before the end of the year.

I can’t wait for the fix. And a wish of “Happy Hollydays” to the whole
Iron Ruby team and to the users of this discussion board.