I declare my variable in my parent class and set it in the ready function of the child class, but I get an error variable not declared in current scope. what am I doing wrong?
how to declare a variable in parent class and set it in child class?
Can you share a pared down excerpt of your code? It's hard to diagnose without seeing something.
You might have better luck declaring the variable in the child class and just have the parent read/write it via the child. Unless there's a reason why it must be defined in the parent.
- Edited
extends Node2D
class_name Creature
var speed
#######################
extends Creature
func _ready():
speed = 1
@bitshift-r said: Can you share a pared down excerpt of your code? It's hard to diagnose without seeing something.
You might have better luck declaring the variable in the child class and just have the parent read/write it via the child. Unless there's a reason why it must be defined in the parent.
I want to define it in the parent because I plan on making many different different children that inherit the parent so I figure it would make sense to define all of their common variables in the parent to cut down on lines of code
I just tested your code and was not able to reproduce your issue. Is this excerpt a simplified version of your actual code?
Maybe try using setters and getters?
Maybe if you shared your whole code? Their might be something wise that's causing issues here.
So this issue actually came up for me recently. I made a new inherited class, then decided to rename the variable that was being inherited. Even though I renamed the variable to the exact same thing in both scripts, doing so caused the exact same error above.
Restarting the editor fixed the issue, so I guess it's just a weird bug with Godot? It's odd.
I had something similar with exported variables. I found that get/set would work.
set("speed", 1)