Discuss Scratch

DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

https://scratch.mit.edu/projects/1046378656/

This is my project that is slowly killing me. The word wrapping is a unique and simple way that even I understand. It slows everything down, and I know it's just the coding because TurboMode isn't any different. Please, can someone help me and proof read? If you find this annoying, sorry, I've posted another topic similar to this months ago, but now it's buried and I didn't get much help. Does anyone know a way to do Word Wrapping in a simple way? Or just explain the hard way and I'll get right to it. Thanks!

Edit: For those of you who don't feel like visiting my project, here is my coding;

define Simple Word Wrapping (t) (X)
set [i v] to (0)
set [f v] to (1)
delete all of [WordCount v]
repeat (length of (t))
if <(letter ((f) + (i)) of (t)) = ( )> then
set [i v] to ((i) + (f))
add ((f) - [1]) to [WordCount v]
set [f v] to (0)
end
set [Calc.Dist. v] to (round (((([195] + (length of (t))) / (3.6)) - (x position)) / (12)))
if <(letter ((write) - [1]) of (t)) = ( )> then
if <(item (length of [WordCount v]) of [WordCount v]) > (Calc.Dist)> then
go to x: (X) y: ((y position) - (round ((size) / [5])))
delete all of [WordCount v]
end
end
if <(length of [WordCount v]) > [5]> then
delete all of [WordCount v]
end
change [f v] by [1]
end
stop [this script v]

The write variable is the thing that always changes by one in my text engine script. The WordCount is a list with the lengths of the recent words. Calc.Dist. is the calculated amount of characters that can fit between their position and the wall (edge).

I can provide my text engine coding if needed.

Last edited by DragonPlusMaster (Dec. 14, 2024 16:54:32)

KevLeCodeur
Scratcher
38 posts

(Solved) WORD WRAPPING ;-;

The link doesn't work
DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

KevLeCodeur wrote:

The link doesn't work
Sorry, I fixed it! I took it down and forgot to put it up for this post.
DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

Uh…. bump. Guys you don't need to write the solution in blocks for me, just explain how it works if it's easier. Please, I've been struggling with this project for over two years!

Last edited by DragonPlusMaster (Dec. 12, 2024 23:43:55)

s714655
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

I haven't seen the project yet, I'll look into that. To word wrap, just keep an index of the letters and a “safe” index counting the index of the last space, if the letter in that word is beyond the word wrap then go back to the safe index and go down.
DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

s714655 wrote:

I haven't seen the project yet, I'll look into that. To word wrap, just keep an index of the letters and a “safe” index counting the index of the last space, if the letter in that word is beyond the word wrap then go back to the safe index and go down.

I have tried it, and it works fine! But I think the code is the end of me, because it works, but it slows everything done and writes in chunks of words. For example, when I have it write:

Agh, why is this so frustrating? For heavens sake, please work!

It would write:
Agh, why is this so frustrating? For {pause} heavens sake, please work!

And it wraps it perfectly. I don't even know whats happening T0T

Last edited by DragonPlusMaster (Dec. 13, 2024 01:51:50)

SpyCoderX
Scratcher
1000+ posts

(Solved) WORD WRAPPING ;-;

Do you have any wait statements in it? Just wondering because wait blocks in a “run without screen refresh” custom block will cause tons of lag.
DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

SpyCoderX wrote:

Do you have any wait statements in it? Just wondering because wait blocks in a “run without screen refresh” custom block will cause tons of lag.

I have waits in the Key Sensing and Backspace custom blocks (They do not have the Run Without Screen Refresh checkbox checked). I tested it with removing them, but I still got the same results (Plus a bunch of characters being typed a million times). Thanks for trying, though.

Last edited by DragonPlusMaster (Dec. 13, 2024 03:58:27)

deck26
Scratcher
1000+ posts

(Solved) WORD WRAPPING ;-;

@griffpatch uses the blank sprite to speed up loading but it has to be the first sprite! Your's is doing nothing helpful at the moment.

I don't see any problem when I manually run it with your test text.

DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

deck26 wrote:

