How do I find the position of the end of displayed text?

0 favourites
  • 3 posts
From the Asset Store
Animated Text
$3.99 USD
With AnimatedText behavior you can make an impression of the text being “typed live”.
  • I have a dialogue box, just a normal text object, and basically I want to put a sprite at the end of the text, like a caret. There's text wrapping, so textwidth doesn't give me the position of the end of the text. So how do I do this?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This may not be possible with C2 as it currently stands.

    You can append a caret to the text object. The only way I can think of to get the position of the last character is if you placed every character in its own instance of a text object, which is possible, but probably not very elegant.

  • In case anyone else is wondering, I figured out how to do it. I'm not sure it's the most elegant solution, but it works. I figured all I need to do is isolate the last row of text and find the width of that. So if I can have a text object that contains just the last row of that text, I can use textwidth to find the width of the bottom row of text.

    So essentially I have a clone object of the text object I want to find the end position of. So same size, font, etc. We'll call this LastRow. Because textwidth doesn't update until the Text object is redrawn, we can't just run a loop, we have to run it frame by frame. Since text wrapping is on a word-by-word basis, it makes sense to run it word-by-word. So we transfer the text from the displayed text object to LastRow, one word per tick, using tokenat() with " " as the separator. Then another event checks when textwidth deviates from the normal value, the height of one line of text (in my case it's 20). When it does, we need to cut off that entire first row of text; so we do this by using tokenat() again, this time setting the text to the last word of text (and a space). Once it's gone through the entire length of text, you'll have the bottom row isolated, and you can use textwidth to find the width of that, and textheight of the display text, and use that for the x and y of the caret sprite or whatever you intend to put there (with an offset according to where that display box is on the screen of course).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)