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.

[Resolved] Create a method with a defined instance variable?

Well, playing with the Seph Tutorial, about the RPG::Class, i was making my own method, so I get to a strange part, maybe this is impossible, or no?.

Imagine that I have a RPG::Class called something, and this something, has a attribute called 'name' and 'amount'..

It's possible do this, to make a shorter class? (I mean, to avoid to create the same method, only with change the name?

Code:
  def gain_"something[id].name"(number)

     something[id].ammount += number

  end

Any idea? :)

Thanks.
 
Not sure on what you are asking. You could pass the instance, or make a sort of case statement that gets an instance value.

Can you post your entire script?

Code:
def gain(instance, n)

  instance.amount += n

end

 

class Something

  attr_accessor :amount

  def initialize

    @amount = 5

  end

end

 

@instance = Something.new

gain(@instance, 3)

 

p @instance.amount # -> 8

Something like that?
 
Thanks I get my objective :)

I don't figure about the creation for an argument that Include the instance, now I get this:

Code:
class Game_Party

  #--------------------------------------------------------------------------

  # * Public Instance Variables

  #--------------------------------------------------------------------------

  attr_reader   :silver

  attr_reader   :copper

  #--------------------------------------------------------------------------

  # * Alias Listing 

  #--------------------------------------------------------------------------

  alias_method  :meph_curry_gprty_init,  :initialize

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    meph_curry_gprty_init

    @silver = 0

    @copper = 0

  end

  #--------------------------------------------------------------------------

  # * Get Currency

  #--------------------------------------------------------------------------

  def gain(currency, n)

    eval("@#{(cur = currency.downcase)} = [[@#{cur} + n, 0].max, 99999].min")

  end

  #--------------------------------------------------------------------------

  # * Lose Currency

  #--------------------------------------------------------------------------

  def lose(currency, n)

    gain(currency, -n)

  end

end

Well that was my objective, now is done thanks to you :)
 

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