Yes, you can definitely run these sections concurrently in GDScript using Thread. Here's how you can do it, along with explanations and considerations:
extends Node # Or whatever your base node is
var Growthmap = {}
var ElevationmapA = {}
var ElevationmapB = {}
var Elevationmap = {}
var Smoothmap = {}
var BaseTilemap = {}
var BaseTilemapA = {}
var MGKeys = [] # Populate this with your MGKeys
var MapGrid = {} # Populate this with your MapGrid
var NeighborMap = {} # Populate this with your NeighborMap
var RNG = RandomNumberGenerator.new() # Initialize your RNG
var GrowthVar = 5 # Replace with your actual value
var ElevationM = 10 # Replace with your actual value
var IslandR = 5 # Replace with your actual value
var IslandF = 2 # Replace with your actual value
var LibFTGF = [1, 2, 3, 4] # Replace with your actual values
var WorldS = 100 # Replace with your actual World Size
var _growth_thread : Thread
var _elevation_thread : Thread
var _smooth_thread : Thread
var _base_tile_thread : Thread
func _ready():
RNG.randomize() # Seed the random number generator
# Populate MGKeys and other necessary variables
# Example:
for i in range(100):
MGKeys.append(str(i))
MapGrid[str(i)] = i
_growth_thread = Thread.new()
_elevation_thread = Thread.new()
_smooth_thread = Thread.new()
_base_tile_thread = Thread.new()
_growth_thread.start(_generate_growthmap.bind())
_elevation_thread.start(_generate_elevationmap.bind())
_smooth_thread.start(_generate_smoothmap.bind())
_base_tile_thread.start(_generate_base_tilemap.bind())
# Wait for all threads to finish. Critical!
while (_growth_thread.is_alive() or _elevation_thread.is_alive() or _smooth_thread.is_alive() or _base_tile_thread.is_alive()):
await get_tree().process_frame # Give the threads a chance to run
print("All map generation threads finished!")
# Now you can use Growthmap, Elevationmap, Smoothmap, and BaseTilemap.
print("Growthmap: ", Growthmap)
print("Elevationmap: ", Elevationmap)
print("Smoothmap: ", Smoothmap)
print("BaseTilemap: ", BaseTilemap)
func _generate_growthmap():
#Initialize Growthmap
var local_growthmap = {} # Local variable for thread safety
for Key in MGKeys:
local_growthmap[MapGrid[Key]] = RNG.randi_range(GrowthVar * -1, GrowthVar)
# Atomic update of the shared variable
call_deferred("_update_growthmap", local_growthmap)
func _update_growthmap(data):
Growthmap = data
func _generate_elevationmap():
#Initialize Elevation
var local_elevationmapA = {}
var local_elevationmapB = {}
var local_elevationmap = {}
var local_rng = RandomNumberGenerator.new() # local RNG instance
local_rng.seed = RNG.seed # Replicate the seed from main RNG
for Key in MGKeys:
Tile_Index = MapGrid[Key]
local_elevationmapA[Tile_Index] = local_rng.randi_range(0, ElevationM)
A = local_rng.randi_range(0, IslandR)
if A == 0:
A = IslandF
else:
A = 0
local_elevationmapB[Tile_Index] = float(A)
local_elevationmapB = Variable_Blur(MapGrid, local_elevationmapB, NeighborMap, 100, 0.5)
for Key in MGKeys:
Tile_Index = MapGrid[Key]
if local_elevationmapB[Tile_Index] > 0.1:
A = 1.05
else:
A = 1
A = int(round(A * local_elevationmapA[Tile_Index]))
local_elevationmap[Tile_Index] = A
call_deferred("_update_elevationmap", local_elevationmap)
func _update_elevationmap(data):
Elevationmap = data
func _generate_smoothmap():
#Initialize Smoothmap
var local_smoothmap = {}
var local_rng = RandomNumberGenerator.new() # local RNG instance
local_rng.seed = RNG.seed # Replicate the seed from main RNG
for Key in MGKeys:
local_smoothmap[MapGrid[Key]] = local_rng.randi_range(0, 4)
call_deferred("_update_smoothmap", local_smoothmap)
func _update_smoothmap(data):
Smoothmap = data
func _generate_base_tilemap():
#Initialize BaseTilemap
var local_baseTilemap = {}
var local_baseTilemapA = {}
var local_rng = RandomNumberGenerator.new() # local RNG instance
local_rng.seed = RNG.seed # Replicate the seed from main RNG
A = LibFTGF[0] + LibFTGF[1] + LibFTGF[2] + LibFTGF[3] - 1
B = int(round(float(WorldS) * 0.35))
for Key in MGKeys:
local_baseTilemapA[MapGrid[Key]] = float(local_rng.randi_range(0, A))
local_baseTilemapA = Variable_Modified_Blur_B(MapGrid, local_baseTilemapA, NeighborMap, 0, A, B)
call_deferred("_update_base_tilemap", local_baseTilemapA)
func _update_base_tilemap(data):
BaseTilemap = data
# Your blurring functions (Variable_Blur, Variable_Modified_Blur_B)
# MUST be thread-safe. If they use Godot's API directly, this will cause crashes.
# You will need to find or write thread-safe alternatives.
func Variable_Blur(map_grid, elevation_map, neighbor_map, iterations, strength):
# Placeholder, replace with your thread-safe blurring logic
return elevation_map
func Variable_Modified_Blur_B(map_grid, base_tilemap_a, neighbor_map, min_value, max_value, threshold):
# Placeholder, replace with your thread-safe blurring logic
return base_tilemap_a