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.

"is_a?" real mystery + more on arrays

Aran

Member

Okay, I read on ruby-language (the site :P) that is_a? returns an array, but I don't understand. And second, how do you add to an empty array? third when you use is_a? does that object go to the end of the array?
 
1. "is_a?" checks if the word before it is equal to the word in parenthesis. The following example would check
if "$scene" is equal to "Scene_Title".

Example:
Code:
if $scene.is_a?(Scene_Title)
  # code
end

2. There are two ways. This example adds to the end of the array:
Code:
array.push(object)

And this example adds the object to the array's index specified:
Code:
array[0] = object

3. I don't think you understand what "is_a?" does.
 
.is_a? Returns true of false when comparing an objects class

Code:
def is_a?(class)
  return self.class == class
end

The actuall mathod probably looks similar to that. it simply checks to see if an objects class is whatever.

So I really have no idea what you are talking about with arrays...
 
Trickster explained well how the is_a? works. But THIS is the default, it isn´t supposed to return arrays. Maybe you misread that or the method was altered by the writer! xD

About Array, daniel explained well too, i just have to add something...
unshift
Adds the given element(s) to the beginning of the array.
Code:
a = []    => []
a.unshift "a"    => ["a"]
a.unshift "b", "c", "d"    => ["b", "c", "d", "a"]

insert
Inserts the given element(s) in the given position.
Code:
a = []    => []
a.insert 1, "a"    => [nil, "a"]
a.insert 0, "b", "c", "d"    => ["b", "c", "d", nil, "a"]

Please correct me if i´m wrong, i´m in a hurry now ^^
 
I use the help file a lot myself too :P I'm finding it helps less over time though...and it would have solved this question. Sometimes we forget that the help file has been translated, and that it pertains to scripting RGSS.

SephirothSpawn said:
.is_a? Returns true of false when comparing an objects class

Code:
def is_a?(class)
  return self.class == class
end

The actuall mathod probably looks similar to that. it simply checks to see if an objects class is whatever.

So I really have no idea what you are talking about with arrays...

If it were that simple, is_a? probably wouldn't exist. It's a definition buried deep within class Object that is probably not defined in Ruby, but in the mother language used to interpret it. It looks to see the signature of the object in memory (inaccessible by ordinary RGSS means) and compares it to the defined class in question.
 
I'm not uber amazing at scripting, but just thought I'd say a really useful use for is_a?
You can use it to check if a message is being displayed
Code:
$window.is_a?(Window_Message)
And then do coding if it's not
eg. only refresh bars when there's no dialogue (reduces lag i think) and when I made a game where one level the map changes season every now and again I had to update scene_map so the events change so I check if a message was displaying so I didn't get rid of 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