Jump to content

Why is base.OnStart(state) not recursive within the method OnStart?


Recommended Posts

Hello all!

I am working on reverse-engineering a mod and I have run into an issue with my understanding of C#.  The mod has this method:

 

public class CivilianPopulationRegulator : BaseConverter
  {
    public override void OnStart(PartModule.StartState state)
    {
      if (!HighLogic.LoadedSceneIsFlight)
      {
        base.OnStart(state);
        return;
      }
      if (this.vessel == null)
      {
        base.OnStart(state);
        return;
      }

      //do some stuff later

      base.OnStart(state);//Not sure what this does at the moment.
    }
  }

Looking at the "base" keyword, it is used to call a method in the base class that is being overriden or it is used to call a constructor.  Unless OnStart is a constructor, isn't this going into the extended class (BaseConverter, which extends PartModule) and run OnStart again?  At the end of which, OnStart will be called yet again.  And so on infinitely?

So since the code is not infinitely recursive, I must be mistaken in either my understanding of OnStart or the "base" keyword.  Would any of you guys know why this works and/or where I could learn more about this?

Edited by Tralfagar
Mistyped the title. OnState -> OnStart
Link to comment
Share on other sites

You might want to read up on virtual methods a bit.  There's no loop going on, it's just calling OnStart on the base class.  When OnStart() is called on an instance of the derived class, that will always use the derived class's implementation, but you have a mechanism for also calling the base class's implementation if need be, through base.OnStart()

Link to comment
Share on other sites

@blowfish thank you very much.  I didn't realize I was barking up the wrong tree.  I just read up on virtual methods and override and now it makes sense.

OnStart within my class, CivilianPopulationRegulator, is an override method that overrides the OnStart that is inherited from BaseConverter.  When the method is overridden, it is called as normal but the method statements are overridden.  By using base.OnStart(state), the original OnStart method is called which does not have any of my modifications.

 

Thank yoU!

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...