(Get it from Godot Asset Library - Dataclasses on AssetLibrary)
- Customization options
- Create object from Dictionary
- Serialize to Dictionary
- Better printing of object
-
Search for "Dataclasses" in the
AssetLib
tab in Godot and click Download. Once it has downloaded click Install. In the popup that opens, click Install again. (You dont need to enable the plugin in ProjectSettings) -
Copy the contents of
addons/dataclasses-godot
into theaddons/
folder in the same directory as your project. (You dont need to enable the plugin in ProjectSettings)
# Main.gd
extends Control
class MyClass:
extends Dataclass
func _init().("MyClass"): pass
# Compulsory
var some_int: int
var some_array: Array
# Optional
var optional_dict = null
func _ready():
# Better printing of objects
var obj = MyClass.new()
obj.optional_dict = {foo = "bar"}
print(obj)
# Create object from Dictionary
print(MyClass.new().from_dict({some_int = 123456789}))
# Serialize to Dictionary
print(MyClass.new().from_dict({some_array = [1,2,3,4, 5]}).to_dict())
Type | Name | Defaults | Description |
---|---|---|---|
String | _name_ | "" | Name of the class to display when printing |
String | _options_ | Default values mentioned in Customization | Options to customize behaviour of the dataclass |
Returns | Definition | Description |
---|---|---|
self | from_dict(dict: Dictionary) | Create object from Dictionary |
Dictionary | to_dict() | Serialize object to Dictionary |
Classes that inherit from Dataclass have the is_dataclass
meta set to true
-
sort_keys: true
Whether to sort properties when printing
-
include_null_in_dict: true
Whether to include properties with null values in the to_dict() method
-
include_null_in_print: true
Whether to include properties with null values when printing
-
print_newline: false
Whether to print properties on newlines when printing
-
print_exclude: []
Names of properties to exclude when printing
- Print properties of the object on newline
# Main.gd class MyClass: extends Dataclass func _init().("MyClass", {print_newline = true}): pass var some_int: int var some_array: Array func _ready(): print(MyClass.new())
- Want to make Discord Bots in Godot? Use discord.gd
- Add multiplayer and stats with Epic Online Services Godot
- Show your Godot Editor in your Discord Presence using this plugin
Want to support in other ways? Contact me on Discord: @3ddelano#6033
For doubts / help / bugs / problems / suggestions do join: 3ddelano Cafe Discord server