Whats faster, global_position, or position

Exactly as the title says. Is it faster to get a nodes global_pos or normal position. It doesn’t matter which one in my case. Also which one is faster to set?

it depends more if you’re taking the value from the node where the script is attached, or if you are getting the value of other node in the scene.

If you’re getting the value of other node, you will have to do get_node() or use $ ($ is a abreviation for call the get_node function, the engine is incumbed to translate it), so it will be slower than getting the value from the self reference as you will have to call a function that will get and return a value to you.

If you are getting the value from the node that has the script attached to it, it will have the same time to retrieve both the values as they are just attributes in an instance.

In both cases it’s take the same time, but when you use get_node() it’s slower because you have to call a function for it.
Here an explanation about it:
https://softwareengineering.stackexchange.com/questions/318055/how-much-do-function-calls-impact-performance

Note that calling a function start impacting a software when you have too many calls for it. Like in a scenary that you call a function in a while loop (_process counts as a while loop). It’s not a thing that needs to be avoided, just be careful and you will be fine.

Other things that can impact global_position and position are in their use for how the engine process them for the two coordinate spaces, it could impact performance, but I would not consider as the difference is despicable.

If you are facing performance problems, I recommend you take care with what operations are you doing and where you are doing. An recursive function in _process() can drop performace easely if not being take in consideration.

For getting, the data is calculated and cached anyway they are approximately the same. Setting the position will be faster.

Here’s the engine code for 4.3 of the relevant functions

// GETTING
Point2 Node2D::get_position() const {
	ERR_READ_THREAD_GUARD_V(Point2());
	if (_is_xform_dirty()) {
		_update_xform_values();
	}

	return position;
}

Point2 Node2D::get_global_position() const {
	ERR_READ_THREAD_GUARD_V(Point2());
	return get_global_transform().get_origin();
}

// SETTING
void Node2D::set_position(const Point2 &p_pos) {
	ERR_THREAD_GUARD;
	if (_is_xform_dirty()) {
		_update_xform_values();
	}
	position = p_pos;
	_update_transform();
}

void Node2D::set_global_position(const Point2 &p_pos) {
	ERR_THREAD_GUARD;
	CanvasItem *parent = get_parent_item();
	if (parent) {
		Transform2D inv = parent->get_global_transform().affine_inverse();
		set_position(inv.xform(p_pos));
	} else {
		set_position(p_pos);
	}
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.