@griffpatch uses the blank sprite to speed up loading but it has to be the first sprite! Your's is doing nothing helpful at the moment.

I don't see any problem when I manually run it with your test text.


Thanks. I tried it, and it helped a little bit, but it still slows down. It's in the coding itself, because TurboMode isn't any better. I'm like, so confuzled right now…

And by test text, do you happen to mean the one about locks? (The very long one) Because when I test it, it always writes it into two chunks, if you know what I mean. If it's working perfectly for you, that is startling.

Last edited by DragonPlusMaster (Dec. 14, 2024 03:44:54)

theashbreather
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

I think the implementation of the system could be refactored. Instead of using the list, for every single typed character I would implement each word as a item. Then, the system would either linebreak every word that isn't the first item but breaks the character threshold, or linebreak when the character threshold has been met again and the word has already linebreaked once because it was too long to fit in with the previous word (the recording of how many times a word has linebreaked stored in a seperate list).

About the original issue, I am not noticing any lag on my computer. Works just as fine for me.

Last edited by theashbreather (Dec. 14, 2024 04:37:13)

MyNameIsNotClyde
Scratcher
24 posts

(Solved) WORD WRAPPING ;-;

It seems like your code is written inefficiently. There is a loop in “Write (thing) (x) (y)” that repeats length of (thing) times, and each loop calls “Simple Word Wrapping (t) (X)” with (t) = (thing) which also loops length of (thing) times. A string of length 300 like your “The spring and upper pins …” would run 300*300 = 90000 loops.

To fix this, you have to improve your code's efficiency. For example instead of going through the entire string everytime you call Simple Word Wrapping, you can just call it whenever “write” goes on a space character, then look at the next word (up to the next space character), and calculate based on the current x position. This way instead of 90000 loops, you only need 300+300 = 600 loops.

In more advanced computer science words, your current code is running in O(n^2) time, when it could be running in O(n) time.

Here is my fix to your project. https://scratch.mit.edu/projects/1110649727/
I have changed the code in the TESTING sprite so writing shouldn't lag as much anymore.

Last edited by MyNameIsNotClyde (Dec. 14, 2024 08:28:36)

B1j2754
Scratcher
78 posts

(Solved) WORD WRAPPING ;-;

Edit: Ninja'd. I didn't reload the page for responses

From the testing I did, it seems that when you click a button (i.e. a or -) it adds that letter to the overall string. (which is what we want)

Then it passes in the whole string into the line wrapper (as in every letter previously typed).

That means that logically, as you make that overall string paragraph bigger, it is going to be running the line wrapper on the whole text, which will obviously slow it down, as it has to process more and more characters.

I think the best solution here (correct me if this is not correct), is to only wrap the current line, and store separate lines in a list or something similar. This way, you can have a variable track which line it should add the character to, reducing the processing down to only the current line.

Happy to try my hand at implementing this in your code if you would like

Last edited by B1j2754 (Dec. 14, 2024 16:39:16)

DragonPlusMaster
Scratcher
100+ posts

(Solved) WORD WRAPPING ;-;

MyNameIsNotClyde wrote:

It seems like your code is written inefficiently. There is a loop in “Write (thing) (x) (y)” that repeats length of (thing) times, and each loop calls “Simple Word Wrapping (t) (X)” with (t) = (thing) which also loops length of (thing) times. A string of length 300 like your “The spring and upper pins …” would run 300*300 = 90000 loops.

To fix this, you have to improve your code's efficiency. For example instead of going through the entire string everytime you call Simple Word Wrapping, you can just call it whenever “write” goes on a space character, then look at the next word (up to the next space character), and calculate based on the current x position. This way instead of 90000 loops, you only need 300+300 = 600 loops.

In more advanced computer science words, your current code is running in O(n^2) time, when it could be running in O(n) time.

Here is my fix to your project. https://scratch.mit.edu/projects/1110649727/
I have changed the code in the TESTING sprite so writing shouldn't lag as much anymore.

Thanks! I see where I went wrong. This means a lot :')

Powered by DjangoBB