I'm trying to figure out inheritance in Godot and I am probably going about it the wrong way. I have a parent class (Plant.gd) which contains all the main functions that I want a child class (Turnip.gd) to inherit. But I want to specify the attributes of Turnip (ie, crop age, grow times, etc) in Turnip that would use those functions.
In plant I have a default dataset specified as "var crop_data" which I want to be overwritten with Turnip's "var crop_data". But I get the error that "the member crop_data already exists in a parent class"
Here's all the code I have so far:
Parent Class (Plant.gd)
extends Area2D
class_name Plant
var farm_map
var crop_vector: Vector2
var crop_planted = false
var full_grown = false
signal needs_water
signal full_grown
var crop_data = {"pk": 0,
"plant_name": "plant",
"grow_times": [5, 10, 15, 15],
"crop_age": 0}
# Called when the node enters the scene tree for the first time.
func _ready():
farm_map = get_parent()
func initialize(clicked_cell):
crop_vector = Vector2(clicked_cell)
crop_planted = true
$PlantSprite.set_frame(crop_data.crop_age)
func get_position():
return crop_vector
func water_plot():
print("Watering Plot")
farm_map.set_cellv(crop_vector, Constants.WET_DIRT)
#Select grow time from array using crop_age as index
$PlantTimer.start(crop_data.grow_times[crop_data.crop_age])
func _on_PlantTimer_timeout():
emit_signal("needs_water")
farm_map.set_cellv(crop_vector, Constants.DIRT_DRY)
if crop_data.crop_age < crop_data.grow_times.size() - 1:
crop_data.crop_age += 1
$PlantSprite.set_frame(crop_data.crop_age)
if crop_data.crop_age == crop_data.grow_times.size() - 1:
emit_signal("full_grown")
full_grown = true
func is_full_grown():
return full_grown
func harvest():
.queue_free()
Child Class (Turnip.gd)
extends "Plant.gd"
class_name Turnip
var crop_data = {"pk": 1,
"plant_name": "Turnip",
"grow_times": [5, 10, 15, 15],
"crop_age": 0}
# Called when the node enters the scene tree for the first time.
func _ready():
pass`
I had actually wanted to initialize the crop_data by selecting a key from a json file (ie, user selects key "turnip" or "potato" to decide which crop to plant) like so:
JSON file
{
"turnip": {
"plant_name": "Turnip",
"grow_times": [5, 10, 15, 15],
"crop_age": 0
},
"potato": {
"plant_name": "Potato",
"grow_times": [5, 10, 15, 15],
"crop_age": 0
}
}
Pseudocode (assume JSON file was loaded and parsed)
var crops = json_file
var crop_key = "turnip"
var crop_data = crops[crop_key]
Initialize method:
func initialize(clicked_cell, crop_data):
crop_vector = Vector2(clicked_cell)
crop_planted = true
$PlantSprite.set_frame(crop_data.crop_age)
With this I was able to select the right values that went with the key (ie, turnip). But for some reason when I used this method it would work to plant the correct initial seeds but after the first growth phase the crops would all grow to be random growth stages. ie, if I planted four seeds they would all be at crop_age 0 but once the soil dried, the first one would be crop_age 1, the next would be crop_age 2, and so forth. That doesn't happen when I use a script with hard coded crop_data, so I guess that's why I am going this route.