Skip to content

Commit

Permalink
Variant.getNamed(key:)
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldeicaza committed Jan 16, 2025
1 parent cf5a44e commit 7fef9be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Sources/SwiftGodot/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ struct GodotInterface {
let variant_get_ptr_keyed_checker: GDExtensionInterfaceVariantGetPtrKeyedChecker
let variant_get_ptr_keyed_getter: GDExtensionInterfaceVariantGetPtrKeyedGetter
let variant_get_ptr_keyed_setter: GDExtensionInterfaceVariantGetPtrKeyedSetter
let variant_get_named: GDExtensionInterfaceVariantGetNamed
let get_variant_from_type_constructor: GDExtensionInterfaceGetVariantFromTypeConstructor
let get_variant_to_type_constructor: GDExtensionInterfaceGetVariantToTypeConstructor

Expand Down Expand Up @@ -342,6 +343,7 @@ func loadGodotInterface(_ godotGetProcAddrPtr: GDExtensionInterfaceGetProcAddres
variant_get_ptr_keyed_checker: load("variant_get_ptr_keyed_checker"),
variant_get_ptr_keyed_getter: load("variant_get_ptr_keyed_getter"),
variant_get_ptr_keyed_setter: load("variant_get_ptr_keyed_setter"),
variant_get_named: load("variant_get_named"),
get_variant_from_type_constructor: load("get_variant_from_type_constructor"),
get_variant_to_type_constructor: load("get_variant_to_type_constructor"),
array_operator_index: load("array_operator_index"),
Expand Down
24 changes: 23 additions & 1 deletion Sources/SwiftGodot/Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,29 @@ public class Variant: Hashable, Equatable, CustomDebugStringConvertible {
public var debugDescription: String {
"\(gtype) [\(description)]"
}


public enum VariantErrorType: Error {
case notFound
}

/// Gets the value of a named key from a Variant.
/// - Parameter key: a Variant representing the key.
/// - Returns: Result with the value on success
public func getNamed(key: StringName) -> Result<Variant?, VariantErrorType> {
var newContent: ContentType = Variant.zero
var valid: GDExtensionBool = 0

gi.variant_get_named(&content, &key.content, &newContent, &valid)
if valid != 0 {
if newContent == Variant.zero {
return .success(nil)
}
return .success(Variant(takingOver: newContent))
} else {
return .failure(.notFound)
}
}

/// Invokes a variant's method by name.
/// - Parameters:
/// - method: name of the method to invoke
Expand Down

0 comments on commit 7fef9be

Please sign in to comment.