Topic was automatically imported from the old Question2Answer platform.
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.
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