D
David McCabe
Hi folks.
I'm pretty comfortable with Python, and I just decided to give Ruby a
shot. I'm trying to duplicate a nifty method I use in Python to
instantiate objects with properties that are loaded from a file.
My program is a war strategy game, which involves several different
types of artillery units. Each type has properties such as range,
speed, power, etc. I need to load these values from a file.
In python, my parser returns a hash of unit_type objects, where the
names of units are the keys. Each time a unit is instantiated, it
receives a unit_type object as an argument. Now here's the interesting
part: it copies the instance variables from the unit_type into itself.
class Unit(object):
def __init__(self, type):
self.type = type
self.__dict__.update(self.type.__dict__)
There's more to it than that, but that's the interesting part. So now
I can say:
my_new_unit = Unit(types["Panzer"])
And I'll get a Unit object with all the values for Panzers
initialized.
drbrain on #ruby-lang came up with this:
http://rafb.net/paste/results/lgb4xv42.html
It looks like it'll do what I want. There's only one deficiency: I
would have to list out every possible proporty of a unit in two
seperate places in the source. In the python version, I don't have to
list it at all. Whatever properties are in the config file are loaded
into the object.
So, now I'm looking for any pointers on ways to do this.
Thanks!
I'm pretty comfortable with Python, and I just decided to give Ruby a
shot. I'm trying to duplicate a nifty method I use in Python to
instantiate objects with properties that are loaded from a file.
My program is a war strategy game, which involves several different
types of artillery units. Each type has properties such as range,
speed, power, etc. I need to load these values from a file.
In python, my parser returns a hash of unit_type objects, where the
names of units are the keys. Each time a unit is instantiated, it
receives a unit_type object as an argument. Now here's the interesting
part: it copies the instance variables from the unit_type into itself.
class Unit(object):
def __init__(self, type):
self.type = type
self.__dict__.update(self.type.__dict__)
There's more to it than that, but that's the interesting part. So now
I can say:
my_new_unit = Unit(types["Panzer"])
And I'll get a Unit object with all the values for Panzers
initialized.
drbrain on #ruby-lang came up with this:
http://rafb.net/paste/results/lgb4xv42.html
It looks like it'll do what I want. There's only one deficiency: I
would have to list out every possible proporty of a unit in two
seperate places in the source. In the python version, I don't have to
list it at all. Whatever properties are in the config file are loaded
into the object.
So, now I'm looking for any pointers on ways to do this.
Thanks!