Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 RGSS scripting dissections and explinations
#67
I wanted you to know about Inheritance and remind you about the Parent and Child Classes. At the same time, I needed you to understand that a Parent Class wears blue tights and a red cape.... er... a [Image: superman-logo-wallpaper-logo.png]UPERCLASS.

Now to talk about how one class 'inherits' and what may happen

As you may have guessed, when you decide to make a class based on another class, crafting a CHILD class, that child class carries over methods from the Parent class. It inherited 'all' the methods from the previous class. And we also say that the Parent class is the Superclass to the new class you're writing.

So let's assume you have an existing class... And ... yeah... there's not much to it.

Code:
class Pops_Class
  
  def initialize
    @name = 'pops'
  end
  
  def my_name
    return @name
  end
  
end
The initialize method creates a goofy value called @name. And the @name value is holding the name 'pops'. The my_name value just returns the data of the @name value. So calling (or using) my_name just supplies you with the name in there.

If we made a class called Kid_Class like THIS:
Code:
class Kid_Class < Pops_Class
end
And it will actually inherit both the initialize and my_name methods!!!

BUT, what happens if you ... perform a change within the Kid_Class????

Code:
class Kid_Class < Pops_Class
  
  def initialize
    @name = 'Brat'
  end
  
end
Ooooh.... This one does not inherit the initialize method. It destroys it and creates its own initialize method. This new initialize method creates a new initialize method and new @name value storing the name 'Brat' this class does inherit the my_name method just as its parent class, so it will return the value of the @name value... returning 'Brat' instead of 'pops'

And...
Code:
class Kid_Class < Pops_Class
  
  def initialize
    @name2 = 'Brat'
  end
  
end
This likewise rewrites the initialize method, this time not even creating a @name variable but a @name2 variable instead. Yes, this version has a my_name call, but running it might go and *CRASH* as this class doesn't have a @name variable.

So, now let's introduce something new....

Code:
class Kid_Class < Pops_Class
  
  def initialize
    super()
    @name2 = 'Brat'
  end
  
end
What's that super() statement?????

As stated some time earlier, a Parent class is also called a superclass to the class. The super() allows you to create a new method, but include all the material and statements from the original within. So the above version with the super() statement acts like this!


Code:
class Kid_Class < Pops_Class
  
  def initialize
    @name = 'pops'
    @name2 = 'Brat'
  end
  
end

Everything from the first statement is replicated, pasted, SHOVED into where the super() statement is!!!! So this version with the super() statement creates both a @name and @name2 value.

So now let's try something practical and already existing.......

Let's look at the initialize method of the Game_Event class.

Code:
def initialize(map_id, event)
    super()
    @map_id = map_id
    @event = event
    @id = @event.id
    @erased = false
    @starting = false
    @through = true
    # Move to starting position
    moveto(@event.x, @event.y)
    refresh
  end
Oh, wow. First, this class is a child class of the Game_Character class. but it also has a super() statement, which means this class not only inherits methods from Game_Character, but this totally rewritten 'initialize' class has some of Game_Character's initialize method thrown in!

Here is the Initialize method from Game_Character
Code:
def initialize
    @id = 0
    @x = 0
    @y = 0
    @real_x = 0
    @real_y = 0
    @tile_id = 0
    @character_name = ""
    @character_hue = 0
    @opacity = 255
    @blend_type = 0
    @direction = 2
    @pattern = 0
    @move_route_forcing = false
    @through = false
    @animation_id = 0
    @transparent = false
    @original_direction = 2
    @original_pattern = 0
    @move_type = 0
    @move_speed = 4
    @move_frequency = 6
    @move_route = nil
    @move_route_index = 0
    @original_move_route = nil
    @original_move_route_index = 0
    @walk_anime = true
    @step_anime = false
    @direction_fix = false
    @always_on_top = false
    @anime_count = 0
    @stop_count = 0
    @jump_count = 0
    @jump_peak = 0
    @wait_count = 0
    @locked = false
    @prelock_direction = 0
  end

So essentially, what the Game_Event class really does is it replaces the super() statement with all the stuff from Game_Character... like this:

Code:
def initialize
    @id = 0
    @x = 0
    @y = 0
    @real_x = 0
    @real_y = 0
    @tile_id = 0
    @character_name = ""
    @character_hue = 0
    @opacity = 255
    @blend_type = 0
    @direction = 2
    @pattern = 0
    @move_route_forcing = false
    @through = false
    @animation_id = 0
    @transparent = false
    @original_direction = 2
    @original_pattern = 0
    @move_type = 0
    @move_speed = 4
    @move_frequency = 6
    @move_route = nil
    @move_route_index = 0
    @original_move_route = nil
    @original_move_route_index = 0
    @walk_anime = true
    @step_anime = false
    @direction_fix = false
    @always_on_top = false
    @anime_count = 0
    @stop_count = 0
    @jump_count = 0
    @jump_peak = 0
    @wait_count = 0
    @locked = false
    @prelock_direction = 0
    @map_id = map_id
    @event = event
    @id = @event.id
    @erased = false
    @starting = false
    @through = true
    # Move to starting position
    moveto(@event.x, @event.y)
    refresh
  end

That's sorta the BASICS of the super() statement. However, there's more to it. But I do want you to understand this before I cover something called parameters or arguments.

NO ARGUMENTS FROM THE AUDIENCE, PLEASE!!!!
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png] [Image: liM4ikn.png] [Image: fdzKgZA.png] [Image: sj0H81z.png]
[Image: QL7oRau.png] [Image: uSqjY09.png] [Image: GAA3qE9.png] [Image: 2Hmnx1G.png] [Image: BwtNdKw.png%5B]
Above are clickable links

Reply }


Messages In This Thread
RE: RGSS scripting dissections and explinations - by DerVVulfman - 11-11-2018, 05:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Help iwth script (RGSS Player crash) Whisper 3 7,674 06-17-2017, 05:03 PM
Last Post: Whisper
  How can I use the cmd of "require" in rgss superegp 2 5,383 11-03-2015, 06:16 AM
Last Post: kyonides
   Scripting in VX vs VX Ace Miharu 5 8,207 02-21-2015, 10:10 AM
Last Post: Taylor
   Combat animations via scripting; How? ZeroSum 2 4,572 09-20-2013, 06:58 PM
Last Post: ZeroSum
Question  RGSS stoped to work Chaos17 5 6,930 02-14-2013, 05:13 PM
Last Post: DerVVulfman
   Ruby, RGSS & General Code Discussion Kain Nobel 6 9,916 12-22-2012, 05:11 AM
Last Post: MechanicalPen
   [Request] Tut. for RGSS Eldur 9 10,612 12-07-2012, 04:27 AM
Last Post: DerVVulfman
   [ASK-RGSS] Behemoth's CBS alike Getsuga_kawaii 0 3,863 04-29-2010, 03:07 PM
Last Post: Getsuga_kawaii
   Scripting I think spazfire 7 8,983 04-12-2010, 03:21 AM
Last Post: DerVVulfman
   Beginner Scripting Tuts? KDawg08 1 3,685 03-31-2010, 11:03 PM
Last Post: Hsia_Nu



Users browsing this thread: