14.类资源

以文件形式存储的类被视为 resources。必须从磁盘加载它们,才能在其他类中访问它们。这可以使用 load 或 preload 函数来完成(见下面)。加载类资源的实例化是通过调用类对象上的 new函数来完成的:

# Load the class resource when calling load().
var my_class = load("myclass.gd") # Preload the class only once at compile time. const MyClass = preload("myclass.gd") func _init(): var a = MyClass.new() a.some_function() 

导出

类成员可以导出。这意味着它们的值与它们所连接的资源一起被保存(例如 scene)。它们还可以在属性编辑器中进行编辑。导出是通过使用“导出”关键字:

extends Button

export var number = 5 # Value will be saved and visible in the property editor. 

导出的变量必须初始化为常量表达式,或者以导出关键字参数的形式具有导出提示(参见下面)。

导出成员变量的基本好处之一是使它们在编辑器中可见并可编辑。通过这种方式,艺术家和游戏设计师可以修改影响程序运行的值。为此,提供了一种特殊的导出语法。

# If the exported value assigns a constant or constant expression,
# the type will be inferred and used in the editor.

export var number = 5 # Export can take a basic data type as an argument, which will be # used in the editor. export(int) var number # Export can also take a resource type to use as a hint. export(Texture) var character_face export(PackedScene) var scene_file # Integers and strings hint enumerated values. # Editor will enumerate as 0, 1 and 2. export(int, "Warrior", "Magician", "Thief") var character_class # Editor will enumerate with string names. export(String, "Rebecca", "Mary", "Leah") var character_name # Named Enum Values # Editor will enumerate as THING_1, THING_2, ANOTHER_THING. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1} export (NamedEnum) var x # Strings as Paths # String is a path to a file. export(String, FILE) var f # String is a path to a directory. export(String, DIR) var f # String is a path to a file, custom filter provided as hint. export(String, FILE, "*.txt") var f # Using paths in the global filesystem is also possible, # but only in tool scripts (see further below). # String is a path to a PNG file in the global filesystem. export(String, FILE, GLOBAL, "*.png") var tool_image # String is a path to a directory in the global filesystem. export(String, DIR, GLOBAL) var tool_dir # The MULTILINE setting tells the editor to show a large input # field for editing over multiple lines. export(String, MULTILINE) var text # Limiting editor input ranges # Allow integer values from 0 to 20. export(int, 20) var i # Allow integer values from -10 to 20. export(int, -10, 20) var j # Allow floats from -10 to 20, with a step of 0.2. export(float, -10, 20, 0.2) var k # Allow values y = exp(x) where y varies between 100 and 1000 # while snapping to steps of 20. The editor will present a # slider for easily editing the value. export(float, EXP, 100, 1000, 20) var l # Floats with Easing Hint # Display a visual representation of the ease() function # when editing. export(float, EASE) var transition_speed # Colors # Color given as Red-Green-Blue value export(Color, RGB) var col # Color is RGB. # Color given as Red-Green-Blue-Alpha value export(Color, RGBA) var col # Color is RGBA. # Another node in the scene can be exported, too. export(NodePath) var node 

必须注意的是,即使脚本在编辑器中没有运行,导出的属性仍然是可编辑的(参见下面的“工具”)。

猜你喜欢

转载自www.cnblogs.com/empist/p/10199253.html