First of all,I can’t accurately classify my questions, so I will describe the phenomenon and my expected results.the script as attached. if you add these scripts to a empty project, everything is ok.But when you uncomment ClassB.funcB, ClassA will error: The class "ClassB" couldn't be fully loaded (script error or cyclic dependency) I think it is maybe a forward declaration problem.

What can I do to keep it from reporting errors. use ClassB.funcA everything is ok,but i think "load" is not a elegant approach

i need your help please,thanks a lot. BR.

The function call "ClassB.new()" already creates a new object of ClassB. You don't need either of those functions in ClassB, and (in fact) you cannot use the class name of the class in its own file (it gives that error/warning in the 3.2 editor). If you delete both funcA() and funcB() it still works.

Thanks for your replying. But i need either of those functions in ClassB, i want to use Prototype Pattern in ClassB, it means i can use ClassB.clone() to get a new ClassB's object. ClassB.clone is funcA or funcB

I haven't tried it, but you can probably make a separate class (called Factory or whatever) that can clone any object you give it. I know I have used that way in C++ (non-Godot) using templates before, but I'm not 100% how to do that in Godot.

Thanks for your replying. But i think Factory cannot solve the problem, the script as attached .the Godot will error.

Hmm. Can you explain what you are trying to accomplish? Maybe there is another way to do it.

Okay, try this: MyClass (the object you want to create / clone)

extends Node2D
class_name MyClass

export var my_var:int
	
func output():
	print("MyClass = ", my_var)

Factory class (which can take an object and copy it's values and return a clone):

extends Node2D
class_name Factory

func _ready():
	create_class()
	
func create_class():
	var new_obj = MyClass.new()
	new_obj.my_var = 13
	new_obj.output()

I want to renturn a MyClass's object in MyClass's function.

For example, i want to get a new object with the data i need from an old object. i want these two objects are the same type. these two script will error

ClassA.gd

class_name ClassA

func _init():
	var object1 = ClassB.new()
	object1.update_info()
	var object2:ClassB = object1.get_need()

ClassB.gd

class_name ClassB
var dictioary = Dictionary()
func _init():
	dictioary["need"]=[]
	dictioary["not_need"]=[]
	pass

func update_info():
	dictioary["need"].append("data")
	dictioary["not_need"].append("data")
	pass

func get_need() -> ClassB:
	var need = ClassB.new()
	need["need"] = dictioary["need"].duplicate(true)
	return need

That's what I'm saying. I don't believe you can instantiate a new object from itself (at least it doesn't seem that way). However, an external class (Factory in my example) can create a new object and copy the values from the first object. Here is my updated example:

extends Node2D
class_name MyClass

export var my_var:int
	
func output():
	print("My Var = ", my_var)
	
func get_data():
	return { "number": my_var }
	
func set_data(var data:Dictionary):
	my_var = data.number
extends Node2D
class_name Factory

func _ready():
	create_class()
	
func create_class():
	var original_obj = MyClass.new()
	original_obj.my_var = 33
	original_obj.output()
	
	var cloned_obj = MyClass.new()
	cloned_obj.set_data(original_obj.get_data())
	cloned_obj.output()

Hope that helps.

Thank you very much for your answer. The difference between me and you is that I insist that the clone function is more in line with design in MyClass. I don't want to add a Factory Class to each class which I need to do similar operations, and this Factory Class needs to know the details in MyClass, i think it is not good

Yeah, I see what you're saying. Maybe using "load" is your best bet.

Much appreciate for your answer, "load" is a way to solve my problem, and i use it. But it need script's path.Maybe others have other answers

You can get the script name in code, maybe that will help.

func clone():
	var obj = load(get_script().resource_path)
	var new_obj = obj.new()
	new_obj.set_data(get_data())
	return new_obj

Note set_data and get_data are custom function like shown in previous posts.

Actually, looks like you can just do this:

func clone():
	var obj = get_script()
	var new_obj = obj.new()
	new_obj.set_data(get_data())
	return new_obj

Surprised by your methord, i think this is exactly what i want. Really appreciate your answer , hope you have a nice day.

6 months later

@cybereality said: Actually, looks like you can just do this:

func clone():
	var obj = get_script()
	var new_obj = obj.new()
	new_obj.set_data(get_data())
	return new_obj

:o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 Wow. This is an essential function for 3.2 that should be flagged, lit, highlighted, put on a pedestal, carved into marble, trumpeted off the roofs with fanfares, and possibly though not necessarily covered with gold leaf. :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3 :o =) <3

@cybereality said: So, I guess you like it?

Just a bit. Alas, it only works for Node2D, not for arbitrary classes. So we still can't put instance return values into a class' function.

get_script() is a member function of Object. So any class that inherits from Object should have access to that function. I don't think it is limited to Node2D. Maybe try to have your class inherit Object and see if it works.