DataContainer

Inherits: Resource < Reference < Object

A Resource which holds any Variant compatible type.

Description

This class allows to store any Variant compatible built-in types as a Resource, such as Vector2, Dictionary, Array, and even nested DataContainer objects themselves. These can be edited in the inspector and saved to disk to be reused throughout the project, as resources are shared between instances by default.

DataContainer is normally edited via the editor inspector, but the type and value can be changed via code as well:

var res = DataContainer.new()
res.value = Vector3(1, 2, 3) # The type is set automatically.
print(res.value)  # prints (1, 2, 3)
res.type = TYPE_VECTOR2 # The previous value is converted to a new type.
print(res.value)  # prints (1, 2)

The conversion logic is mostly equivalent to @GDScript.convert, except that the @GlobalScope.TYPE_NIL is never automatically converted to other types, and a new empty Variant is constructed instead.

Methods

Variant get_value ( ) const
void set_value ( Variant value )

Property Descriptions

Default 0
Setter set_property_hint(value)
Getter get_property_hint()

Specifies how the value is represented in the editor, one of PropertyHint values.


Default ""
Setter set_property_hint_string(value)
Getter get_property_hint_string()

Configures property_hint.


Default "value"
Setter set_property_name(value)
Getter get_property_name()

Specifies the value’s property name. By default, the data can be fetched via code by referencing the value property, but this can be customized. Prefer to use the implicit default value property, unless you’re not sure whether the property’s name is customized.


Default 7
Setter set_property_usage(value)
Getter get_property_usage()

Specifies how the value should be used throughout the editor and code, a combination of PropertyUsageFlags values.


Default 0
Setter set_type(value)
Getter get_type()

Sets the type of the Variant, one of the TYPE_* constants available at @GlobalScope, such as @GlobalScope.TYPE_INT.

Once the type is set, an implicit Variant value property is constructed. The value property can be changed dynamically anytime, and this emits the Resource.changed signal, which can be connected to other script or engine methods:

extends Node2D

export(DataContainer) var res = DataContainer.new()

func _ready():
    # Whenever the color is changed, redraw the canvas.
    res.connect("changed", self, "update")
    # This emits the "changed" signal above.
    res.value = Color.blue

func _draw():
    if res.type == TYPE_COLOR:
        draw_circle(Vector2(), 100, res.value)

Method Descriptions

Returns Variant data associated with this resource. The method is recommended to use over Object.get as the property’s name may be customized with property_name.


Modifies existing value of this resource. The value’s type is updated automatically if types differ. The method is recommended to use over Object.set as the property’s name may be customized with property_name.