-
-
Notifications
You must be signed in to change notification settings - Fork 21k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add static methods for loading images #23782
Comments
I'm not sure but I think the variant system "automagically" turns an image into a texture behind the scenes. I have code like this var texture = load("path/to/image.png")
$Sprite.texture = image and it works just fine like that... |
Yeah, but it works like this only if the texture is imported as a part of your project. If you want to load external texture, you need to use the code I provided. |
Gotcha, what about just creating your own wrapping function then? func load_texture(path):
var image = Image.new()
image.load(path)
var texture = ImageTexture.new()
return texture add it to a global singleton script or however you want to do it :) Yeah, I get your point about having a built-in static function, but does that go along with the coding style and the way the engine works? |
@TheCire Power of GDScript! I love doing mini wrapper functions like that. I feel the OP's sentiments however, sometimes I wish there was an easier way to do something, but then realize you can really achieve a lot of stuff in GDS by itself |
Well, I know I can make a mini-wrapper (I actually did that). But then I have to copy it over to all projects I'd need it. Also I can't just have utility script with everything, because I don't always need all these functions and it's project-specific a lot >_> My proposition seems logical and fixes the issue without workarounds, but maybe I'm just used to having language work that way. |
Looking at your code from your OP @KoBeWi , looks like Image should have a static initializer like so: var image = Image.load("path/to/image.png") instead of having to do: var image = Image.new()
image.load("path/to/image.png")
#etc |
#20972 |
There seem to be little interest in this and I actually needed it in just one or two projects, so closing. There's a slightly related proposal though godotengine/godot-proposals#1101 |
Right now, if you want to assign an arbitrary image as a texture, you need to do this:
This is a lot of code for simple operation. I know that
load
is supposed to return error code, but it can be done smarter. There could be static versions of bothload
andcreate_from_image
, which return null if load fails (as simple as that). The above code could then become this:$Sprite.texture = ImageTexture.create_from_image(Image.load("some/path.png"))
I know this is unsafe code, but there are people like me who don't mind unsafe code. Well, the unsafeness could be mitigated at all if trying to create from null image would result in a warning instead of error and simply return null.
There are probably more classes that would benefit from more static methods btw.
The text was updated successfully, but these errors were encountered: