is it wrong to use a parameterized _init ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lpoe

Hi Fellas,

sorry is this is a dumb question.

Im completely new to godot.
For starters I wanted to create a simple Card game.

I have a Card class and a Stack class which contains an array of cards.
As I want to use both classes / scripts for KinematicBode2D objects, I let them inheret from this class. For the Card class ive modified the _init() function to accept one parameter which is the value of the Card

extends KinematicBody2D

class_name Card


export(Card_values) var value = Card_values.ONE

# TODO method should only accept card enum
func _init(new_value):
	value = new_value

func myinit(new_value):
	value = new_value

Ive created a scene for the card containing so I can instance it. The scene consists of a KinematicBody2D whith the card script attached and a Sprite and CollisionShape2D as childs.

When I try to add a new instance of the card scene and change the values of the card via custom function of the card class i get an error which tells me, that the function i try to call would not exists for KinematicBody2D

Nonexistent function 'myinit' in base 'KinematicBody2D'

Also I’ve noticed the following error

_create_instance: Condition "r_error.error != Variant::CallError::CALL_OK" is true. Returned: nullptr

This is the call of the function

func create_card(new_value):
	var card_scene = load("res://src/actors/card.tscn")
	var testcard = card_scene.instance()
	return testcard
	#testcard =

# Called when the node enters the scene tree for the first time.
func _ready():
	var testcard1 = create_card(Card.Card_values.SIX)
    export(Card_values) var value = Card_values.ONE
    #var value = Card_values.ONE
    # TODO method should only accept card enum
    func _init(new_value):
    	value = new_value

Could it be that my error comes from creating a _init fuction which takes values ?
Should I use a custom init function everywhere ?

:bust_in_silhouette: Reply From: Inces

It should totally work. You have created custom function in custom class and it should always be recognized by compiler. The only thing that could go wrong is that You actually didn’t attach script to your card scene :slight_smile:

Init is a bit different than other functions. You can pass all kinds of arguments into new() function of script being created, and these arguments are passed into its init() function. So this is the only way to use parameters inside init(). That won’t work with loading whole packedscenes.

:bust_in_silhouette: Reply From: 5pectre7

The _init function just like _ready is an override of such function from the Object class which you inherit by extending, e.g. KinematicBody2D. You can’t change the signature when overriding though, therefore the error.

You should use custom ‘initialize’ function whenever you want to pass additional parameters, or create a custom class that will not extend object, and will have _init(param1, param2): signature that you want to enforce.