Unfortunately, I still just can't get what the problem is...
Here's my whole code if it helps.
The scene contains only a tilemap called "world".
extends TileMap
var cell_side = 8
var n_grid = 800/cell_side
var world = []
var pause = true
func _ready():
new_world("random")
toggle_pause()
func _process(_delta):
if Input.is_action_just_pressed("pause"): pause = !pause ; toggle_pause()
if Input.is_action_just_pressed("fullscreen"): OS.window_fullscreen = !OS.window_fullscreen
if Input.is_action_just_pressed("quit"): get_tree().quit()
if pause:
if Input.is_action_pressed("revive"):
set_cellv((get_local_mouse_position()/cell_side).floor(), 1)
read_world()
if Input.is_action_pressed("kill"):
set_cellv((get_local_mouse_position()/cell_side).floor(), 0)
read_world()
if Input.is_action_just_pressed("next"): update_world()
if Input.is_action_just_pressed("clear"): new_world("empty")
if Input.is_action_just_pressed("random"): new_world("random")
if Input.is_action_just_pressed("save_world"):
new_world("load")
save_world()
if Input.is_action_just_pressed("load_world"): load_world()
else: update_world()
func neighbors(x,y):
var x1 = x-1; if x1 < 0: x1 = n_grid-1
var x2 = x+1; if x2 > n_grid-1: x2 = 0
var y1 = y-1; if y1 < 0: y1 = n_grid-1
var y2 = y+1; if y2 > n_grid-1: y2 = 0
var neighbors = get_cell(x1,y) + get_cell(x2,y) + get_cell(x,y1) + get_cell(x,y2) + get_cell(x1,y1) + get_cell(x2,y1) + get_cell(x1,y2) + get_cell(x2,y2)
return neighbors
func read_world():
for v in range (n_grid):
for u in range (n_grid):
world[u][v] = get_cell(u,v)
func update_world():
for v in range (n_grid):
for u in range (n_grid):
if get_cell(u,v) == 1:
if neighbors(u,v) in [2,3]:
world[u][v] = 1
else: world[u][v] = 0
if get_cell(u,v) == 0:
if neighbors(u,v) == 3:
world[u][v] = 1
else: world[u][v] = 0
for v in range (n_grid):
for u in range (n_grid):
set_cell(u,v,world[u][v])
func new_world(type):
for v in range (n_grid):
var cells = []
for u in range (n_grid):
if type == "random":
randomize()
if randf() <= 0.25: set_cell(u,v,1)
else: set_cell(u,v,0)
if type == "empty": set_cell(u,v,0)
if type == "load": set_cell(u,v,world[u][v])
cells.append(0)
world.append(cells)
func toggle_pause():
if pause: modulate = Color(3,1,1,1)
else: modulate = Color(1,1,1,1)
func save_world():
var dir = Directory.new()
dir.remove("user://save.dat")
var file = File.new()
var error = file.open("user://save.dat", File.WRITE)
if error == OK:
file.store_var(world)
file.close()
func load_world():
var file = File.new()
if file.file_exists("user://save.dat"):
var error = file.open("user://save.dat", File.READ)
if error == OK:
world = file.get_var()
new_world("load")
file.close()