Double Sept
Double Sept

Reputation: 326

How to get a Format enum in Godot from Image data dictionary?

I'm trying to send a texture with rpc in Godot using the data dictionary of an image when sending and the create_from_data on the receiving side but I cannot automate the "Format" argument stored as a string in the dictionary.

Sending code:

func _get_texture_for_rpc(texture):
    return texture.get_image().data

Receiving code:

@rpc("any_peer", "call_remote", "reliable")
func _set_texture_rpc(texture):
    received_texture = ImageTexture.create_from_image(Image.create_from_data(
            texture.width,
            texture.height,
            texture.mipmaps,
            texture.format,
            texture.data
        ))

But the texture.format is not working as it is a string and not a member of the Format enum in Image.

I can achieve it by using always the same format (RGBA8) on the receiving side and converting to it before sending but is there a solution to have a String to Enum conversion in Godot.

Aka, having "RGBA8" <=> Format.FORMAT_RGBA8

Upvotes: 0

Views: 42

Answers (1)

Bugfish
Bugfish

Reputation: 1729

I am not aware of a way to even get these texts from the enum. So I would alter the data dictionary before sending. Replacing the format string with the enum int value, since image.get_format() returns the enum value.

So it would look like this

func _get_texture_for_rpc(texture):
    var image = texture.get_image()
    var data = image.data
    data.format = image.get_format()
    return data

_set_texture_rpc can stay unchanged than.

If you find a way to get all string representations, you could write a match/case function, or create your own dictionary to map the strings to the enums. But I really don't see a way to get these strings from the enum.

Upvotes: 1

Related Questions