为什么全局位置打印0,0

Why does global position print 0,0

我正在用 Godot 制作一个挖矿游戏。我有矿脉,以后可以分块加载。这些采矿矿脉会在其中生成方块。这是相关代码:

extends Node2D

onready var selfnoise = OpenSimplexNoise.new()
onready var chunksettings = $"/root/ChunkSettings"
onready var position2d = $Position2D
var emerald_block = preload("res://Scenes/Blocks/Emerald block.tscn")
var stone_block = preload("res://Scenes/Blocks/Stone_Block.tscn")
var offset = 16
var rng = RandomNumberGenerator.new()

func _ready():
    selfnoise.seed = chunksettings.noise.seed
    selfnoise.octaves = chunksettings.noise.octaves
    selfnoise.persistence = chunksettings.noise.persistence
    selfnoise.lacunarity = chunksettings.noise.lacunarity
    gen_vein()
    
func gen_vein():
    for x in 3:
        for y in 3:
            var inst_stone = stone_block.instance()
            var inst_emerald = emerald_block.instance()
            var inst = inst_stone
            var rand = round(abs(chunksettings.noise.get_noise_2d(self.global_position.x + (x * offset),self.global_position.y + (y * offset)))*3)
            print(rand)
            if rand == 1:
                inst = inst_emerald
            elif rand == 0:
                inst = inst_stone
            print(self.global_position)
            add_child(inst)
            inst.position.x = offset * x
            inst.position.y = offset * y

该场景在主场景中实例化。这是主场景的代码:

extends Node2D


#offset between chunks
var offset = 48

#preloading chunk settings
export var load_x = 20
export var load_y = 20

#all the veins to spawn
var stone_vein = preload("res://Scenes/veins/Stone vein.tscn")
var emerald_vein = preload("res://Scenes/veins/Emerald vein.tscn")

func _ready():
    for x in load_x:
        
        for y in load_y:
            var emerald_vein_inst = emerald_vein.instance()
            add_child(emerald_vein_inst)
            emerald_vein_inst.position2d.global_position.x = x * offset
            emerald_vein_inst.position2d.global_position.y = y * offset
            emerald_vein_inst.global_position.x = x * offset
            emerald_vein_inst.global_position.y = y * offset

func getrandomnumber():
    pass

我遇到的问题是祖母绿矿脉位于全局位置 0,0,但我在生成脚本中对其进行了更改。我需要获得全局位置才能在种子中使用它,因此我可以获得 1 或 0 的值来获得石头或翡翠块。因为它是 0,0 每个块加载如下: The image

var rand = round(abs(chunksettings.noise.get_noise_2d(self.global_position.x + (x * offset),self.global_position.y + (y * offset)))*3)

我试过使用: 一个位置2d来计算chunk的位置。

我更改了一部分代码并完成了这项工作。当它产生节点时,它运行节点脚本,然后 然后 改变它的位置。所以,我改变的是它产生的顺序。

func _ready():
    for x in load_x:
        
        for y in load_y:
            var emerald_vein_inst = emerald_vein.instance()
            emerald_vein_inst.global_position.x = x * offset
            emerald_vein_inst.global_position.y = y * offset
            add_child(emerald_vein_inst)

而不是:

func _ready():
    for x in load_x:
        
        for y in load_y:
            var emerald_vein_inst = emerald_vein.instance()
            add_child(emerald_vein_inst)
            emerald_vein_inst.position2d.global_position.x = x * offset
            emerald_vein_inst.position2d.global_position.y = y * offset
            emerald_vein_inst.global_position.x = x * offset
            emerald_vein_inst.global_position.y = y * offset

Reddit 上有人帮助了我 (details)。