Topic was automatically imported from the old Question2Answer platform.
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()?
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.