How to use get() on a vector components?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Multirious
get("position") # Returns the expected value.

get("position:x") # Returns null

From the doc, I have to use ":x" to get to a vector component but using it on get() just simply returns null.

Edit: From the comment instead do get("position").x

:bust_in_silhouette: Reply From: timothybrentwood

get() is more used for times when you don’t necessarily know what kind of node you’re working with so you don’t cause crashes by accessing a property that doesn’t exist. Example:

for child in get_children():
    var child_position = child.get("position")
    if child_position != null:
        do_something_with_x_component(child_position.x)

If you know what node you’re working with your should either access the property itself using node.property or if it is the node that the script is attached to: simply property or self.property (which calls the getter/setter).

You more than likely want to use self.position.x, position.x or some_node.position.x rather than get().

when you don’t necessarily know what kind of node you’re working with

Well, this is actually the reason I’m trying to use get() but instead, it’s not knowing what property I am working with.

Multirious | 2021-05-18 15:48

I’m not sure I follow. If you want to access the x component of a Vector2 and you’re not certain if the node you are accessing has a position property use this:

var my_node_position = my_node.get("position")
if my_node_position is Vector2: # can also use != null
   print(my_node_position.x) # prints the x component of the my_node's position if it exists

The "position:x" qualifier that you were attempting to use is used for NodePaths.
NodePath — Godot Engine (stable) documentation in English
get() expects a String that is the name of a property of the node it is used on e.g. get("position").
Object — Godot Engine (stable) documentation in English

timothybrentwood | 2021-05-18 16:26

Edit: Actually I got this now, I just need to change from
var path_x = str(property) + ":x"
input_x.text = str(connected_var.get(node_path_x))
to
input_x.text = str(connected_var.get(property).x)
Thanks for helping me out!
Just deeply reading your code and reminding me about .x

Alright, I am sorry for not quite very clear, not very good at talking, but I want to create some kind of vector input like in Godot inspector.
Vector Input GUI

When you enter the object and vector property path into this function.

var connected_object: Object
var connected_property: NodePath

func connect_property(object: Object, property: NodePath):
    # This var for to use in others function
    connected_object = object
    connected_property = property
    
    # Checks if the object or property path is null
    if not connected_var or not connected_property:
    		return
    
    # Split the path into x and y
    # If use "position" as a path
    var path_x = str(property) + ":x"#Should get "position:x"
    var path_y = str(property) + ":y"#Should get "position:y"
    
    # Finally should be displaying the property to the input but got null.
    input_x.text = str(connected_var.get(node_path_x))
    input_y.text = str(connected_var.get(node_path_y))

This function should be getting any Vector2 properties to display it to the UI, but display null.
All I have to do is figure out to split the Vector2 correctly; get() don’t works with “:x”. Please help me fix this or got another idea that works too.

Apologize in advance if you can’t understand this.

Multirious | 2021-05-18 17:00