- Edited
Say I have a script which contains a class with a value, which in turn contains another class, then yet another, each extending the last like so:
extends Node
class Item:
var name : String
func _init(initial_value):
name = initial_value
class Weapon extends Item:
class Sword extends Weapon:
...
I want to create an instance of the Sword class, which supposedly inherits both the Item and and Weapon classes, so I would expect to be able to give it a name since that's a property of Item:
var cool_sword = Item.Weapon.Sword.new("Cool Sword")
print(cool_sword.name)
But this doesn't work because the class Sword doesn't have an init, so I tried doing it like this:
var cool_sword = Item.new("Cool Sword").Weapon.Sword.new()
print(cool_sword.name)
That obviously didn't work either, as the name value doesn't seem to exist in the Sword class so I can't call it.
I just want to create a parameter which is inherited between classes and can be set once the instance is created. Obviously this isn't the right way to do that - can anyone explain why and maybe tell me how to properly do what I'm trying to do?
I understand that doing this all in one script is definitely not ideal for real-world application - But just go along with me here. I'm only prototyping something and I want to keep it confined to one script, but if that's the biggest problem please do tell me.