The Wrong Method To Load Image Sprites From GDScript - Can Someone Here Show Us The Correct Method - URL Link To GitHub Provided!

Godot Version

v4.6.2.stable.official [71f334935] - “CachyOS” KDE Plasma Linux x86_64

Question

Hi,

Our implementation to load image sprites from GDScript source code is as bad as it can get.
We really want to do it correctly so we are presenting the source code on GitHub below:
https://github.com/savantsavior/numbersfall/blob/main/project_numbersfall/VisualsCore.gd#L137

Any help would be appreciated!
Thank you!

Now that’s quite a mess!

Your initial design is currently very bloated, and could be simplified, and I recommend removing it, but for the sake of completeness:

Your code for SpriteClass could be simplified immensely.

var SpriteScreenX = []
var SpriteScreenY = []
var SpriteScaleX = []
var SpriteScaleY = []
var SpriteRotation = []

All that could be turned into a singular SpriteScale property, with a type of Transform2D, a built-in engine class that has all of these properties, and some convenience functions.
Similarly,

var SpriteColorRed = []
var SpriteColorGreen = []
var SpriteColorBlue = []
var SpriteColorAlpha = []

Could all be turned into a SpriteColor variable with type Color, another built-in engine type.
SpriteImageWidth and SpriteImageHeight are unnecessary, as Image already has get_height and get_width methods.

TextClass can be simplified in like fashion as well.

Also, a better way to store these would be to have an array of SpriteClass’s, without using arrays inside SpriteClass but instead individual properties for each Sprite.

However, I recommend that you abandon both TextClass and SpriteClass, and get accustomed to using regular Sprite2D and Label nodes. You don’t have to set up explicit code for loading, and they’re much easier to deal with in general. If there’s a reason you can’t do this, feel free to tell me!

1 Like