Search code examples
godotgdscript

Why does this sprite spawn at 0,0 when I change it when it spawns?


Background

I'm making a mining game in Godot. I'm doing procedural generation and spawning chunks based on player position.

Problem

The issue I have is that the emerald vein is at global position (0,0) but I change it in the spawning script. I need to get the global position to use it in the seed, so I can get a value of 1 or 0 to get a stone or emerald block. Because it is 0,0 each chunk loads like this: The image

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

I have mining veins to load as chunks later on. These mining veins spawn blocks within them.

This scene is instanced in the main scene.

Code

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

Here is the code for the mining veins:

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

Solution

  • I changed one part of my code and made this work. When it spawns the node, it runs the node script and then changes its position. So, what I've changed was the order of when it spawns.

    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)
    

    Instead of:

    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
    

    Someone on Reddit helped me (details).