Godot Version
4.2.2
Question
I am trying to create a procedurally generated world that also uses autotiling, however it is not working.
Here is the relevant code:
extends TileMap
@onready var tilemap1 = $Water
@onready var tilemap2 = $Land
@export var moisture = FastNoiseLite.new()
@export var temperature = FastNoiseLite.new()
@export var altitude = FastNoiseLite.new()
var width = 64
var height = 64
@onready var player = get_tree().current_scene.get_node("CharacterBody2D")
var loaded_chunks = []
func _ready():
moisture.seed = randi()
temperature.seed = randi()
altitude.seed = randi()
altitude.frequency = .001
func _process(delta):
var player_tile_pos = local_to_map(player.position)
generate_chunk(player_tile_pos)
unload_distant_chunks(player_tile_pos)
func generate_chunk(pos):
var cells = []
for x in range(width):
for y in range(height):
var moist = moisture.get_noise_2d(pos.x - width / 2 + x, pos.y - height / 2 + y)*10
var temp = temperature.get_noise_2d(pos.x - width / 2 + x, pos.y - height / 2 + y)*10
var alt = altitude.get_noise_2d(pos.x - width / 2 + x, pos.y - height / 2 + y)*10
#If statment to determine Water
if alt < 0:
set_cell(0, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 0, Vector2(0,0), 0)
#If statement to determine sand
if alt > 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(4,0), 0)
#If statement to determine land
if alt > .35:
#Within land, if statement to determine hot biomes
if temp > 1:
#Within hot biomes, if statement to determine savannah
if moist > 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(0,2), 0)
#Within hot biomes, if statement to determine dessert
if moist < 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(0,3), 0)
#Within land, if statement to determine cold biomes
if temp < -1:
#Within cold biomes, if statement to determine snow
if moist > 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(1,0), 0)
#Within cold biomes, if statement to determine Tundra
if moist < 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(2,0), 0)
#Within land, if statement to determine mid biomes
if temp < 1 and temp > -1:
#Within mid biomes, if statement to determine swamp
if moist > 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(2,1), 0)
#Within mid biomes, if statement to determine plains
if moist < 0:
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), 2, Vector2(3,1), 0)
if Vector2i(pos.x,pos.y) not in loaded_chunks:
loaded_chunks.append(Vector2i(pos.x,pos.y))
tilemap2.set_cells_terrain_connect(0, cells, 0, 1)
set_cells_terrain_connect(0, cells, 0, 2)
func unload_distant_chunks(player_pos):
var unload_dist = (width * 2) + 1
for chunk in loaded_chunks:
var dist_to_player = dist(chunk, player_pos)
if dist_to_player > unload_dist:
clear_chunk(chunk)
loaded_chunks.erase(chunk)
func clear_chunk(pos):
for x in range(width):
for y in range(height):
set_cell(0, Vector2(x,y), -1, Vector2(-1,-1))
set_cell(0, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), -1, Vector2(-1,-1))
set_cell(1, Vector2i(pos.x-width/2 + x, pos.y-height/2+y), -1, Vector2(-1,-1))
func dist(p1,p2):
var r = p1-p2
return sqrt(r.x ** 2 + r.y ** 2)
I am using a local_to_map function to generate the world, and it only seemed to work in a script that extende Tilemap, however it seems that the set_cells_terrain_connect function isnt working due to tilemap2 being null. But in the tutorials ive seen, everyone uses in in a script that extends node 2D
I apologize for any confusion this is my first post and i am a very inexperienced programmer.
*Edit to show more code