How to use class constructors for scene instances?

Godot Version



I wrote the arkanoid, everything works in general, but I am not satisfied with the logic of how I implemented the strength of the blocks

I created a “Block” class that extends StaticBody2D, I wanted to create a constructor in it, but I ran into a problem: I put blocks in a loop when loading the root scene using scene instances.
How do I call the class constructor in this case…?

Project files - Godot - Google Drive

Just add an _init function to your Block class, it will be run when the scene is instantiated:

This may be true, but I would like to transmit the durability of the block precisely through the constructor, and cannot pass necessary parameters to instantiate().

What should be done in this case?

Okay, in my example, I didn’t understand what I was doing at all, I passed a parameter to instantiate, hoping that it would be a constructor parameter

But my thought and what I want to do, I hope, has become clear

If you want to pass arguments to the constructor, just write a custom function for it and call it on the individual instance. So in your loop, you do:

var instance = block_scene.instanntiate()

And in your block class:

func custom_init(_durability):
    durability = _durability