﻿1
00:00:04,650 --> 00:00:10,830
‫So in the previous sections we made heavy use of inheritance through our actors.

2
00:00:10,860 --> 00:00:11,940
‫Now what is inheritance?

3
00:00:11,940 --> 00:00:18,960
‫Inheritance is when a child class automatically has all of the functionality of its parent.

4
00:00:18,990 --> 00:00:23,060
‫We say essentially that the child is a parent.

5
00:00:23,070 --> 00:00:25,560
‫So where did we use that previously?

6
00:00:25,560 --> 00:00:33,420
‫Well, for example, in our moving platforms, we had an actor that was a moving platform actor, and

7
00:00:33,420 --> 00:00:39,240
‫then we had different children of that actor that gave us the spinning, rotating platforms that up

8
00:00:39,240 --> 00:00:41,730
‫and down platforms, the side to side pushers.

9
00:00:41,730 --> 00:00:43,020
‫Those were all children.

10
00:00:43,020 --> 00:00:45,060
‫They all were movers.

11
00:00:45,060 --> 00:00:49,260
‫You could say the moving pusher is a mover.

12
00:00:49,260 --> 00:00:53,760
‫We could say that the rotating baton is a mover.

13
00:00:53,940 --> 00:00:59,880
‫So there is an alternative to using inheritance, which is a bit all or nothing.

14
00:00:59,880 --> 00:01:04,770
‫And this is composition which is a bit more pick and choose.

15
00:01:04,770 --> 00:01:11,100
‫You get to say that Class A has an instance of Class B, it can choose to use this functionality, but

16
00:01:11,100 --> 00:01:12,180
‫it doesn't have to.

17
00:01:12,180 --> 00:01:17,460
‫It's not tied in to being class B, it can just choose to take the bits it wants.

18
00:01:17,460 --> 00:01:26,610
‫So we say that Class A has a class B, so let's have a look at some examples of inheritance versus composition

19
00:01:26,610 --> 00:01:27,990
‫using something quite natural.

20
00:01:27,990 --> 00:01:35,160
‫I think talking about inheritance in the animal kingdom, we might have a class an actor say that is

21
00:01:35,160 --> 00:01:40,380
‫an animal actor and it has a method on it, which is to live its lives.

22
00:01:40,380 --> 00:01:47,040
‫When you call that method now, it makes sense to have things like a mammal that inherits from this

23
00:01:47,040 --> 00:01:48,420
‫because it also needs to live.

24
00:01:48,420 --> 00:01:53,280
‫So it needs all of that functionality that the animal has, but it adds some more functionality, such

25
00:01:53,280 --> 00:01:54,510
‫as making milk.

26
00:01:55,260 --> 00:02:01,080
‫We might also have a reptile that has the functionality, the added functionality of laying eggs.

27
00:02:01,080 --> 00:02:06,660
‫Now, it's fair to say that a mammal is an animal and a reptile is an animal as well.

28
00:02:07,050 --> 00:02:11,100
‫Now we could go onwards and say, okay, we've got a cow.

29
00:02:11,100 --> 00:02:12,960
‫Well, a cow clearly is a mammal.

30
00:02:12,960 --> 00:02:13,980
‫It needs to make milk.

31
00:02:13,980 --> 00:02:18,450
‫And by extension, it also needs to live because it's also an animal.

32
00:02:18,720 --> 00:02:23,040
‫A snake is a reptile and it is an animal, but it is not a mammal.

33
00:02:23,040 --> 00:02:24,600
‫So that makes sense, too.

34
00:02:24,600 --> 00:02:26,520
‫But what about a platypus?

35
00:02:26,670 --> 00:02:31,830
‫A platypus suddenly needs to both be a mammal and a reptile because it needs the functionality of laying

36
00:02:31,830 --> 00:02:35,490
‫eggs and it needs the functionality of making milk and it needs to be an animal.

37
00:02:35,490 --> 00:02:38,250
‫So it gets a little bit confusing here with inheritance.

38
00:02:38,250 --> 00:02:43,650
‫And inheritance can put us in a straightjacket and make these situations really hard to resolve, which

39
00:02:43,650 --> 00:02:46,380
‫is why composition is what comes to the rescue.

40
00:02:46,410 --> 00:02:48,420
‫Here is the has a relationship.

41
00:02:48,420 --> 00:02:54,870
‫So instead of trying to build this taxonomy of classes with animals and mammals, etc., what we do

42
00:02:54,870 --> 00:03:00,390
‫instead is we say we've got a milk making component and that component has the functionality of making

43
00:03:00,390 --> 00:03:01,020
‫milk.

44
00:03:01,020 --> 00:03:06,420
‫So the cow would have one of these milk making components and it gets its functionality.

45
00:03:06,420 --> 00:03:13,350
‫From there, the snake can have its egg laying component and it gets its egg laying functionality from

46
00:03:13,350 --> 00:03:13,830
‫there.

47
00:03:13,860 --> 00:03:16,470
‫And the platypus can have both components.

48
00:03:16,470 --> 00:03:18,480
‫It doesn't have to pick and choose one component.

49
00:03:18,480 --> 00:03:23,340
‫It can have all of them, and it gets to share the functionality just fine.

50
00:03:23,340 --> 00:03:30,000
‫So let's take the example that we're going to be using here, where we've got a mover, we've got something

51
00:03:30,000 --> 00:03:30,780
‫that wants to move.

52
00:03:30,790 --> 00:03:35,970
‫We might have started setting it up as an inheritance and is a relationship.

53
00:03:35,970 --> 00:03:42,210
‫So we've got a mover that inherits from this static mesh actor, kind of like we've done in previous

54
00:03:42,210 --> 00:03:47,760
‫sections, and then from the mover, we might inherit a moving door or a moving wall.

55
00:03:47,850 --> 00:03:52,200
‫But what happens if we wanted to do a moving light?

56
00:03:52,350 --> 00:03:54,330
‫How could that be done?

57
00:03:54,330 --> 00:03:59,400
‫We can't inherit from mover because that would make it a static mesh actor and we want to use a light

58
00:03:59,400 --> 00:04:01,980
‫actor instead as the parent for moving light.

59
00:04:02,100 --> 00:04:04,560
‫How could we resolve this problem?

60
00:04:04,560 --> 00:04:10,710
‫Using has a relationships instead pause a video, have a think about it, get out a piece of paper and

61
00:04:10,710 --> 00:04:12,600
‫jot down some of your thoughts.

62
00:04:16,250 --> 00:04:16,700
‫Okay.

63
00:04:16,700 --> 00:04:17,680
‫Welcome back.

64
00:04:17,690 --> 00:04:23,390
‫So this is the way I would resolve the problem so we could have our moving door and moving well.

65
00:04:23,390 --> 00:04:24,530
‫Both inherit from static.

66
00:04:24,770 --> 00:04:26,060
‫Actor That's fine.

67
00:04:26,150 --> 00:04:28,640
‫And the moving light can inherit from the light.

68
00:04:28,640 --> 00:04:33,620
‫Actor But now the difference is that we include this has a relationship in here as well.

69
00:04:33,620 --> 00:04:42,020
‫So the moving door, the moving wall and the moving light all can have a mover component on the actor.

70
00:04:42,020 --> 00:04:45,470
‫And this is where actor components come in really handy.

71
00:04:45,470 --> 00:04:49,730
‫And we're going to put this into practice in the next lecture, so I'll see you there.

