0 votes

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?
Godot version 3.4.1
in Engine by (24 points)

1 Answer

+1 vote

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)
by (710 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 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.