I have a program like the following, where I the instances are declared
within the body of the program and I'd like to access 'count' in a
method of obj2, as indicated, but it doesn't work.
I thought it might be because object one needed to be a global variable
but according to http://www.rubyist.net/~slagell/ruby/localvars.html
it's scope would be the entire script anyway as it is not defined with a
block. I tried using $obj1 and $obj2 anyway but I can't seem to refer to
$obj1.count within $obj2's methods.
If I'm reading things right, class variables would be of no use either
in this situation. So, short of reading the value from obj1 first then
passing it as a parameter to obj2 is there another way?
TIA
Paul
class One
attr_reader :count
def initialize
...
end
end
class Two
def initialize
...
end
def someMethod #access One.count here
end
end
Is there a particular reason why you are approaching it like this? I do
believe this looks like the wrong approach, but I'm not positive what purpose
you are going for.
-c
···
On Wed, Feb 26, 2014 at 03:17:09PM +0100, Paul Robinson wrote:
I have a program like the following, where I the instances are declared
within the body of the program and I'd like to access 'count' in a
method of obj2, as indicated, but it doesn't work.
I thought it might be because object one needed to be a global variable
but according to http://www.rubyist.net/~slagell/ruby/localvars.html
it's scope would be the entire script anyway as it is not defined with a
block. I tried using $obj1 and $obj2 anyway but I can't seem to refer to
$obj1.count within $obj2's methods.
If I'm reading things right, class variables would be of no use either
in this situation. So, short of reading the value from obj1 first then
passing it as a parameter to obj2 is there another way?
TIA
Paul
class One
attr_reader :count
def initialize
...
end
end
class Two
def initialize
...
end
def someMethod #access One.count here
end
end
you do need an instance of One to access an instance variable
So you can pass obj1 as a parameter to obj2.someMethod
or maybe obj1 should be created inside someMethod
···
On Wed, Feb 26, 2014 at 9:17 AM, Paul Robinson <lists@ruby-forum.com> wrote:
I have a program like the following, where I the instances are declared
within the body of the program and I'd like to access 'count' in a
method of obj2, as indicated, but it doesn't work.
I thought it might be because object one needed to be a global variable
but according to http://www.rubyist.net/~slagell/ruby/localvars.html
it's scope would be the entire script anyway as it is not defined with a
block. I tried using $obj1 and $obj2 anyway but I can't seem to refer to
$obj1.count within $obj2's methods.
If I'm reading things right, class variables would be of no use either
in this situation. So, short of reading the value from obj1 first then
passing it as a parameter to obj2 is there another way?
TIA
Paul
class One
attr_reader :count
def initialize
...
end
end
class Two
def initialize
...
end
def someMethod #access One.count here
end
end
Thanks Craig, I'm trying to write a very simple game; I realise that
Ruby isn't the right vehicle for writing games (!) but hopefully it will
help me learn Ruby and have some fun along the way. I've got a 'world'
object and a 'player' object which have very different attributes and
methods but each may need to access the information contained in the
other, for example the player might only move in a certain direction
depending upon where he is in the world.
Chris, I think that may be way to go, i.e. creating the player object
within the world object (sounds logical too!) - I've never done anything
like that before but a quick search shows that such nested or inner
classes might be just right.
Thanks all, that's all useful info and most welcome (I never expected
those game libraries for Ruby). Maybe I should say that I've written,
co-written or been involved in a fair few 2D/3D freeware games before
(C, Blitz Basic) and have a fair bit of experience with Java/Processing.
I'm not writing this for anyone except myself - it will be a tiny game,
text only (but maybe using curses) and just to help me learn more about
Ruby.
I've just modified the few hundred lines of code such that the player is
part of the world as per Ricky's suggestion, let's see where it goes...
Thanks Robert, I've never had cause to try creating one object inside
another in Java (or any other language) but as I say, my Java experience
is not extensive.
I've just modified the few hundred lines of code such that the player is
part of the world as per Ricky's suggestion, let's see where it goes...
The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction.
Haha, *the program* is a few hundred lines, I didn't have to modify all
of them - if that were the case even I could tell that the program had a
serious design flaw.
Incorporating the player into the world class has certainly made things
a lot cleaner so far.
Thanks Regis, I need to think more about the example you've given!
Those have been super helpful to me, and many other code slingers. To that
end, I think you are taking the wrong architectural approach, but it's
great that you are giving it a go!
-c
···
On Wed, Feb 26, 2014 at 09:20:36PM +0100, Paul Robinson wrote:
Thanks Craig, I'm trying to write a very simple game; I realise that
Ruby isn't the right vehicle for writing games (!) but hopefully it will
help me learn Ruby and have some fun along the way. I've got a 'world'
object and a 'player' object which have very different attributes and
methods but each may need to access the information contained in the
other, for example the player might only move in a certain direction
depending upon where he is in the world.
Chris, I think that may be way to go, i.e. creating the player object
within the world object (sounds logical too!) - I've never done anything
like that before but a quick search shows that such nested or inner
classes might be just right.
Ruby actually isn't that bad for games. Native ruby can use http://www.libgosu.org/ and JRuby can use any of the Java frameworks.
And for your actual issue, organizing complex OO programs is quite hard.
What you want to do is not actually have nested/inner classes exactly. You
just want to have instances of world contain instances of player. So
something kinda like:
class World
attr_accesor :player
def tick
self.player.update() unless self.back_hole?
end
end
w = World.new
p = Player.new
w.player = p
An inner class would imply that the Player is tightly coupled with the
World. This would mean that say if you original had a Player that acted
like a fish, it would be much harder to add a second Player that acts like
a jellyfish. If you will, think of the World class as god. It tells all the
players simply to live and it is up to the players to figure out how to do
it.
"Practical Object-Orientated Design in Ruby" by Sandi Metz explains this
much better than I can.
···
On Wed, Feb 26, 2014 at 2:20 PM, Paul Robinson <lists@ruby-forum.com> wrote:
Thanks Craig, I'm trying to write a very simple game; I realise that
Ruby isn't the right vehicle for writing games (!) but hopefully it will
help me learn Ruby and have some fun along the way. I've got a 'world'
object and a 'player' object which have very different attributes and
methods but each may need to access the information contained in the
other, for example the player might only move in a certain direction
depending upon where he is in the world.
Chris, I think that may be way to go, i.e. creating the player object
within the world object (sounds logical too!) - I've never done anything
like that before but a quick search shows that such nested or inner
classes might be just right.
Thanks Craig, I'm trying to write a very simple game; I realise that
Ruby isn't the right vehicle for writing games (!)
the issue with game & ruby is performence,
object design of a game is easy in ruby (as any good object langage)
but object design of a game not so easy....
I've got a 'world'
object and a 'player' object which have very different attributes and
good start,
like that before but a quick search shows that such nested or inner
classes might be just right.
Inner class are not necessary...
Here a example
···
===
class Actor
def collision(pt,r)
...
end
end
class Player < Actor; ... end
class Robot < Actor; .... end
====
class World
def initialize()
@player= Player.new(self) @lrobots = (0..10).each.map { Robot.new(self) }
end
def tick @player.tick @lrobots.each { |r| r.tick }
end
def tst_collision_robot(p) @lrobots.select { |r| r.collision_with(p.position,p.radius) }
end
. . .
end
see rrobots (many versions),
In gosu world ( with ruby 1.9 only, 2.x not yet supported),
ang : one player with obstacles and targets... (github / gem)
Let me put my 5 cents about game development in ruby. Someone says that ruby isn’t suitable for gamedev, but it depends on your purposes. If want to implement AAA crisis-like FPS, ruby is not your tool of choice. But if you want to take a part in some game jams like Ludum Dare, ruby is awesome tool. I hardly recommend you blog of this guy http://spooner.github.io/ to make sure that ruby gamedev isn’t a myth. My advice to use: gosu(graphics rendering, game loop, sound, etc), chipmunk(game physics), ashton(shaders), releasy(packager).
···
--
Iurii Plugatariov
Sent with Airmail
On February 26, 2014 at 22:50:28, Craig Monson (craig@malachiarts.com) wrote:
Those have been super helpful to me, and many other code slingers. To that
end, I think you are taking the wrong architectural approach, but it's
great that you are giving it a go!
-c
On Wed, Feb 26, 2014 at 09:20:36PM +0100, Paul Robinson wrote:
Thanks Craig, I'm trying to write a very simple game; I realise that
Ruby isn't the right vehicle for writing games (!) but hopefully it will
help me learn Ruby and have some fun along the way. I've got a 'world'
object and a 'player' object which have very different attributes and
methods but each may need to access the information contained in the
other, for example the player might only move in a certain direction
depending upon where he is in the world.
Chris, I think that may be way to go, i.e. creating the player object
within the world object (sounds logical too!) - I've never done anything
like that before but a quick search shows that such nested or inner
classes might be just right.
Thanks all, that's all useful info and most welcome (I never expected
those game libraries for Ruby). Maybe I should say that I've written,
co-written or been involved in a fair few 2D/3D freeware games before
(C, Blitz Basic) and have a fair bit of experience with Java/Processing.
This slightly confuses me: you state that you have experience doing
Java coding but your question is about basic OO principles. These are
the same across OO languages: separations of concerns, information
hiding, internal state consistency... In what way is Ruby so different
that you feel you need to ask about this? Maybe there's more to the
question that we haven't grasped yet.
I'm not writing this for anyone except myself - it will be a tiny game,
text only (but maybe using curses) and just to help me learn more about
Ruby.
Nothing wrong with that. And Ruby is certainly suited for that type of
game development.
I've just modified the few hundred lines of code such that the player is
part of the world as per Ricky's suggestion, let's see where it goes...
The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction.
Kind regards
robert
···
On Wed, Feb 26, 2014 at 10:20 PM, Paul Robinson <lists@ruby-forum.com> wrote:
Thanks Robert, I've never had cause to try creating one object inside
another in Java (or any other language) but as I say, my Java experience
is not extensive.
Oh, I thought so from "have a fair bit of experience with Java/Processing".
I've just modified the few hundred lines of code such that the player is
part of the world as per Ricky's suggestion, let's see where it goes...
The fact that you needed to change so many lines of code may be
indicative of room for improvement of your abstraction.
Haha, *the program* is a few hundred lines, I didn't have to modify all
of them
Oh, then I misunderstood. It sounded to me as if you had modified a
few hundred lines of code.
- if that were the case even I could tell that the program had a
serious design flaw.
Incorporating the player into the world class has certainly made things
a lot cleaner so far.
Good!
Thanks Regis, I need to think more about the example you've given!
Reasoning about code is good and important - trying out things as well.
Cheers
robert
···
On Thu, Feb 27, 2014 at 2:46 PM, Paul Robinson <lists@ruby-forum.com> wrote: