Is instantiating new object using class_name differs from preload() and load() functions?

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

I register class name with class_name keyword

extends Object
class_name MyClass

And than inside other script I instantiate it like that

var obj = MyClass.new()

Is it differs from that methods?

var MyClass = preload("MyClass.gd")
var obj = MyClass.new()

and

var MyClass = load("MyClass.gd")
var obj = MyClass.new()

If I instantiate object using registered class_name, will be script loaded during runtime or during compilation time?

:bust_in_silhouette: Reply From: klaas

Hi,
you can read about it here:

https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_continued.html?highlight=class_name#register-scripts-as-classes

  • Named scripts are registered globaly on startup.
  • Preloaded scripts are loaded into a var or const in a class on compile time. They are not globaly accessable.
  • Loaded scripts are loaded on runtime into a class variable. They are not globaly accessable.

So, will it be loaded before runtime?

Robotex | 2020-08-20 05:31