Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to write a function which receives different types of parameters?

Hideo

Member

Let's take the class in RGSS script as an example, say Bitmap.

When we draw a text on a bitmap which is an instance of Bitmap,
bitmap = Bitmap.new(width, height)

we can either

# # # # # # # # # # # # # # # # # #
bitmap.draw_text(x,y,width,height,text)
# # # # # # # # # # # # # # # # # #

or we can

# # # # # # # # # # # # # # # # # #
bitmap.draw_text(rect,text)
# # # # # # # # # # # # # # # # # #

So how to create a def which can either receive x,y,width,height or rect?

In other language, we may simply build two functions with different parameters , however for Ruby, after we writed a new def, the old one was automatically covered. So I wonder how to write something like Bitmap.draw_text.

Thanks all ^_^
 

OS

Sponsor

Try using the #instance_of? method to determine what kind of value was actually passes. That way, you can decide how the method you are making should react to different parameters.

Code:
if @param.instance_of?(Rect)
   @param.rect_method_I_dont_know_about
elsif @param.instance_of?(Integer)
   @param.bitmap_method_I_dont_know_about

Do you understand?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top