0
Answered

public override void onStart()

eagleeyez 2 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 3

What is this Override stuff? I only find Start in Bolt, how do I start with this override?

Sorry for the stupid question, I don't know enough c# at this moment but I am learning.

 // Use this for initialization
        public override void Start () {
            // Start base
            base.Start();
            // Set the flash icon
            SetFlashIcon();
        }
        #endregion

        
        #region --NatCam and UI Callbacks--

        public override void OnStart () {
            // Display the preview
            panel.Apply(NatCam.Preview);
            // Start tracking focus gestures
            focuser.StartTracking();
        }
       


Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

c# allows you to re-use methods from scripts that are 'higher' in the food-chain, so-called ancestors. When you look at a script in Unity, you usually see the term 'Monobehaviour' at the beginning. This means that your script is based on the Monobehaviour script (and all scripts *that* is based upon, all the way up), with the nice advantage that you can use all the methods and properties (variables) that Monobehavior has already defined for you: transform, gameObject, GetComponent, etc. This is called 'inheriting'.

Now, at some point in time you might decide that you want to change the way a certain method you have inherited behaves. You can do that simply by creating a new method with the same name, for example GetComponent. If you do that, your new method replaces the inherited function completely (and you will get a warning in console that you did that). Whenever you called GetComponent in your script, your new implementation would be called.

You could, however - and this is clever - acknowledge that you are writing a new method for GetComponent, but at the same point, you want to be able to invoke the old version. This is usually done to extend a capability, and one of the killer features of modern programming. In order to acknowledge this, use the 'override' keyword. If you want to access the already implemented, and now overridden method, you can access it by prepending 'base.' to the method name, or base.GetComponent() in our example.

So why am I blabbing on about this? Because when you see that you need to use override, someone else has already implemented that method further up. To indicate that methods can be overridden, they usually use the 'virtual' keyword where you are now asked to put 'override'. So look for 'public virtual void Start()' and you shall find the original implementation for Start().

In most cases, when you need to place 'override' in the method name, make sure to call 'base.myMethodName()' as the fist thing to ensure that the code you are basing on is correctly initialized.

Hope this helps,

-ch


Oh Shit! That was one of the best explanation’s I’ve ever heard. I even understand it now.

Thank you very much

Answered

This seems to be related to NatCam, not to Unity or Bolt. 

NatCam is what defines a virtual onStart method that you can override.