Change inherited class variables (parent) when child is created

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

I have a base class for Bullets, in this class there is a variable for bullet_speed.

extends RigidBody2D
class_name Bullet

var BULLET_SPEED:float

func set_bullet_speed(speed:float):
	BULLET_SPEED = speed

func _do_movement_and_stuff():
    ...

Then I have the childs of the base class

extends Bullet
class_name EnergyBullet
 
func _ready() -> void:
	... SET BULLET SPEED HERE

The thing is, everything I tried for setting up the bullet_speed for this child doens’t seems to change the parent bullet_speed (where the movement is handled).

What I tried (replace “… SET BULLET SPEED HERE”) on the child’s_ready func.

Calling the parent set_bullet_speed method:

set_bullet_speed(500)
.set_bullet_speed(500)

Changing the variable direcly:

BULLET_SPEED = 500
 set("BULLET_SPEED", 500)
	

call defered:

call_defered("set_bullet_speed", 500)

and yield:

yield(get_tree().root, "ready")
... above code
(I don't know if I did the yield right tho)

The only thing that worked was setting a _init(speed:float): BULLET_SPEED = speed on parent and setting the bullet_speed there, then calling the _init().(500): pass, on the child.

I came from java and this is not a problem there as you inherits everything from the parent as being the same thing on the same instance.
Is the above situation the only way to access and change a parent variable? or i’m doing something wrong?

It should work. There must be some code interrupting it.
is there anything else in baseclass or inheritors ready() function ?
also, show the _do_movement_and_stuff() code and how it is called

Inces | 2022-07-30 20:36