Discuss Scratch

Slugsly
Scratcher
10 posts

Text engine help D:

Okay, so like here's what I want to do. I want to detect where a keyword is in the string of text I want to display using the text engine, which would then like change its color or something. Something like this:

define engine. x (x) y (y) string (string)
set [ last x v] to [ (x)]
set [ last y v] to [ (y)]
set [ x v] to [ (x)]
set [ y v] to [ (y)]
set [ # v] to [0]
set [ string v] to [ (string)]
set [ destroy all text v] to [0]
repeat (length of [ (string)])
change [ # v] by (1)
create clone of [ myself v]
end
wait until <key [ z v] pressed?>
set [ destroy all text v] to [1]

when I start as a clone
set x to (x)
set y to (y)
render
wait until <[ destroy all text v] = [1]>
hide
delete this clone

define render
set x to ((last x) + (12))
set y to (last y)
switch costume to (letter (#) of (string))
set [ last x v] to (x position)
set [ last y v] to (y position)

engine. x (0) y (0) string ()
in the string, “<red> Hey! </red> I hate you!!!!”
which would make the text “Hey!” red.

How would I do this????????? I'm so confused..

Last edited by Slugsly (Oct. 13, 2025 00:41:58)

ththorn
New Scratcher
45 posts

Text engine help D:

probably incorrect, but heres what i think you meant, assuming the default color of the text in the costumes is red:

(this goes inside the repeat block)

set [brightness v] effect to (-100)
if <[(string)] contains [(keyword)]?> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
change [# v] by (1)
create clone of [myself v]


Last edited by ththorn (Oct. 13, 2025 02:42:21)

Slugsly
Scratcher
10 posts

Text engine help D:

ththorn wrote:

probably incorrect, but heres what i think you meant, assuming the default color of the text in the costumes is red:

(this goes inside the repeat block)

set [brightness v] effect to (-100)
if <[(string)] contains [(keyword)]?> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
change [# v] by (1)
create clone of [myself v]



Thanks for the help! Although I think you misunderstood me a bit, which is probably my fault. I'm not the best at explaining things lol. I was asking about this, but I was also asking how I would detect it inside a tag, like this <red> text </red>. A bit like HTML elements, if you know HTML.

Edit: And yes, the costumes are red.

Last edited by Slugsly (Oct. 13, 2025 03:21:57)

ththorn
New Scratcher
45 posts

Text engine help D:

Slugsly wrote:

Thanks for the help! Although I think you misunderstood me a bit, which is probably my fault. I'm not the best at explaining things lol. I was asking about this, but I was also asking how I would detect it inside a tag, like this <red> text </red>. A bit like HTML elements, if you know HTML.

Edit: And yes, the costumes are red.

can you provide an example on what you mean by ‘tag’? with code please.
kuswerdz
Scratcher
100+ posts

Text engine help D:

Putting color in a text engine is very hard. Luckily, I managed to implement it myself in an Undertale-inspired dialogue engine not too long ago. I'll try to show what I did as concisely as I can, accounting for the fact that you seem to be using HTML formatting for the color codes:

define engine. xpos(x) ypos(y) string(string)
set x to (x)
set y to (y)
set [letter # v] to [0]
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
change [length of string v] by (-1)
change [letter # v] by (1)
set [letter is: v] to (letter (letter #) of (string))
switch costume to (letter is:)
if <(letter is:) = [<]> then
set [how many chars in color code? v] to [0]
set [color is: v] to []
repeat until <(letter ((letter #:) + (how many chars in color code?)) of (string)) = [>]>
change [how many chars in color code? v] by (1)
set [color is: v] to (join (color is:) (letter ((letter #:) + (how many chars in color code?)) of (string)))
end
change [length of string v] by ((how many chars in color code?) * (-1))
change [letter # v] by (how many chars in color code?)
if <(color is:) = [red>]> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
if <(color is:) = [/red>]> then
set [brightness v] effect to (100)
set [color v] effect to (0)
end
end
create clone of [myself v]
change x by (20)
end
wait until <not <key [z v] pressed?>
wait until <key [z v] pressed?>
broadcast [destroy all text v]

when I start as a clone
show

when I receive [destroy all text v]
delete this clone

Important things to note about the example script I provided:
- the text costumes are named EXACTLY what they represent (e.g. the letter “t” is named “t”)
- the text costumes are all colored RED in the sprite editor (they must be colored in order for color codes to work!)
- the text costumes are supposed to be WHITE when they are not affected by color codes

Please note that I HAVE NOT tested the script, so it might have a few bugs. I would recommend that you take inspiration from it rather than copy it verbatim, since I really don't know the internal structure of your project overall. Feel free to ask about any part if you're confused!

Last edited by kuswerdz (Oct. 13, 2025 04:11:47)

ththorn
New Scratcher
45 posts

Text engine help D:

kuswerdz wrote:

Putting color in a text engine is very hard. Luckily, I managed to implement it myself in an Undertale-inspired dialogue engine not too long ago. I'll try to show what I did as concisely as I can, accounting for the fact that you seem to be using HTML formatting for the color codes:

define engine. xpos(x) ypos(y) string(string)
set x to (x)
set y to (y)
set [letter # v] to [0]
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
change [length of string v] by (-1)
change [letter # v] by (1)
set [letter is: v] to (letter (letter #) of (string))
switch costume to (letter is:)
if <(letter is:) = [<]> then
set [how many chars in color code? v] to [0]
set [color is: v] to []
repeat until <(letter ((letter #:) + (how many chars in color code?)) of (string)) = [>]>
change [how many chars in color code? v] by (1)
set [color is: v] to (join (color is:) (letter ((letter #:) + (how many chars in color code?)) of (string)))
end
change [length of string v] by ((how many chars in color code?) * (-1))
change [letter # v] by (how many chars in color code?)
if <(color is:) = [red>]> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
if <(color is:) = [/red>]> then
set [brightness v] effect to (100)
set [color v] effect to (0)
end
end
create clone of [myself v]
change x by (20)
end
wait until <not <key [z v] pressed?>
wait until <key [z v] pressed?>
broadcast [destroy all text v]

when I start as a clone
show

when I receive [destroy all text v]
delete this clone

Important things to note about the example script I provided:
- the text costumes are named EXACTLY what they represent (e.g. the letter “t” is named “t”)
- the text costumes are all colored RED in the sprite editor (they must be colored in order for color codes to work!)
- the text costumes are supposed to be WHITE when they are not affected by color codes

Please note that I HAVE NOT tested the script, so it might have a few bugs. I would recommend that you take inspiration from it rather than copy it verbatim, since I really don't know the internal structure of your project overall. Feel free to ask about any part if you're confused!
oh wow, good job
Slugsly
Scratcher
10 posts

Text engine help D:

ththorn wrote:

Slugsly wrote:

Thanks for the help! Although I think you misunderstood me a bit, which is probably my fault. I'm not the best at explaining things lol. I was asking about this, but I was also asking how I would detect it inside a tag, like this <red> text </red>. A bit like HTML elements, if you know HTML.

Edit: And yes, the costumes are red.

can you provide an example on what you mean by ‘tag’? with code please.

Sorry it took so long for me to respond.. Scratch didn't notify me for some reason. ≽^¬`︿´¬^≼

Anyways, basically this:

define engine. x (x) y (y) string (string)
set [ last x v] to (x)
set [ last y v] to (y)
set x to (x)
set y to (y)
set [ # v] to (0)
set [ string v] to (string)
set [ destroy all text v] to (0)
repeat (length of [string])
change [ # v] by (1)
create clone of [ myself v]
end
wait until <key [ z v] pressed?>
set [ destroy all text v] to (1)

when i start as a clone
set x to (x)
set y to (y)
render
wait until <(destroy all text) = (1)>
hide
delete this clone

define render
set x to ((last x) + (12))
set y to (last y)
switch costume to (letter (#) of (string))
set [ last x v] to (x position)
set [ last y v] to (y position)

engine. x (0) y (0) string ()

In the string, “This ”<red>“ tag will enable the red text, <red> Hey! </red> This ”</red>“ tag will disable the red text”.

Truly sorry if it's still confusing; it wouldn't make sense to me either if I weren't the one asking about it. I honestly don't know how to simplify it any further.
Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Putting color in a text engine is very hard. Luckily, I managed to implement it myself in an Undertale-inspired dialogue engine not too long ago. I'll try to show what I did as concisely as I can, accounting for the fact that you seem to be using HTML formatting for the color codes:

define engine. xpos(x) ypos(y) string(string)
set x to (x)
set y to (y)
set [letter # v] to [0]
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
change [length of string v] by (-1)
change [letter # v] by (1)
set [letter is: v] to (letter (letter #) of (string))
switch costume to (letter is:)
if <(letter is:) = [<]> then
set [how many chars in color code? v] to [0]
set [color is: v] to []
repeat until <(letter ((letter #:) + (how many chars in color code?)) of (string)) = [>]>
change [how many chars in color code? v] by (1)
set [color is: v] to (join (color is:) (letter ((letter #:) + (how many chars in color code?)) of (string)))
end
change [length of string v] by ((how many chars in color code?) * (-1))
change [letter # v] by (how many chars in color code?)
if <(color is:) = [red>]> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
if <(color is:) = [/red>]> then
set [brightness v] effect to (100)
set [color v] effect to (0)
end
end
create clone of [myself v]
change x by (20)
end
wait until <not <key [z v] pressed?>
wait until <key [z v] pressed?>
broadcast [destroy all text v]

when I start as a clone
show

when I receive [destroy all text v]
delete this clone

Important things to note about the example script I provided:
- the text costumes are named EXACTLY what they represent (e.g. the letter “t” is named “t”)
- the text costumes are all colored RED in the sprite editor (they must be colored in order for color codes to work!)
- the text costumes are supposed to be WHITE when they are not affected by color codes

Please note that I HAVE NOT tested the script, so it might have a few bugs. I would recommend that you take inspiration from it rather than copy it verbatim, since I really don't know the internal structure of your project overall. Feel free to ask about any part if you're confused!

Wow, this is really good! I'll go ahead and see if your solution works Thanks for the help! ≽^•⩊•^≼

Edit: And yeah, HTML formatting. Just easier for me personally to understand, and I feel like that would be the same for a lot of other people, too.

Last edited by Slugsly (Oct. 13, 2025 05:57:35)

awesome-llama
Scratcher
1000+ posts

Text engine help D:

When you are enumerating over the characters of the string, you can check the character and process it differently if needed. For example, some text engines are able to break the input into many lines when a special character is encountered.

if <(letter (#) of (string)) = [/]> then
set x to (0) // go to new line, do not print the / character
change y by (-10)
else
print character::grey
end

The angle brackets in your tag example can be handled specially as well as the content within them. The complexity of this depends a lot on the format you want. I don't recommend XML tags as the first thing to try. You could first try creating new lines of text when a particular character is encountered. Then move on to symbols composed of multiple characters.
Perhaps if you only had a few colours you could use a special character followed by a letter representing the colour to switch to…
#rHey!#b I hate you!!!!
This example the #r means switch to red and #b means switch to black.
Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Putting color in a text engine is very hard. Luckily, I managed to implement it myself in an Undertale-inspired dialogue engine not too long ago. I'll try to show what I did as concisely as I can, accounting for the fact that you seem to be using HTML formatting for the color codes:

define engine. xpos(x) ypos(y) string(string)
set x to (x)
set y to (y)
set [letter # v] to [0]
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
change [length of string v] by (-1)
change [letter # v] by (1)
set [letter is: v] to (letter (letter #) of (string))
switch costume to (letter is:)
if <(letter is:) = [<]> then
set [how many chars in color code? v] to [0]
set [color is: v] to []
repeat until <(letter ((letter #:) + (how many chars in color code?)) of (string)) = [>]>
change [how many chars in color code? v] by (1)
set [color is: v] to (join (color is:) (letter ((letter #:) + (how many chars in color code?)) of (string)))
end
change [length of string v] by ((how many chars in color code?) * (-1))
change [letter # v] by (how many chars in color code?)
if <(color is:) = [red>]> then
set [brightness v] effect to (0)
set [color v] effect to (0)
end
if <(color is:) = [/red>]> then
set [brightness v] effect to (100)
set [color v] effect to (0)
end
end
create clone of [myself v]
change x by (20)
end
wait until <not <key [z v] pressed?>
wait until <key [z v] pressed?>
broadcast [destroy all text v]

when I start as a clone
show

when I receive [destroy all text v]
delete this clone

Important things to note about the example script I provided:
- the text costumes are named EXACTLY what they represent (e.g. the letter “t” is named “t”)
- the text costumes are all colored RED in the sprite editor (they must be colored in order for color codes to work!)
- the text costumes are supposed to be WHITE when they are not affected by color codes

Please note that I HAVE NOT tested the script, so it might have a few bugs. I would recommend that you take inspiration from it rather than copy it verbatim, since I really don't know the internal structure of your project overall. Feel free to ask about any part if you're confused!

Well, the first opening bracket of the </red> tag is white. Also, is there any way I can make the tags not display on screen when I click my custom block? Thanks again!
kuswerdz
Scratcher
100+ posts

Text engine help D:

Slugsly wrote:

Well, the first opening bracket of the </red> tag is white. Also, is there any way I can make the tags not display on screen when I click my custom block? Thanks again!

Of course! I had a feeling my script would have a bug or two. Would you mind sharing the project so I could tinker with it a little?
Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Slugsly wrote:

Well, the first opening bracket of the </red> tag is white. Also, is there any way I can make the tags not display on screen when I click my custom block? Thanks again!

Of course! I had a feeling my script would have a bug or two. Would you mind sharing the project so I could tinker with it a little?

Sure! I'll do that now! ≽^•⩊•^≼


It should be shared now.
kuswerdz
Scratcher
100+ posts

Text engine help D:

Ahh okay, I see how you're doing things. The main issue is that you're making an amount of clones equal to the length of the input, which includes special characters like your color code “<red>”. That is why I start by doing this…

define YOUR FUNCTION (string)
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
...

Instead of this:

define YOUR FUNCTION (string)
repeat (length of (string))
...
end

The length variable is essentially the length of the string, but it can be changed if the function discovers characters that should NOT be printed. When the function sees “<”, it doesn't make a clone. Instead, it knows that this is the start of a code.

If the code is “<red>”, that is 5 characters that should NOT be printed. The length variable is decreased by 5, and the current character increases by 5. This makes it so none of “<red>” is printed, and the text sprite can simply adjust its brightness/color values to be red before cloning more characters. It will stay this way until it runs into the code “<\red>”.

Keep in mind that the MAIN text sprite should be the one changing costume, color, brightness, etc.

The clones do not need to adjust their appearance, and should look how they're supposed to be RIGHT when they appear. I would restructure a lot of the “STRING MAKER” function so that almost everything is done in the “Engine.” function.
Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Ahh okay, I see how you're doing things. The main issue is that you're making an amount of clones equal to the length of the input, which includes special characters like your color code “<red>”. That is why I start by doing this…

define YOUR FUNCTION (string)
set [length of string v] to (length of (string))
repeat until <(length of string) = [0]>
...

Instead of this:

define YOUR FUNCTION (string)
repeat (length of (string))
...
end

The length variable is essentially the length of the string, but it can be changed if the function discovers characters that should NOT be printed. When the function sees “<”, it doesn't make a clone. Instead, it knows that this is the start of a code.

If the code is “<red>”, that is 5 characters that should NOT be printed. The length variable is decreased by 5, and the current character increases by 5. This makes it so none of “<red>” is printed, and the text sprite can simply adjust its brightness/color values to be red before cloning more characters. It will stay this way until it runs into the code “<\red>”.

Keep in mind that the MAIN text sprite should be the one changing costume, color, brightness, etc.

The clones do not need to adjust their appearance, and should look how they're supposed to be RIGHT when they appear. I would restructure a lot of the “STRING MAKER” function so that almost everything is done in the “Engine.” function.

Wow!!! I never realised how inefficient it is until now, haha. ≽^o︿o^≼ Thanks for all the help! ≽^•⩊•^≼

Had you never replied to this forum, I would've given up completely, haha. Or at least some of the features I wanted to add would've been given up on.

edit: I think I might need to completely rework my text engine. Oh well, it's no big deal! (save me)

Last edited by Slugsly (Oct. 15, 2025 23:39:22)

TheUugrimaa
Scratcher
87 posts

Text engine help D:

use <#FF0000> Hey! </#FF0000>
I think this works
kuswerdz
Scratcher
100+ posts

Text engine help D:

Slugsly wrote:

edit: I think I might need to completely rework my text engine. Oh well, it's no big deal! (save me)

Oh boy, I sure know how that feels. I had to do a lot of troubleshooting with my own version, but it felt really satisfying to get it all up and running. I hope you stick with it, because it looks great so far!
Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Slugsly wrote:

edit: I think I might need to completely rework my text engine. Oh well, it's no big deal! (save me)

Oh boy, I sure know how that feels. I had to do a lot of troubleshooting with my own version, but it felt really satisfying to get it all up and running. I hope you stick with it, because it looks great so far!

Hey, I just have one last thing.. How could I distinguish between a code and the characters the user wants printed?
kuswerdz
Scratcher
100+ posts

Text engine help D:

Slugsly wrote:

Hey, I just have one last thing.. How could I distinguish between a code and the characters the user wants printed?

What you need is a system that goes character by character, like that giant script I showed you.

If the character is NOT the start of a code (<), the text sprite gets into the proper costume, position, and size for the character, then clones itself.

If the character IS the start of a code, the text sprite does not clone itself. Instead, it does something like this:

set [code v] to []
repeat until <<(letter ((index) + (1)) of (input)) = [>]> or <(letter ((index) + (1)) of (input)) = []>
change [index v] by (1)
change [length of input v] by (-1)
set [code v] to (join (code) (letter (index) of (input))
end
change [index v] by (1)
change [length of input v] by (-1)
if <(code) = [red]> then
set [color v] effect to (0)
set [brightness v] effect to (0)
end
if <(code) = [/red]> then
set [color v] effect to (0)
set [brightness v] effect to (100)
end

This fetches the code without printing anything, then skips to the character after the code.
If there is text after the color code, it should print in the specified color.

Should be more convenient to implement than the first example I gave you, so let me know how it goes!

Last edited by kuswerdz (Oct. 18, 2025 19:20:37)

Slugsly
Scratcher
10 posts

Text engine help D:

kuswerdz wrote:

Slugsly wrote:

Hey, I just have one last thing.. How could I distinguish between a code and the characters the user wants printed?

What you need is a system that goes character by character, like that giant script I showed you.

If the character is NOT the start of a code (<), the text sprite gets into the proper costume, position, and size for the character, then clones itself.

If the character IS the start of a code, the text sprite does not clone itself. Instead, it does something like this:

set [code v] to []
repeat until <<(letter ((index) + (1)) of (input)) = [>]> or <(letter ((index) + (1)) of (input)) = []>
change [index v] by (1)
change [length of input v] by (-1)
set [code v] to (join (code) (letter (index) of (input))
end
change [index v] by (1)
change [length of input v] by (-1)
if <(code) = [red]> then
set [color v] effect to (0)
set [brightness v] effect to (0)
end
if <(code) = [/red]> then
set [color v] effect to (0)
set [brightness v] effect to (100)
end

This fetches the code without printing anything, then skips to the character after the code.
If there is text after the color code, it should print in the specified color.

Should be more convenient to implement than the first example I gave you, so let me know how it goes!

So, it's not changing color????? It doesn't print the color code anymore, but it doesn't change color. I'm confused because your code should be working. D:
kuswerdz
Scratcher
100+ posts

Text engine help D:

Re-share project?

Powered by DjangoBB