• Godot HelpGUI
  • how to a add a sprite with code, that takes up area chosen in editor

I have a control node and I would to add an area for a sprite to go. The sprite im using is 16x16 pixels, while the area i need it to fill will be much better. How do I choose the size and location of this sprite in the editor, while still being able to actually pick the texture of the sprite from script?

Couldn't you simply add the sprite in the editor mode, then change its texture at runtime with code?

6 days later

@SIsilicon28 said: Couldn't you simply add the sprite in the editor mode, then change its texture at runtime with code?

no because I need it to be able to have the properties of a control node. The sprite is a 2D node but i need to be able to edit this sprite as if it was a control

You can add a sprite with code, but I don't think that really solves your problem. If you're having a problem with the node hierarchy in the editor, doing it in code will not be any easier. You probably have to think about how to arrange the nodes so they are in the formation you need. For example, having a control node and making the sprite a child or something along those lines.

Additionally, if the sprite is just an image, the TextureRect might work. It extends Control, so you have all of the ability to edit it as needed, but it can also display an image. You can change the texture using something like $TextureRect.texture = load("res://texture_path_here.png").

Though I agree with @cybereality, in that having a formation for the nodes and how you will need them will help, regardless of whether it is in the editor or through code.

6 days later

Thank you, textureRect was exactly what I needed

3 years later