0 votes

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 bulletspeed for this child doens't seems to change the parent bulletspeed (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?

Godot version 3.4.4 stable
in Engine by (12 points)

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

Please log in or register to answer this question.

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.