Using Inheritances

By HeliJunkie

What do you mean with inheritance?

With inheritance we mean, that you can "copy" all properties from an existing class to a new class.

Why do we need it?

You don't need it. But it makes coding dialogs a lot easier. It has 2 main advantages:

  1. You don't need to define all those properties again! You only have to modify existing properties or add missing or new properties.
  2. If you change a value in the class you inherit from, the value also changes in the new class.

Using inheritance with our button

I'm not a fan of too much theory. Let us build an example and test this great feature.
One button is too little for most dialogs. So we add a second one. It should look like the first one. And
it should be placed right of it. Let's use inheritance. To inherit from another class in our new one,
we need only to attach a ":" behind the class name, and the name of the class from which we want to
inherit. We want to display a hint with our new button, so we decide to name it "HintButton". We directly add it to the dialog controls list.
This is what we should code:

class SampleDialog {
  //..
  controls[] = {
    "ButtonControl",
    "HintButton"
  };
  class ButtonControl {
  //..
  };
  Class HintButton : ButtonControl {
  };
};

If we would now use this dialog, we really have two controls, but we won't see the second one, because they have the same size, and the same position. So one would overlay the other. So let us move the second button a little bit to the right, and give it another action and another text.

class SampleDialog {
  //..
  controls[] = {
    "ButtonControl",
    "HintButton"
  };
  class ButtonControl {
  //..
  };
  Class HintButton : ButtonControl {
    x = 0.6;
    text = "Hint";
    action = "hint ""Hint from second button! "";";
  };
};

The first button was placed at x=0.45 and has a width of 0.1. So the right bound of the control will be 0.55. We left a space of 0.05 to the next button. So our button should start at 0.6. It will display "Hint" as text. If we press the button it will display another hint to the screen. Here is a screenshot:

 

So we created a new control by only extending one line (controls[]=) and adding 5 lines of code. If we define the button from scratch, we would have to code a lot more! Use inheritance as often as you can. It makes life a lot easier. And there is another reason why you should use inheritance: Easy... alignment. If you now want to place the buttons a little further up, you only need to change the "y" property of your first control. The second control will inherit the value from the first control and will move up too! Easy...