0 votes
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

Godot version 3.3
in Engine by (80 points)
edited by

1 Answer

0 votes
Best answer

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:

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().

by (3,882 points)
selected by

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.

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.
get() expects a String that is the name of a property of the node it is used on e.g. get("position").

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))
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:

    # 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.