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

jackhenjackhen Posts: 11Member

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?


  • SIsilicon28SIsilicon28 Posts: 703Moderator
    edited April 28

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

  • jackhenjackhen Posts: 11Member
    edited May 4

    @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

  • cyberealitycybereality Posts: 927Moderator

    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.

  • TwistedTwiglegTwistedTwigleg Posts: 2,635Admin

    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.

  • jackhenjackhen Posts: 11Member

    Thank you, textureRect was exactly what I needed

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file