0 votes

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

enum Card_values {ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGTH, NINE, TEN, ELEVEN, TWELVE,  SKIP_BO}


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()

    testcard.set_name("testcard")
    testcard.myinit(new_value)
    add_child(testcard)
    return testcard
    #testcard = Card.new(Card.Card_values.SIX)

# 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 ?

Godot version 3.4
in Engine by (12 points)

2 Answers

0 votes

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 :)

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.

by (6,461 points)
0 votes

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.

by (21 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.