-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override variables from extended class #19113
Comments
I found out you could use
extends KinematicBody2D
var speed = 0
var health = 3
_init(_speed = 0, _health = 3).():
speed =_speed
health = _health
extends 'res://Entity.gd'
_init().(5, 5):
pass Which seems better, but I still think setting variables above would look nicer. Maybe with an |
I thought I commented here already, but it seems it did not register... Let me try again.
This is not hacky, it's the way OOP works in pretty much every language. However, I believe In my lost comment I said that you can use The thing is, you shouldn't expect this to work: extends 'res://Entity.gd'
speed = 5
health = 5 So adding an I think such shorthand can be quite cryptic, because the difference between declaration and parent override is just a |
It more felt hacky because I thought there should be a better way to change inherited variables, and using
Those were my concerns as well, and there aren't really any arguments against them. Setting the values in |
Say I have a script,
Entity.gd
. It looks like:Now I want to extend that script into
Enemy.gd
. I want to change its health and speed, in order to do that I have to set them in the_ready
function like so:This seems like a somewhat hacky way to set variables in your base class, having to set your variables in
_ready
and then calling the parent's function if necessary.I am proposing a small change to allow setting variables in a similar way to the base class, just using the
onready
keyword. The documentation says (referring to storing references to nodes):So
onready
is already used for a similar purpose in its current state. This suggestion would allowEnemy.gd
to be written like:It would let scripts look more consistent when extending base classes, if you wanted to change one of the base class's variables you could do it in the same area that it was done in both scripts.
_ready
will be called just like in the base script.Thoughts?
The text was updated successfully, but these errors were encountered: