Q & A: Understand the inheritance mechanism in GDScript through a scenario (Godot game engine is new)

Many Mengxin have not been exposed to object-oriented programming before learning Godot Engine and GDScript, so I was confused when I saw the concept of "inheritance" when I read "The First Bullet of Godot Game Development" , what scenarios use inheritance? ? Moreover, some friends have reported that they have read the textbooks and some articles on the Internet, but they tend to be more and more confused.
Pharaoh also came from Mengxin all the way, so I understand this kind of confusion. The problem is not textbooks and net texts, but because Mengxin lacks the practice of programming, it causes that when reading some abstract narratives, there is not enough in my mind. The situation evokes resonance. There is only one fundamental solution to this type of confusion, that is, to "knock it out" , practice more, touch more nails, and accumulate more. In the end, what can make you suddenly open is not a certain answer, but precisely the question you accumulated.

Today Pharaoh also uses Godot's GDScript to provide a scenario of using the inheritance mechanism.

scene

This is a new story about Godet game Xiaomeng. . .
Insert picture description here

If there is no inheritance

The first edition of the game plan

The game planner said to Mengxin: "There are three kinds of little monsters to be realized in the game, little monster A, little monster B and little monster C".

Little Monster A is from the healing department, and its conceptual design is as follows:

member Explanation
level grade
hp physical strength
loyalty loyalty
cure() Call it to release healing skills

The mob B is an attack system. Its conceptual design is as follows:

member Explanation
level grade
hp physical strength
loyalty loyalty
attack() Call it to release attack skills

Little Monster C is from the Department of Magic, and its conceptual design is as follows:

member Explanation
level grade
hp physical strength
loyalty loyalty
magic() Call it to release magic skills

It didn't seem difficult to achieve, so Mengxin started construction:

#MonsterA.gd
extends Node
var level = 1
var hp = 5
var loyalty = 10
func cure():
	"""具体逻辑略"""
	print("MonsterA的治愈技能")

Then Ctrl + C/ Ctrl + Vremove and func cure()change to func attack()implement logic ...

#MonsterB.gd
extends Node
var level = 1
var hp = 5
var loyalty = 10
func attack():
	"""具体逻辑略"""
	print("MonsterB的攻击技能")

Continue Ctrl + C/ Ctrl + Vdelete and func attack()change to func magic()implementation logic ...

#MonsterC.gd
extends Node
var level = 1
var hp = 5
var loyalty = 10
func magic():
	"""具体逻辑略"""
	print("MonsterC的魔法技能")

Insert picture description here

Yay! It looks pretty good!

Game Planning Case Second Edition

Before it was too happy, the plan brought the second edition of the plan.

"We hope to add more kinds of mobs, um ... about 100 kinds. They have a similar relationship with mobs A, B, and C. They all have the same attributes and have different methods," the planner said.

"Ah ... okay" Mengxin's partner frowned and started working again. . .

The process is a bit ... This time is a bit long. During the period, my hands dazzled and I made a lot of bugs ...

Insert picture description here

Game Planning Case Third Edition

Just going to call it a day, planning again: "This should remove all the mobs in the loyaltyproperty, adding a common" roar "skills."

Mengxin: "...... & *% ...... & ¥% …… #)) ——"

Insert picture description here

Insert picture description here

When Mengxin learned inheritance

The first edition of the game plan

#MonsterBase.gd
extends Node
class_name MonsterBase #从Godot3.1开始可以给类起名字了
var level = 1
var hp = 5
var loyalty = 10
#MonsterA.gd
extends MonsterBase #继承MonsterBase
func cure():
	"""具体逻辑略"""
	print("MonsterA的治愈技能")
#MonsterB.gd
extends MonsterBase #继承MonsterBase
func attack():
	"""具体逻辑略"""
	print("MonsterB的攻击技能")
#MonsterC.gd
extends MonsterBase #继承MonsterBase
func magic():
	"""具体逻辑略"""
	print("MonsterC的魔法技能")

Define a more base class and write fewer lines of repeated code.

Game Planning Case Second Edition

The process is omitted. . . Without repeated code, the probability of error is smaller, but I do n’t see anything that is too magical.

Insert picture description here

Game Planning Case Third Edition

#MonsterBase.gd
extends Node
class_name MonsterBase #从Godot3.1开始可以给类起名字了
var level = 1
var hp = 5
#var loyalty = 10
func roar():
	print("路见不平一声吼,该出手时就出手")

what? ! ! H (&(……% &% …………, so simple to achieve? !!!

Insert picture description here

The problem that once allowed Mengxin and planning to break is no longer a problem, and since then they have lived happily together ...

Insert picture description here

summary

To be honest, from the comparison of the above two situations, it is not difficult to find that the inheritance mechanism is to put the common characteristics of derived classes into (abstract to) the base class. When there are a large number of classes with common characteristics, and the common characteristics may change When using inheritance, it will greatly improve the maintainability of the code.

Published 401 original articles · Like 1608 · Visit 250,000+

Guess you like

Origin blog.csdn.net/hello_tute/article/details/105426355