0 votes

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.setcell(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()?

in Engine by (12 points)

2 Answers

0 votes

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()
by (3,886 points)
+1 vote

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.

by (28 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.