0 votes

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.

Godot version godot 3.3
in Engine by (414 points)

1 Answer

+1 vote

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

by (6,876 points)

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


func _process(delta):

func shoot_bullets():

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.

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

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.