Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | szabotudor |
Exactly like the title says. I create a thread, it runs one time and the wait_to_finish function does not work. this is my entire code:
extends Spatial
var objects = 0
var block = load("res://Scenes/Blocks/Block.tscn")
var chunks = []
var generator_thread = Thread.new()
onready var gridmap = $GridMap
onready var player = $Player
export var generator_seed = int()
export var generator_height_amplifier = int()
export var chunk_size = int()
func _ready():
generator_thread.start(self, "generate_chunk", [generator_seed, chunk_size, Vector2(0, 0)])
generator_thread.wait_to_finish()
func _process(delta):
$Label.text = String(Engine.get_frames_per_second()) + "\n" + String(objects)
print(generator_thread.is_active())
var pos = player.global_transform.origin
pos = gridmap.world_to_map(pos)
pos = pos / chunk_size
pos = Vector2(floor(pos.x), floor(pos.z))
for x in range(pos.x - 3, pos.y + 3):
for y in range(pos.y - 3, pos.y + 3):
if not(generator_thread.is_active()):
generator_thread = Thread.new()
generator_thread.start(self, "generate_chunk", [generator_seed, chunk_size, Vector2(x, y)])
func generate_chunk(data):
var gen_seed = data[0]
var size = data[1]
var place = data[2]
if place in chunks:
return
chunks.append(place)
place = place * size
var noise = OpenSimplexNoise.new()
noise.seed = gen_seed
var cells = []
for x in range(place.x - size, place.x + size):
for z in range(place.y - size, place.y + size):
for y in range(0, noise.get_noise_2d(x, z) * generator_height_amplifier + generator_height_amplifier):
cells.append(Vector3(x, y, z))
yield(get_tree(), "idle_frame")
for cell in cells:
gridmap.set_cell_item(cell.x, cell.y, cell.z, 0)
yield(get_tree(), "idle_frame")
call_deferred("generate_done")
```
func generate_done():
generator_thread.wait_to_finish()
```
I don't know almost anything about threads, this is the first time I used them in godot, so please explain in noob terms.