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.

What is Super?

Hello, Again with my Silly Questions :wink:, but This is something that don't let me sleep.

Well, I see in a lot of SDK scripts, that there is a super at the begin of the, for example Scene methods definitions, or in the update.

Now, I only copied that, but now, I see scripts that uses at the end of the scene, example:

Code:
  #--------------------------------------------------------------------------

  # * Main Processing : Spriteset Initialization

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

  def main_spriteset

    super

    @spriteset = Spriteset_Map.new

  end

but, there is also:

Code:
  #--------------------------------------------------------------------------

  # * Main Processing : Spriteset Initialization

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

  def main_spriteset

    @spriteset = Spriteset_Map.new

    super

  end

Why?, I don't understand this?, what's the function for super, and why sometimes is below or above...??


Thanks.
 
I know one function for super, and yes there are more but I dont know them. One of them is like:
Code:
 

super(0, 0, 640, 480)

 
Where you can, of coures, change the numbers. The first one is the X of the window, second one the Y, 3rd and 4rd width and heigth respectively.
 
Yes, I knew that also, but Thanks anyway ;)

Now, I have noticed, when i used super, the lag increase, the FPS down by 10 aprox. But I noticed super is Innecesary, in the spriteset case, but, why do I should use it?
 
I'm not an expert in that matter but, as far as I know, super calls the original method of the object's Superclass like that
Code:
 

class MySuperClass

  def do_something(arguments)

    # blablabla

  end

end

 

class MyChildClass < MySuperClass

  def do_something(arguments)

    # call do_something from the Superclass

    super(arguments)

    # do something else

  end

end

 

In the case of Window_Base (Superclass for most windows),
the initialize method requires 4 arguments; X, Y, Width and Height.

Now, if you take a look at any other windows, in initialize, you'll see
super(arg1, arg2, arg3, arg4). That means the script is calling the initialize method from Window_Base with the 4 required arguments.

Most of the time, using super is essential. For example, methods like initialize, dispose or update often need to use super if they have a superclass doing something important.

Hope it helps
-Dargor
 
It's a farily simple concept. It simply calls the method to which the super call is in to the parent class (As Dargor has illustrated).

Placing the super call before or after a code bloc in a method really only depends on interaction with the parent method. Consider the following:

Incorrect:
Code:
class Something

  def initialize(a)

    @a = 5

  end

end

 

class Something_Else < Something

  def initialize(add = 3)

    @a += add

    super()

  end

end

The super call needs to come first because @a has not been initialized

Correct:
Code:
class Something

  def initialize(a)

    @a = 5

  end

end

 

class Something_Else < Something

  def initialize(add = 3)

    super()

    @a += add

  end

end
 
I have some question on the method super.
I know to use it, and I totally agree with the explanation above.

In this case, I can't explain it :
Code:
class Test

  def method

    p 0

  end

end

 

class MiniTest < Test

  def method

    super

    p 1

  end

  

  alias old_method method

  def method

    old_method

    p 2

  end

end

 

MiniTest.new.method
It prompts 0, 1 and 2.
The only problem is that there is no metho "old_method" for the class Test.
Why it's working ?

The second question is that super call the method on the parent class.
Is it possible to call a method older ?

Ex: Window > Window_Base > Window_Selectable
Can we call the method of the class Window from a method from Window_Selectable, without call the method of Window_Base ?

Thank by advance.
 
Ok. Try thinking of your code like this:

Code:
class Test

  def method

    p 0

  end

end

 

class MiniTest < Test

  def method

    old_method

    p2

  end

 

  def old_method

    super # Although this method is now called old_method

          # It still calls "method" from the parent class

    p 1

  end

end

 

MiniTest.new.method

I made a method to skip super calls.
Code:
class Module

  def super_skip(method_name, tpc, script_name = 'modulemethodskip')

    method_name = method_name.id2name

    ancestors = self.ancestors

    unless ancestors.include?(tpc)

      p 'Un-defined Parents Class - ' + tpc.to_s

      return

    end

    index = 0

    target_index = ancestors.index(tpc)

    p ancestors, index, target_index

    loop do

      index += 1

      break if index >= target_index

      cname1 = ancestors[index].to_s

      cname2 = ancestors[index - 1].to_s

      aliasn = "seph_#{script_name}_#{cname1.downcase}_#{method_name}"

      string  = "class ::#{cname1};"

      string += "  alias_method :#{aliasn}, :#{method_name};"

      string += "  def #{method_name}(*args);"

      string += "    super(*args);"

      string += "    return if self.is_a?(#{ancestors[index - 1]});"

      string += "    self.#{aliasn}(*args);"

      string += "  end;"

      string += "end;"

      eval string

    end

  end

end

Example of usage:
Code:
class A

  def blah

    p 1

  end

end

 

class B < A

  def blah

    p 2

  end

end

 

class C < B

  super_skip :blah, A

  def blah

    super

  end

end

It was kinda complicated process to write that, but works well enough (as far as my testing goes).
 

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