[RGSS] HANDLING MANY ARGUMENTS

Posts

Pages: 1
Dear all,

Is there an easier way to handle a method which takes a lot of arguments?

I recently created a class that helps me create animations. It's supposed to feature many parameters, for animating different aspects of a sprite. As a result, it takes a lot of arguments in its initialization:


def initialize(graphic, x, y, ox, oy, z, angles, special_commands, src_rect_loop,
relative_to, special_movement, id, using_slot, target_slot)


I was wondering if Ruby had an easier way to enter all of these any time I wanted to animate a new object.

Thank you for reading,
Zachary
The software-engineering-y way to go about this would be to create a arguments class. Or in ruby if you don't like classes, I guess this'd just be a hash called "args". Then somewhere you can have a function to initialize the args hash to sane default values. That way you have named arguments and don't have to type x=0 y=0 special_movement=false over and over

Excuse my broken ruby, I don't actually know ruby

def animationArgs()
args = { }
args[:x] = 0
args[:y] = 0
args[:graphic] = ""
end

args = animationArgs()
args[:x] = 10
args[:graphic] = whatever
Animation.initialize(args)


I think ruby's got some special syntax where you can name the arguments in-line and it results in a hash, too, if that's what you're looking for.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
You can actually pass an array into a Ruby method and it'll unpack the elements into the parameters.
Pages: 1