this is an updated version of the typewriter effect I made last year, shhhh!!!
Introduction
Have you ever wanted to recreate those fancy typewriter effects from games like Undertale or Hollow Knight??!?!? No??? Well, you probably have or would have at some point. Anyways, you’re in LUCK!! Because this topic will cover how to create a simple & working typewriter effect. I am not the best explainer so please don’t go too hard on me.
How to actually do it
For this tutorial, we’ll be using custom blocks because they are awesome they are very handy for code reusing and abiding by the classic Don’t Repeat Yourself rule. If you have a very strong grudge against them, you are free to skip the steps marked with a .
Step 0: Prepare an Object
Before we start coding, we obviously need an object for the code to be put in! So go ahead, either create a new text object or use one that already exists.
Step 1: Creating a Custom Block
First of all, we’ll create a custom block that you can name anything BUT be sure to add 2 parameters; text
and delay
. You don’t have to name them exactly that but I’ll be using those names for this tutorial.
FOR THOSE WHO AREN’T USING CUSTOM BLOCKS: Create 2 local variables named text
and delay
!
Step 2: Creating an ‘Index’ variable
For the next step, we’d need to create an Index
local variable. I’ll explain its purpose in a bit!
Step 3: Code!
Now it is time for us to cook up code! Brace yourselves!
- Insert a
Set variable
block and change the 2 parameters intoIndex
and0
inside the custom block.
- Insert a
Repeat times
block afterwards. - Change the parameter in the
Repeat times
block withlength(text)
(Use the local variable!) - Add a
Increase variable
block inside theRepeat times
block and change it toIncrease Index by 1
- Afterwards, add a
Set Text
block (still in the repeat block) and change the first parameter tocharacters in text between 0 and Index
(Use the local variables! You can change the color to anything you want btw) - [OPTIONAL] Add a
Start Sound
block (stilll in the repeat block) and change the sound to anything you want. - Add a
Wait seconds
block and change the parameter todelay
. (the local variable) -
Fill in the details of the custom block! Change the
text
parameter to the text you want and thedelay
parameter to how long you want the delay for every letter to be.
Explanation
For you to actually understand what you just coded, we’ll need to grasp on how this typewriter system works in general.
We want to have the text start from nothing, then have the code add more letters with a delay for every character until it fully forms a sentence.
In the repeat times
block, the code inside will loop for the same amount as the amount of characters in the text you put in within the custom block. And what the code does is that it adds how many letters that can be seen (what the characters in text between
block does).
And you may be asking, “what’s the purpose of the Index variable??? you havent explained it AT ALL!!!” Index
variables within loops are usually used to keep track of how many times the loop has run. And you may be asking, “what’s the purpose in that???” And to that, you can see that it was used within the characters in text between
block.
The Index variable makes the text you see longer based on how many times the loop has run. It basically tells the character in text between
block, “hey! this code has run for the Xth time, so you should add Xth amount of letters to display!” in which the character in text between
block would respond with “Ok” and do what it said.
And as explained before, the repeat times block will run for the exact amount of letters from the text parameter/local variable, so that means the code will end with a fully formed sentence, courtesy of Index
and character between text
’s hard work.
…And that’s it!
If you did it correctly (and if I actually wrote this tutorial correctly), it should work beautifullyyyy! I deeply apologize if I explained things very awfully or some of the code won’t work even though you’ve followed this tutorial very closely, you can always ask me for questions!
So, thanks for reading. <3