How can I move one Node3D to another Node3D's child in Godot 4.0? And how can I duplicate a random child of a Node3D

huangapple go评论58阅读模式
英文:

How can I move one Node3D to another Node3D's child in Godot 4.0? And how can I duplicate a random child of a Node3D

问题

I am making some script for a game I'm making. The game's map is a randomly generated series of hallways, and there are different shaped hallways. For example, one hallway is straight, another turns, another turns and has a set of stairs that go down. If there is no way to do anything I mentioned above in the question, is there an alternative?

I haven't really tried anything yet for moving one node to another, because all the tutorials are outdated. For duplicating a random node, I tried something like this:

var parent_node = $Node3D
var random_index = randi() % parent_node.get_child_count()
var random_child = parent_node.get_child(random_index)
var duplicated_child = random_child.duplicate()

It gave me an error saying cannot call duplicate on a null instance.

英文:

I am making some script for a game I'm making. The game's map is a randomly generated series of hallways, and there are different shaped hallways. For example, one hallway is straight, another turns, another turns and has a set of stairs that do down. If there is no way to do anything I mentioned above in the question, is there an alternative?

I haven't really tried anything yet for moving one node to another, because all the tutorials are outdated. For duplicating a random node, I tried something like this:

var parent_node = $Node3D
var random_index = randi() % parent_node.get_child_count()
var random_child = parent_node.get_child(random_index)
var duplicated_child = random_child.duplicate()

It gave me an error saying cannot call duplicate on a null instance.

答案1

得分: 0

I figured out your problem:
您已经解决了您的问题:

You are trying to get the node: $Node3D and call .get_child_count() on it before the scene has finished loading.
您在场景加载完成之前尝试获取节点:$Node3D 并对其调用 .get_child_count()

To fix this, add @onready at the beginning of each of the lines, or put the in the _ready() function.
为了解决这个问题,在每行的开头添加 @onready,或者放在 _ready() 函数中。

Doing this would make sure the code only runs when the scene is loaded and everything is ready.
这样做可以确保代码仅在场景加载完毕并且一切准备就绪时运行。

This is the final code I would suggest:
这是我建议的最终代码:

@onready var parent_node = $Node3D

func _ready():
    var random_index = randi() % parent_node.get_child_count()
    var random_child = parent_node.get_child(random_index)
    var duplicated_child = random_child.duplicate()

You also said that you want to move the node to another parent node.
您还说您想将节点移动到另一个父节点。

I will assume you are trying to move the random node. So first lets add another variable for the new parent node:
我假设您想要移动随机节点。所以首先让我们为新的父节点添加另一个变量:

@onready var new_parent = $Node

Replace $Node with the node you want to be the new parent.
$Node 替换为您想要作为新父节点的节点。

Next we want to add the child to the new parent in the _ready() function:
接下来,我们希望在 _ready() 函数中将子节点添加到新的父节点中:

new_parent.add_child(duplicated_child)

You are now done unless you want to remove the node from the original parent, then you will add this after:
除非您想要从原始父节点中删除节点,否则您已经完成,然后您将在此之后添加以下内容:

parent_node.remove_child(duplicated_child)

This is now the final Code:
这现在是最终的代码:

@onready var parent_node = $Node3D
@onready var new_parent = $Node

func _ready():
    var random_index = randi() % parent_node.get_child_count()
    var random_child = parent_node.get_child(random_index)
    var duplicated_child = random_child.duplicate()
    new_parent.add_child(duplicated_child)
    parent_node.remove_child(duplicated_child)
英文:

I figured out your problem:
You are trying to get the node: $Node3D and call .get_child_count() on it before the scene has finished loading. To fix this, add @onready at the beginning of each of the lines, or put the in the _ready() function. Doing this would make sure the code only runs when the scene is loaded and everything is ready. This is the final code I would suggest:

@onready var parent_node = $Node3D

func _ready():
	var random_index = randi() % parent_node.get_child_count()
	var random_child = parent_node.get_child(random_index)
	var duplicated_child = random_child.duplicate()

You also said that you want to move the node to another parent node. I will assume you are trying to move the random node. So first lets add another variable for the new parent node:

@onready var new_parent = $Node

Replace $Node with the node you want to be the new parent.

Next we want to add the child to the new parent in the _ready() function:

new_parent.add_child(duplicated_child)

You are now done unless you want to remove the node from the original parent, then you will add this after:

parent_node.remove_child(duplicated_child)

This is now the final Code:

@onready var parent_node = $Node3D
@onready var new_parent = $Node

func _ready():
	var random_index = randi() % parent_node.get_child_count()
	var random_child = parent_node.get_child(random_index)
	var duplicated_child = random_child.duplicate()
    new_parent.add_child(duplicated_child)
    parent_node.remove_child(duplicated_child)

huangapple
  • 本文由 发表于 2023年5月11日 06:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定