Centering Text Objects has been something that i’v always found difficult to do in GDevelop… but as it turns out… its quite simple!
The main reason iv struggled with this is because of how Text Objects are different from regular Sprite Objects… like the fact that they dont have “Points” or a “Body” to them.
When i looked up how to “Center Text Objects”, i didnt really find anything about how to do it… only why they couldnt be treated the same as other objects…
Then i had the idea of adjusting the “Width” of a text object and use that into calculations, but… you cant adjust the “Width” of a Text Object with an Action, because they dont work like normal sprites… so i gave up on it and decided to make a feature request about this…
But today i decided to try again and give it some more thought… and i had an idea!
GDevelop cant change the Height and Width of a Text Object… but does the engine know the Hight and Width of the object after its created?
The answer is… YES!
Even if the engine cant change these parameters with an action, it can still tell you their values and use them for positioning! My mind = Blown
So now centering an object becomes quite easy!!
To center the Text Object on an Objects “Point”
For example, a text over a Player character head…
- Create object “TextObject” at position “Player.CenterX()” “Player.Y()-64”
- Change X position of “TextObject” “set to” "Player.CenterX()-(TextObject.Width()/2)
…before checking for the Text Objects Width and adjusting the position we need to first create the object, otherwise GDevelop has no way of know its Width.
The example bellow has the “Player” as “Rogue” and the Text Object as “LevelUpField”.
To center the Text Object on the screen instead of an object is just as simple!
Lets center a text in the middle of a “HUD” layer…
- Create object “TextObject” at position “CameraCenterX(“HUD”, 0)” "CameraCenterY(“HUD”, 0)
- Change the X position of “TextObject” to CemeraCenterX(“HUD”, 0)-(TextObject.Width()/2)"
- Change the Y position of “TextObject” to CemeraCenterY(“HUD”, 0)-(TextObject.Height()/2)"
…maybe this stuff is some sort of common knowledge, but in case theres more people out there like me that have been struggling to make this happen, i decided to share my findings!
Hope this can help you!
If theres anything about this that didnt make sense, let me know and ill try to explain things better