UE1 - UT How to center Fonts using Canvas with either width or number of fonts

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

War_Master

Member
May 27, 2005
702
0
16
I'm making a monster radar for the MonsterHunt mod that displays the name and health of a monster. I got the script working correctly and placed on top of the pawn starting from the center, so my question is, how do I calculate the width of a font so that each letter/number gets placed in the center depending on its pixels using the SetPos(float X,float Y) function from Canvas?

I have searched and looked at other mods for weeks and I found nothing about it or even close. All I found was how to center a normal texture since a texture have variables for it but not a font texture. So please, if you know how or a way to center fonts post an example or let me know how else it can be implemented. Any ideas or discussion will help.
 

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
Well, Canvas has a bCenter property, but I'm not sure how it works. It seems to center the text across the entire screen.

If you want to center text manually, try the following:
Code:
local float XL, YL;

C.StrLen(Text, XL, YL);
C.SetPos(X - 0.5 * XL, Y - 0.5 * YL);
C.DrawText(Text);
Remember to set the desired font before obtaining the string size.
 

War_Master

Member
May 27, 2005
702
0
16
I was messing with the bCenter option before I read this and I encountered the same problem you mentioned Wormbo.



I tried the script you posted and worked like a charm. After seeing several 3d radars displaying text for over 10 years mine was the first time that automatically centered the whole thing like I always wanted it to. Seeing how nice it looks I just hoped that all modders in the past knew about this, specially because there are some awful looking monster radars in MH.

Also, I was having some glitch before where the text looked well centered from far but as I got close to the monster the text will move all the way to the left or right. This problem was also happening whenever I rotated my view from the monster or when I moved around it. It seems that what was causing it was that I was displaying an icon in the middle of its collision which I had to use another SetPos() to get it centered. After I deleted that part the glitch stopped and the text displayed properly from any angle or position.


Well, a big thanks to you for your help and I'll post again if there's any other questions or problems.
 
Last edited: