extends JSONProperty
returns: Image
Only allows strings representing a path to an image. This path can be absolute or relative. When the file path is relative, it would be relative to the directory that contains the JSON configuration file that specifies this field. Please, use '/' as a path delimiter. Also, check Godot's supported image formats.
Once you have instantiated the 'JSONPropertyImage' class, you can determine the size of the image via the 'set_size' method. By default, the images would be resizable and rescaled using Image.INTERPOLATE_BILINEAR. That means that when the image size is different, this property will raise a warning. If you want it to raise an error, set the third parameter in 'set_size' to 'false'.
In this example, the configuration structure has one required property. The property 'image' must be a path to an image which size should be 64x64.
# Create a JSON configuration file
var json_config_file = JSONConfigFile.new()
# Create an image property
var image_property = JSONPropertyImage.new()
image_property.set_size(64, 64)
# Add the 'image' property, which is a path to an image
json_config_file.add_property("image", image_property)
# Validate input
json_config_file.validate(json_file_path)
This JSON has the required field.
{
"image": "image.png"
}
This JSON contains one error. The 'image' property is not the correct type.
{
"image": 42
}
Returned error:
[
{
"error": JSONProperty.Errors.WRONG_TYPE,
"expected": JSONProperty.Types.IMAGE,
"context": "image",
"as_text": "Wrong type: expected 'image path', at 'image'."
}
]
This JSON contains one error. The 'image' property indicates a path to an image that does not exist.
{
"image": "missing_image.png"
}
Returned error:
[
{
"error": JSONProperty.Errors.COULD_NOT_OPEN_IMAGE,
"code": ERR_FILE_NOT_FOUND,
"context": "image",
"as_text": "Could not open the image, at 'image'."
}
]
This JSON contains one warning. The 'image' property indicates a path to an image which size is not the desired one.
{
"image": "32x32_image.png"
}
Returned warning:
[
{
"warning": Warnings.IMAGE_WRONG_SIZE,
"expected_size": [64, 64],
"size": [32, 32],
"context": "image",
"as_text": "The image is not the correct size (64, 64), at 'image'."
}
]
The public methods of this class are:
Name | Params | Description | Returns |
---|---|---|---|
set_preprocessor | processor -> JSONConfigProcessor: Object that defines the function to execute before the validation process. |
Sets the process to execute before the validation process. | Nothing. |
set_postprocessor | processor -> JSONConfigProcessor: Object that defines the function to execute after the validation process. |
Sets the process to execute after the validation process. | Nothing. |
set_size | width -> int: Width of the image. height -> int: Height of the image. resizable -> bool(true): Determines if the image is resizable. If it is, it will raise a warning whenever the size of the input image is different than this size, and then it will resize the image. If it is not, it will raise an error instead. interpolation -> int(Image.INTERPOLATE_BILINEAR): The interpolation technic to apply whenever the size of the input image is different, and it must be resized. NOTE: Check Image.Interpolation for more information. |
Sets the recommended, or required, size of the image. | Nothing. |
This class could directly raise any of the following errors:
Enum value | Description | Params | As text |
---|---|---|---|
WRONG_TYPE | The type of the input does not match the expected one. | expected -> int: Takes the value IMAGE. |
Wrong type: expected 'file path' |
COULD_NOT_OPEN_IMAGE | The image path leads to a nonexistent image or an error occurred when loading the image. | code -> int: Error code returned by Image.load(). |
Could not open the image |
IMAGE_WRONG_SIZE | The image has not the correct size. | expected_size -> array: Array with two integers representing the expected size of the image. size -> array: Array with two integers representing the actual size of the image. |
The image is not the correct size ({expected_size}) |
This class could directly raise any of the following warnings:
Enum value | Description | Params | As text |
---|---|---|---|
IMAGE_WRONG_SIZE | The image has not the correct size. | expected_size -> array: Array with two integers representing the expected size of the image. size -> array: Array with two integers representing the actual size of the image. |
The image is not the correct size ({expected_size}) |