Godot Version
4.1.2
Question
I’m trying to create some scripts to learn OOP in Godot and I keep getting this error “Invalid call. Nonexistent function ‘init’ in base ‘CharacterBody2D’.”
Here is my main parent class:
extends CharacterBody2D
class_name Char
var _name: String
var _hp: int
var _age: int
var isDead: bool
func _init(string_name: String, hp: int, age: int):
set_charname(string_name)
set_hp(hp)
set_age(age)
func get_charname():
return _name
func set_charname(string_name: String):
_name = string_name
func get_hp():
return _hp
func set_hp(hp: int):
if hp <= 0:
isDead = true
hp = 0
else:
isDead = false
_hp = hp
func get_age():
return _age
func set_age(age: int):
assert(!(age < 18), "Only 18+ age allowed")
_age = age
Here is my NPC class, that inherits from Char:
extends Char
class_name NPC
var _friendly: bool
var _faction: String
func init(npc_name: String, hp: int, age: int, friendly: bool, faction: String = ""):
_init(npc_name, hp, age, friendly, faction)
func _init(npc_name: String, hp: int, age: int, friendly: bool, faction: String = ""):
super(npc_name, hp, age)
set_friendly(friendly)
set_faction(faction)
func _ready():
print(get_faction())
func get_friendly():
return _friendly
func set_friendly(friendly: bool):
_friendly = friendly
func get_faction():
return _faction
func set_faction(faction: String):
_faction = faction
And then I try to call it inside a Scene:
extends Node2D
# Called when the node enters the scene tree for the first time.
func _ready():
init_npc()
#pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func init_npc():
#Roberto
var roberto = load("res://Scenes/npc.tscn").instantiate()
roberto.position = Vector2(400, 200)
roberto.init("Roberto", 200, 20, true, "Test")
add_child(roberto)
Then it throws that error “Invalid call. Nonexistent function ‘init’ in base ‘CharacterBody2D’.” because of this line:
roberto.init("Roberto", 200, 20, true, "Test")
But as you can se in the NPC.gd, init function definitively exists
I don’t understand what is wrong with this code, the parent node from
“res://Scenes/npc.tscn” is from type NPC, so not really sure what’s happening. If anyone can shed a light I would be very thankful.