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.

Custom Transitions @ Higher Resolutions

Greetings,

I'm back and am working on a few projects. One of which will utilize multiple resolutions. I had a script I wrote before that allowed a basic fade to/from black for the resolution, and now I am in the process of modifying it to also allow other transitions. My issue right now is this: I'm trying to define a method where any and all variables are optional. For example:

Code:
module foo

  def bar(duration=16, file='', invert=false)

    #some code

  end

end

 

How would I make it so that:

  • If I were to call
    Code:
    foo.bar
    it would run the default fade transition in 16 frames.
  • If I were to call
    Code:
    foo.bar(20)
    it would run the default fade transition in 20 frames.
  • If I were to call
    Code:
    foo.bar('trans1')
    it would fade to/from trans1.png in the transitions folder in 16 frames
  • If I were to call
    Code:
    foo.bar(20, 'trans1')
    it would fade to/from trans1.png in the transitions folder in 20 frames
  • If I were to call
    Code:
    foo.bar('trans1', true)
    it would fade to trans1.png in the transitions folder in 8 frames and fade from invert-trans1.png in the transitions folder in 8 frames
  • and if I were to call
    Code:
    foo.bar(20, 'trans1', true)
    it would fade to trans1.png in the transitions folder in 10 frames and fade from invert-trans1.png in the transitions folder in 10 frames

Any help would be appreciated. Note, I know how to make it run the transitions themselves, I just want to know how to make the declared variables optional as i have demonstrated.

Thank you,

Draycos Goldaryn
 
Greetings.

Actually, Google helped me out quite a bit this time....

I found this:
Code:
def foo(*my_string)  

  my_string.each do |words|  

    puts words  

  end  

end  

foo('hello','world')  

foo()

I modified it to:
Code:
module Foo

  def self.bar(*args)

    i=0

    arg=Array.new

    args.each do |words| 

      arg[i]=words

      i+=1

    end 

    case arg.size

    when 0

      duration,file,invert=16,'',false

    when 1

      if arg[0].integer?

        duration,file,invert=arg[0],'',false

      else

        duration,file,invert=16,arg[0],false

      end

    when 2

      if arg[0].integer?

        duration,file,invert=arg[0],arg[1],false

      else

        duration,file,invert=16,arg[0],arg[1]

      end

    when 3

      duration,file,invert=arg[0],arg[1],arg[2]

    end 

  end

end

Now my question is this: can I clean it up any further?

Thank you,

Draycos Goldaryn
 
Code:
module Foo

    def Foo.bar(*args)

        duration = file = invert = nil

        args.each do |i|

            if i.is_a?(Integer)

                duration = i

            elsif i.is_a?(String)

                file = i

            elsif i.is_a?(Boolean)

                invert = i

            end

        end

        duration ||= 16

        file ||= ''

        invert ||= false

    end

end

If you want to do it this way, I think this is the best code possibility.
Ruby 1.9 has better support of that problem:
Code:
Foo.bar(:duration => 10, :invert => true)
This can be simulated with a Hash. This way you can have more than one parameter of a class:
Code:
module Foo

    def Foo.bar(args_hash)

        duration = args_hash[:duration] || 16

        file = args_hash[:file] || ''

        invert = args_hash[:invert] || false

    end

end
This is the way I would do it.
 

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