Skip to content

Commit

Permalink
Merge pull request #448 from xzirox/main
Browse files Browse the repository at this point in the history
added support for converting stringValues to and from Vector2 & Vector2i
  • Loading branch information
WolfgangSenff authored Dec 25, 2024
2 parents 52c17c9 + 30c7243 commit bac6dbd
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions addons/godot-firebase/Utilities.gd
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ class FirebaseTypeConverter extends RefCounted:
"nullValue": _to_null,
"booleanValue": _to_bool,
"integerValue": _to_int,
"doubleValue": _to_float
"doubleValue": _to_float,
"vector2Value": _to_vector2,
"vector2iValue": _to_vector2i
}

func convert_value(type, value):
if converters.has(type):
return converters[type].call(value)

return value

func _to_null(value):
Expand All @@ -78,6 +79,12 @@ class FirebaseTypeConverter extends RefCounted:

func _to_float(value):
return float(value)

func _to_vector2(value):
return type_convert(value, TYPE_VECTOR2)

func _to_vector2i(value):
return type_convert(value, TYPE_VECTOR2I)

static func from_firebase_type(value):
if value == null:
Expand All @@ -91,7 +98,13 @@ static func from_firebase_type(value):
value = Time.get_datetime_dict_from_datetime_string(value.values()[0], false)
else:
var converter = FirebaseTypeConverter.new()
value = converter.convert_value(value.keys()[0], value.values()[0])
var type: String = value.keys()[0]
value = value.values()[0]
if type == "stringValue":
var split_type: String = value.split("(")[0]
if split_type in [ "Vector2", "Vector2i" ]:
type = "{0}Value".format([split_type.to_lower()])
value = converter.convert_value(type, value)

return value

Expand All @@ -115,7 +128,10 @@ static func to_firebase_type(value : Variant) -> Dictionary:
TYPE_ARRAY:
var_type = "arrayValue"
value = {"values": array2fields(value)}

TYPE_VECTOR2, TYPE_VECTOR2I:
var_type = "stringValue"
value = var_to_str(value)

return { var_type : value }

# Pass the .fields inside a Firestore Document to print out the Dictionary { 'key' : 'value' }
Expand Down Expand Up @@ -343,4 +359,4 @@ class AwaitDetachable extends Node2D:
func _init(freeable_node, await_signal : Signal) -> void:
awaiter = await_signal
add_child(freeable_node)
awaiter.connect(queue_free)
awaiter.connect(queue_free)

0 comments on commit bac6dbd

Please sign in to comment.