Auto-Adjusting Text Boxes

Post your own tutorials, guides and demos.

Post » Wed Jul 01, 2009 1:34 pm

I don't know if someone has already supplied a tutorial to do this or if Construct has this feature already, but just in case they haven't, here is my way.

You'll need three Private Variables for your Text Box.

--------------------------------------------------------
TextSize (this should be set to your chosen font size)

NewWidth (this leave as 0)

TextAmmount (this leave as 0)
--------------------------------------------------------

In a new event sheet create an Always event.

Actions:

Lives_Txt: Set 'TextAmmount' to len(Lives_Txt.Text)

Start a new action. Select your text box, in this example mine is named Lives_Txt. Click Set Value. Your private variable should be TextAmmount and for your value click down on the System icon. When it comes up, look in the Text section for Get String Length, or just type lens(yada) Now the value you want to put inside the lens function is Lives_Txt.Text. You can type it in or click the Lives_Txt object and select Get Text.

Lives_Text: Set 'NewWidth' to Lives_Txt.Value('TextSize')*Lives_Text.Value('TextAmmount')

This should be obvious if you're used to tooling around in the events already. You start your
action with the Lives_Txt object (or whatever you've called it) and set the value of NewWidth to the size of your font (represented here as TextSize)multiplied by the size of your string.

Lives_Txt: Set width Lives_Txt.Value('NewWidth')+5

Start your action and in the Size & Positions section theres a Set Width command. What we're doing is inserting the value of NewWidth that we just found, into the Set Width command. Then I tacked on a +5 to add some room for things like bold, italics, the differing widths of letters.

That should be it, other than having to do this for each Text object you want the size to update. Let me know if someone has a better way of doing this or if you found this tutorial to be unclear.
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Post » Sat Jul 18, 2009 1:47 am

I might be missing something, but why not just make it long to begin with if all you are doing is adjusting the width?
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Sat Jul 18, 2009 5:15 am

nice. If you could post an example .cap it would be even nicer.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Sat Jul 18, 2009 9:41 am

This was useful where I needed to add text with a border; dynamic text widths allow you to make things look smarter.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sat Jul 18, 2009 10:57 am

The purpose is for Dialog boxes in RPGs or something similar. Here's the cap, [url:1ofs2gml]http://www.box.net/shared/flbrtsc86h[/url:1ofs2gml] I hope it helps.

Theres a few additions, now instead of the text possibly going off screen if its too long, the box will drop down a little bit.

It simply checks if the NewWidth would be wider than a predetermined length (like the size of a background image to put behind your dialog box.) If so it drops down. Then if it is twice as much as that length, but not 3 times as much, it will drop down a second level. And if its three times as long, it will drop down a third level.

I set it up to also adjust the height of the background images, and theres a private variable for those called OrigHeight. You set that as the starting Height of your image (in the panel on the left, no need to do it in an event.) So when the Text box is adjusting its height, the image will set its height to the OrigHeight + an amount depending on how many levels the text has to adjust.

Theres a seperate event sheet to control the actual dialog, its not a very good example and theres a few kinks but I just hacked it up tonight for this demo.
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Post » Sat Jul 18, 2009 10:23 pm

This combined with the panel object can make for sweet, sweet RPG dialog.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Sun Jul 19, 2009 10:13 pm

By panel object, do you mean just the background image like I used in that cap, or a feature of Construct I haven't found yet?
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Post » Mon Jul 20, 2009 12:29 am

The Panel object is a new feature of version 0.99.4
B
2
G
3
Posts: 27
Reputation: 904

Post » Fri Jul 31, 2009 5:57 am

Currently working on an update for this, so far I have the panels in place and working, most of a new button system to look more like a console RPG and am revising the dialog section to make it into a system so you can build and call up scripts easily. (scripts in the movie sense of the word)

I'm going to leave the old link up for those who haven't switched to 9.4 but will post a new one when the dialog system is finished.
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Post » Sun Aug 02, 2009 7:30 am

http://www.box.net/shared/8zb2lnnsca

edit: I should mention its made in 99.42 so won't work with earlier versions (earlier than 99.4 probably)

Thats the new cap, the change from tiled backgrounds to panels was a simple replacement but I also added a generic dialog system that can be changed for things like letting an NPC ask a player to go on a quest.

Its pretty heavily commented, its not really a tutorial. I'm going to write that in a few days.

I'm not sure how much better this system is actually, it loads values into hash tables from an INI file, so that you get the ease of update from using a .txt as the INI file.

Later on I'll update it even more with some preprogrammed responses for the player to choose from like in a console RPG.

Ya'll let me know what you think, are the hash tables better than just getting it straight from the INI all the time or does it even out? I know INIs are supposedly slow, but what about hashes?

this isn't the finished project, but I'd really like some advice before I continue
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Next

Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 1 guest