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] ()= or []=

Status
Not open for further replies.

Aran

Member

I'm not sure which one I saw, but I was competely lost when I saw a method name as this: ()= or, this []= (one of them)

in other words, what does:
Code:
def []= #or, ()=
#blah
end
 

Anonymous

Guest

It was []=, and it defines the operator [] for use with objects of that type. It's usually used for array wrapper data types.

Look at Game_Actors:

Code:
class Game_Actors
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    @data = []
  end
  #--------------------------------------------------------------------------
  # * Get Actor
  #     actor_id : actor ID
  #--------------------------------------------------------------------------
  def [](actor_id)
    if actor_id > 999 or $data_actors[actor_id] == nil
      return nil
    end
    if @data[actor_id] == nil
      @data[actor_id] = Game_Actor.new(actor_id)
    end
    return @data[actor_id]
  end
end

$game_actors is not an array, but you can use $game_actors to access the actors at the various indices just like an array thanks to the #[] method.

[]= would allow you to assign the object to an array. For example, if we were adding that method to a class:

Code:
class Foo
  def initialize
    @data = []
  end

  def []=(somearray)
    @data = somearray
  end

  def [](i)
    return @data[i]
  end
end

We could then do this:

Code:
foo = Foo.new
foo = [1, 2, 3, 4]
print foo[1]  # prints "2"
foo = []

And so on.
 

Aran

Member

*tear comes to eye* beautiful...

I'm learning a lot today... already...

thanx, ccoa. I would believe its resolved now... unless someone else wants to reply... I guess...
 
Status
Not open for further replies.

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