Godot: Expose private method Node::_get_node to GDNative

Created on 5 Jan 2019  路  7Comments  路  Source: godotengine/godot

Right now, if you call get_node("name") on a node and "name" does not exist, the function throws an error, so you have to use code like if (has_node("name")) auto node = get_node("name");. The problem with this is that both functions call _get_node() so you end up searching for the node twice, which is inefficient (and a bit silly).

I understand that get_node is used in GD scripts with the special syntax and in these cases the error is useful to prevent typos and so on, but in certain cases, especially when using GDNative with C++, this is quite annoying.

We could add a separate function but honestly the easiest thing to do is to simply expose _get_node itself to GDNative so you can call this function when you want to "check if node exists, if so return it" in one go without generating errors.

enhancement gdnative

Most helpful comment

We should definitely add a get_node_or_null, this was discussed but never added..

All 7 comments

get_node is returning null when it can not find the node.
Therefore if you use it something like this:

var node = get_node("thing")
if node:
    node.dosomething()

it shouldn't be a problem?

Yes, that's what I use. Unfortunately, get_node generates an error when the node does not exist. In fact, get_node is just a wrapper to _get_node that generates an error if _get_node returns null.

It would be confusing to expose it as _get_node, but something like get_node_nocheck or get_node_or_null (as proposed in an earlier issue IIRC) could work.

I did search for old issues but the only one I could find any specific for this. I personally don't find it confusing to expose it as _get_node but I'm fine with any other name.

We should definitely add a get_node_or_null, this was discussed but never added..

I marked this fixed as Node::get_node_or_null is now exposed for C++ and scripting languages, but should anything specific be synced for GDNative to have it too @karroffel?

No, should all work since this is only a change to the Godot API which is autogenerated for GDNative languages :)

Was this page helpful?
0 / 5 - 0 ratings