GetTextWidth (solved)

Started by Lufia, Mon 19/10/2009 00:36:42

Previous topic - Next topic

Lufia

Hey, me again.

I'm setting up a simple system where hotspot names are displayed nex to the mouse cursor. I have a problem to check whether the label would end past the right edge of the screen.

Here's the relevant code from my repeatedly_execute function :

Code: ags
lblHotspotLabel.Text = String.Format("@OVERHOTSPOT@");
largeur = GetTextWidth(lblHotspotLabel.Text, eFontTahomaOutline);
lblItemLabel.Text = String.Format("%d", largeur);
if (mouse.x+largeur >= 800) 
  gLabels.SetPosition(mouse.x - largeur, mouse.y + 15);
else gLabels.SetPosition(mouse.x + 10, mouse.y + 15);
gLabels.Visible = true;


largeur is always equal to 109 no matter what's under the mouse cursor. (Well, it changes if I change the font passed as an argument for GetTextWidth.)
I've tried various things, like pass String.Format("@OVERHOTSPOT@") or String.Format("%s", lblHotspotLabel.Text) as arguments for the GetTextWidth function but to no avail. The label, on the other hand, displays the correct text.

What am I doing wrong?

Khris

It seems that when the line is run, it always calculates the width of "@OVERHOTSPOT@".
Try setting the label's text in the editor; it doesn't need to be set every game loop.
You can also try using GetLocationName instead.

Lufia

QuoteTry setting the label's text in the editor; it doesn't need to be set every game loop.
I'm using the same label for characters and objects, so it needs to be set in a loop.

Anyway, setting the text in the editor to work with hotspots did nothing to improve matters. Using GetLocationName does, though. That's actually easier, I can use one single condition for any interaction now.

Thanks Kris.

monkey0506

Quote from: Lufia on Mon 19/10/2009 01:57:52
QuoteTry setting the label's text in the editor; it doesn't need to be set every game loop.
I'm using the same label for characters and objects, so it needs to be set in a loop.

To clarify this, if you're using "@OVERHOTSPOT@" there is no reason or justification to setting the text in a loop. The actual displayed text is calculated in real-time by the engine. It's not determined at the time the label text is set.

However, if you're using Game.GetLocationName then it will work at the time the text is set, which allows you then to dynamically get the width of the text.

If the label text is @OVERHOTSPOT@ the Label.Text property will always contain the text "@OVERHOTSPOT@" unless you manually change it. The only place you will get the actual character/hotspot/object/inventory item name by using @OVERHOTSPOT@ is in the displayed text, it cannot be retrieved this way from the script.

So if you need the text in the script (for dynamic sizing, positioning, etc., etc.) then use Game.GetLocationName instead. ;)

SMF spam blocked by CleanTalk