Doubt regarding inheritance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Scavex

Let’s say I have a parent script with the following variables :

extends KinematicBody2D

var speed
var position
var health

then let’s assume I inherit this script. So now I have a new scene having a child script like this :

extends "res://parent.gd" #The script above

func _ready():
     speed = 100
     position = Vector2(100,100)
     health = 100
     .... some code using the above variables !

Now let’s say that I further inherit this script. So now I have a grandchild script :

extends "res://child.gd" #The script above

func _ready():
     speed = 500
     position = Vector2(300,300)
     health = 10

What I want to know is that whether something like this will work or not ? And if yes then I want to know if the code in the child script will behave differently as the grandchild script is making changes to variables and is inheriting from child script.

:bust_in_silhouette: Reply From: Wakatta

Yes it will work as you’ve suspected.

In fact that is how Godot’s node system is structured

And just as in real life each child instance will act accordingly to their own variables.

If you want them to act the same way use functions and groups

Unfortunately I am experiencing some issue with it. I have a scene with this script :

extends "res://Scripts/Character.gd"
onready var rotater = $Rotater

onready var Bullet = preload("res://Scenes/EnemyBullet.tscn")

func _ready():
	rotate_speed = 100
	spawn_point_count = 10
	radius = 100
	shoot_wait_time = 1
	
      ......code......
	
func _process(delta):
      ......code......

func shoot_bullets():
      ......code......

and then I am further inheriting from it like this for another scene :

extends "res://Scripts/Enemy.gd" 

func _ready():
	rotate_speed = 1000 
	shoot_wait_time = 0.1
	radius = 50
	spawn_point_count = 5

The changes I made to the variables should work. And strangely the rotate_speed is the only variable that is getting changed. The rest are showing no change. What I mean is that when I instance these two scenes with different scripts in the main scene, they are still acting in same way apart from their rotation.

Scavex | 2021-04-23 13:46

Yeah so what actually happens is your ready func in the character gets called then the one in the enemy after so if you printed those values you would see the change.

Can’t exactly remember but I believe _process works the same way so if your character _process does something so too will your enemy

Wakatta | 2021-04-23 14:31

Ok that’s surely helpful but then why is rotate_speed getting changed to 1000 in main scene. The child scene has 100 rotate speed and it’s grandchild scene has 1000.

Scavex | 2021-04-23 14:49

That my friend i won’t be able to tell you but it’s got something to do with your coding as what you’re expecting to happen should. Try using break points and stepping through your code to see what’s taking place

The work flow should be for enemy.gd
Call Character _ready set vars Call enemy _ready set vars

Wakatta | 2021-04-23 22:56