I see. I will provide more detail.
I am trying as you said place the objects in a grid like position.
Grid it self is dependent on a 2d int array that is generated by this code.
func create_int_grid():
var int_array = []
for i in height:
int_array.append([])
for j in width:
int_array[i].append(randi_range(0,3))
return int_array
This 2d array is then passed to the code I posted previously.
func spawn_grid(int_array:Array):
var default_pos = entity_spawner.position
for i in height:
if i != 0:
entity_spawner.position.y+=1
entity_spawner.position.x = 0
for j in width:
if j != 0:
entity_spawner.position.x += 1
entity_spawner.spawn_entity(int_array[i][j])
entity_spawner.position = default_pos
(I removed the i,j string and chunk_id from spawn_entity function since that concerns a feature I didn’t manage to implement anyway)
spawn_entity function than places individual pixels
func spawn_entity(entity_id: int):
var entity = general_entity_instancer.return_entity(entity_id).instantiate()
chunk_holder.add_child(entity) #spawn do objektu
entity.set_global_position(self.get_global_position()) #globalni pozice
also here is the general_entity_instancer if that helps
extends Node
class_name EntityInstancer
@export var dirt_1: PackedScene
@export var dirt_2: PackedScene
@export var dirt_3: PackedScene
@export var dirt_4: PackedScene
@onready var dirt_array = [dirt_1,dirt_2,dirt_3,dirt_4]
func _ready() -> void:
pass
func return_entity(entity_id: int):
return dirt_array[entity_id]
Then I run this code in top parent node
func _ready() -> void:
array_functions.spawn_grid(array_functions.create_int_grid())
and that creates this grid of pixels
also here is the structure of this chunk spawner scene
I tried to make chunk based system that would load these tiles (grids) as the player would move. But the performance was horrible since the two loops of 2d arrays were running with each spawned grid.
It is true that saving grid int array and just reusing it didn’t cross my mind, that would definitely boost the performance so thank you for that advice :D.
But I was wondering if I could do the same think with the grid of already placed pixels. Somehow preload it and then instantiate it so the loop for placing pixels wouldn’t have to run over and over again.