1
0
Fork 0
This repository has been archived on 2022-10-04. You can view files and clone it, but cannot push or open issues or pull requests.
godot-thunderk-basics/scene_tool.gd

68 lines
1.7 KiB
GDScript

tool
static func empty_node(node: Node):
""" Remove all children from a container node
"""
for child in node.get_children():
child.queue_free()
static func set_parent(child: Node, parent: Node):
""" Set a node's parent
"""
var cur_parent = child.get_parent()
if cur_parent:
if cur_parent == parent:
return
else:
cur_parent.remove_child(child)
parent.add_child(child)
static func set_owner(node: Node, owner: Node, force_inherited=false):
""" Recursively set a owner
"""
if node != owner:
node.set_owner(owner)
if force_inherited or not node.filename:
for child in node.get_children():
set_owner(child, owner)
static func set_layer(node: Spatial, layer: int):
""" Recursively set a display layer
"""
if node is VisualInstance:
node.layers = layer
else:
for child in node.get_children():
set_layer(child, layer)
static func pack(node: Node, update_owner=true) -> PackedScene:
""" Pack a node as a PackedScene
"""
if update_owner:
for child in node.get_children():
set_owner(child, node)
var scene = PackedScene.new()
if scene.pack(node) == OK:
return scene
else:
return null
static func pack_as_file(node: Node, filename: String, update_owner=true):
""" Pack a node in a file
"""
var scene = pack(node, update_owner)
if scene:
if ResourceSaver.save(filename, scene) != OK:
push_error("Failed to save packed scene: %s" % filename)
return FAILED
else:
push_error("Failed to pack packed scene: %s" % filename)
return FAILED
return OK
static func distance3d(obj1: Spatial, obj2: Spatial) -> float:
""" Get the distance between two 3d nodes
"""
return obj1.global_transform.origin.distance_to(obj2.global_transform.origin)