# Displaying text letter by letter

**URL:** https://discourse.processing.org/t/displaying-text-letter-by-letter/31099
**Category:** Processing.py
**Created:** [July 7, 2021, 8:25am UTC](https://discourse.processing.org/t/displaying-text-letter-by-letter/31099 "2021-07-07T08:25:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Polo](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@Polo](https://discourse.processing.org/u/Polo)
#### Post date: [July 7, 2021, 8:25am UTC](https://discourse.processing.org/t/displaying-text-letter-by-letter/31099/1 "2021-07-07T08:25:31Z")

</div>

Hello everyone, I’m a school-level beginner programmer and I’m trying to display text letter by letter. There is something that I don’t understand about my code, because it works perfectly on a console but just doesn’t update the display window until the function is finished. I was wondering if there is a way to force the update or if maybe there could be a better script for this (of those that I could understand).

```auto
def textAnimated(toType,x,y,nxt):
    background(0)
    global t
    global toTypeR
    if nxt==False:
        t=0
        toTypeR=""
    st=millis()
    s=st
    toTypeR=toTypeR+toType[t]
    text(toTypeR,x,y)
    while s<st+100:
        s=millis()
    t+=1
    if t<len(toType):
        textAnimated(toType,x,y,True)

```

Thank you very much!

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [July 7, 2021, 8:55am UTC](https://discourse.processing.org/t/displaying-text-letter-by-letter/31099/2 "2021-07-07T08:55:13Z")

</div>

Welcome @Polo

Processing uses the [`draw()` function](https://py.processing.org/reference/draw.html) to draw to the display window. You should adapt your code to work with this structure.

```python
def setup():
    # this code executes once at the start
    size(200, 200)
    frameRate(5)

def draw():
    # this code executes every frame
    background(random(255))
    text('Hello, World', 65, 105)

```

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [July 7, 2021, 9:06am UTC](https://discourse.processing.org/t/displaying-text-letter-by-letter/31099/3 "2021-07-07T09:06:37Z")

</div>

Welcome to the Processing Community, @Polo!

Your `textAnimated` function contains this conditional block:

```auto
    if t<len(toType):
        textAnimated(toType,x,y,True)

```

Therefore, the function calls itself. Are you intentionally writing the function as a recursion?

_EDITED 2x on July 7, 2021 to add the following:_

To follow @tabreturn’s advice, you can add this prior to your function definition:

```auto
toTypeR = ""
t = 0
def setup():
    size(400, 100)
    noLoop()
def draw():
    textAnimated("This is the text!", 0, 20, True)

```

You may see the entire text appear all at once, which does not seem to be what you wanted.

Fitting the recursion into the code structure of a Processing sketch to implement an animation can be tricky. You may benefit from reading the following discussion thread for guidance:

[Is my fractal optimized? Could it animate?](https://discourse.processing.org/t/is-my-fractal-optimized-could-it-animate/28845/)

Though it focuses on Java Mode, it does include a Python Mode example.

Also see [Tower Hanoi - Why recursive draw only first and last steps?](https://discourse.processing.org/t/tower-hanoi-why-recursive-draw-only-first-and-last-steps/28923).

---

<div class="post-metadata">

### Author: ![Polo](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@Polo](https://discourse.processing.org/u/Polo)
#### Post date: [July 7, 2021, 3:34pm UTC](https://discourse.processing.org/t/displaying-text-letter-by-letter/31099/4 "2021-07-07T15:34:37Z")

</div>

Thank you very much, I’ll try that. I was simply wondering if it was possible to write such a function outside of _draw_.
