godot icon indicating copy to clipboard operation
godot copied to clipboard

Error message asked me to make an instance of TextServer

Open MewPurPur opened this issue 1 year ago • 5 comments

Tested versions

4.2.2

System information

Pop!_OS 22.04 LTS - X11 - Vulkan (Forward+) - integrated Intel(R) Graphics (ADL GT2) () - 12th Gen Intel(R) Core(TM) i5-1235U (12 Threads)

Issue description

var shaped_text_rid := TextServer.create_shaped_text()

I wrote this and I got this loony error asking me to create an instance of TextServer: "Cannot call non-static function "create_shaped_text()" on the class "TextServer" directly. Make an instance instead."

I know this isn't correct, but the error message for sure was wrong and it took me a while to work out the problem.

Steps to reproduce

Use the above line of code.

Minimal reproduction project (MRP)

N/A

MewPurPur avatar May 14 '24 10:05 MewPurPur

The error doesn't seem wrong to me. TextServer isn't a singleton, and the method isn't static, so it can be called like this.

I'm not familiar with using it but from a quick look, there seems to be a TextServerManager singleton, and its get_interface() method should give you a TextServer instance.

I assume you found it confusing because other *Servers tend to be singletons?

CC @bruvzg

akien-mga avatar May 14 '24 11:05 akien-mga

Ooh, I see. The confusion came from the fact that TextServer is an abstract class, and you're expected to make an instance of it, which actually means you're are expected to make an instance of the inheriting classes (in this case, typically TextServerManager.get_primary_interface()).

Should we close this then?

MewPurPur avatar May 14 '24 11:05 MewPurPur

You can get an active text server instance using TextServerManager.get_primary_interface(), but in most cases you should prefer TextParagraph/TextLine/Font classes instead of using text server directly.

bruvzg avatar May 14 '24 11:05 bruvzg

I think these points would be worth clarifying in the description of TextServer in the docs.

akien-mga avatar May 14 '24 11:05 akien-mga

To clarify, what confused me is how the error asked me to instantiate a TextServer, when it's actually an abstract class that can't be instantiated, and you're meant to instantiate inheriting classes. I thought TextServer was a singleton, like the other servers I've had to use before.

MewPurPur avatar May 14 '24 11:05 MewPurPur