0 votes

I have a wall node which has the following structure:

StaticBody2D
    CollisionShape2D
    Sprite

I want to export a texture variable so that I can drag different images into the walls when I'm designing my levels. I also want to see these changes in the editor.

It wouldn't be enough to just make the children of the StaticBody2D editable so that I could drag the images into the texture field of theSprite node, because I may want to change a bunch of these nodes at once.

Here is my script:

tool
extends StaticBody2D

export(Texture) var asset setget my_func

func my_func():
    get_node("Sprite").texture = asset

This doesn't seem to work however. When I drag an image into the asset field of my nodes, nothing happens. It doesn't even change the text in the field from <null>.

How can I export a texture variable so that I can change the images in multiple of my nodes at once, and view these changes in the editor?

asked Oct 11, 2018 in Engine by Diet Estus (1,449 points)

1 Answer

+1 vote
Best answer

The set method my_func needs one argument. That argument would be your new value.

func my_func(tex):
    asset = tex
    get_node("Sprite").texture = asset
answered Oct 11, 2018 by SIsilicon (3,705 points)
selected Oct 11, 2018 by Diet Estus

The only issue now is that I get a null instance for "Sprite" when running the level scene, as well as a nil value. Any advice?

Nevermind, this works:

tool
extends StaticBody2D

export(Texture) var asset setget my_func

func my_func(tex):
    asset = tex
    if Engine.editor_hint:
        get_node("Sprite").texture = asset

func _ready():
    get_node("Sprite").texture = asset
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.