How to fill(set_cell) a tilemap that is staggered?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bryanlorenzo79

i have an 8x8 image tile to fill a tilemap that is 30x50(portrait) size. i fill the tilemap using a for loop. it works if you want it instantaneous.
for x in range(0, size.x):
for y in range(0, size.y):
tileMap.set_cell(x, y, cellIndex)

but what if i want to see the cellIndex filling the screen gradually? i did try using the yield(). it does work but if the timer is set to 0.01 to fill the screen faster, there is no difference between a timer set to 0.1 and 0.01. or even 0.00001.
for x in range(0, size.x):
for y in range(0, size.y):
tileMap.set_cell(x, y, color)
yield(get_tree().create_timer(0.1), “timeout”)
what can i do to gradually fill the tileMap with cell that is gradually but can fill it faster using the yield()?

:bust_in_silhouette: Reply From: goldfish

You shouldn’t be using yield at all. You’re doing that in _physics_process, not _process, right? Otherwise you wouldn’t see it updating at all because you’re halting the function and the function is actually supposed to be run as fast as possible, as often as the screen can update.
You have to remember where you left off and check in whether it is the right time to place the next tile:

# these are outside the function, so that they remember the time and which tile we left of at
var time : float = 0
var currentTile : int = 0
func _physics_process(delta : float) -> void:
 time += delta
 if time > 0.1:
  time -= 0.1
  if currentTile < size.x*size.y:
   # here we are converting the current index to a position
   var x : int = currentTile%size.y
   var y : int = currentTile/size.y
   tileMap.setcell(x, y, color)
   currentTile += 1

Replace the spaces with tabs. I haven’t tested it but it should work.

:bust_in_silhouette: Reply From: timothybrentwood

Creating a loop through a Timer’s timeout signal should work unless there’s something funky about TileMaps that I’m unaware of:

var wait_timer : Timer
var cur_x := 0
var cur_y := 0

func _ready() -> void:
	wait_timer = Timer.new()
	self.add_child(wait_timer)
	wait_timer.one_shot = false
	wait_timer.connect("timeout", self, "_on_wait_timer_timeout")
    wait_timer.wait_time = 0.1
	wait_timer.start()
	
func _on_wait_timer_timeout():
	if cur_x < size.x:
		if cur_y < size.y:
			tileMap.setcell(cur_x, cur_y, color)
			cur_y = cur_y + 1
		else:
			cur_y = 0
			cur_x = cur_x + 1
	else:
		wait_timer.stop()