How to call _init() explicitly for class-name

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

I defined a custom class like so and want to construct it from the same file:

var x = 0.0
var y = 0.0
var z = 0.0

func _init(x_,y_,z_):  
    self.x = x_
    self.y = y_
    self.z = z_

static func dot(a, b):
    return a.x*b.x + a.y*b.y + a.z*b.z


static func length(a):
    return sqrt(dot(a,a))

static func scale(a,s):
    return _init(a.x*s,a.y*s,a.z*s) # doesn't work, how to do this?
:bust_in_silhouette: Reply From: juppi

Hey,

here is an example:

Create a class (class is a script):

extends Node

class_name Person

var first_name: String

func _init(first_name_: String):
	first_name = first_name_

Now create the static function in another script:

static func get_person(first_name: String) -> Person:
	return Person.new(first_name)

This will then print the given name “Bob”:

var person: Person = get_person("Bob")
print(person.first_name)