This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I want to make a mesh preview. I created a function that returns a texture for a button with TextureRect, but it returns a gdscript function state.

func render_thumbnail(_mesh, size, screen):
    var new_viewport = viewport.instance()
    new_viewport.size = screen
    add_child(new_viewport)
    var camera = new_viewport.find_node("Camera")
    camera.size = size
    if _mesh:
        var mesh = new_viewport.find_node("Mesh")
        mesh.set_mesh(_mesh)

    var thumbnail = ImageTexture.new()
    for a in 2: yield(get_tree(), "idle_frame")
    var image = new_viewport.get_texture().get_data()
    thumbnail.create_from_image(image)
    new_viewport.queue_free()
    return thumbnail

It seems to me that this is due to yield (), but I do not know how to fix this or how to replace it...

in Engine by (38 points)
edited by

2 Answers

+1 vote
Best answer

Yes this is because of yield.

I think the simplest way is to declare a signal for when the thumbnail render has finished and use that instead of return
All in one class:

signal thumbnail_finished(thumbnail)

func _ready():
    self.connect("thumbnail_finished", self, "_process_rendered_thumbnail")

func _render_thumbnail(_mesh, size, screen):
    # all the code in the render_thumbnail function except for the last line
    emit_signal("thumbnail_finished", thumbnail) # instead of return thumbnail

func _process_rendered_thumbnail(thumbnail):
    # This function gets called when the thumbnail has finished rendering
    # Do stuff
    pass
by (1,519 points)
selected by
0 votes

You can still keep the yield!

If you don't want to change a code that you wrote just to get the right return value, or you just don't want to use extra signals you can just use another yield when calling the function!


So in your case you can say;

var thumbnail = yield(render_thumbnail(mesh, size, screen), "completed")

important part is "completed" it will only set thumbnail to render_thumbnail when it returns an actual value!

by (60 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.