How do i see the global position of a node?

if link in future will be down there code:

  • res://addons/GlobalPosition2DGS/global_position_control.gd
@tool
extends GridContainer
@export var _line_editX: LineEdit
@export var _line_editY: LineEdit
var object : Node2D

func _set_object(new_object : Node2D) -> void:
	object = new_object

func _ready() -> void:
	_line_editX.text_submitted.connect(_edit_x)
	_line_editY.text_submitted.connect(_edit_y)

func _process(delta: float) -> void:
	if(!_line_editX.has_focus()):
		_line_editX.text = str(object.global_position.x)
	if(!_line_editY.has_focus()):
		_line_editY.text = str(object.global_position.y)

func _edit_x(new_x : String) -> void:
	if(new_x.is_valid_float()):
		object.global_position = Vector2(new_x.to_float(),object.global_position.y)
		return
	_line_editX.text = str(object.global_position.x)
	

func  _edit_y(new_y : String) -> void:
	if(new_y.is_valid_float()):
		object.global_position = Vector2(object.global_position.y,new_y.to_float())
		return
	_line_editY.text = str(object.global_position.y)
	
  • res://addons/GlobalPosition2DGS/global_position_inspector_plugin.gd
@tool
extends EditorInspectorPlugin

func _can_handle(object: Object) -> bool:
	if(object is Node2D):
		return true
	return false

func _parse_property(object: Object, type: Variant.Type, name: String, hint_type: PropertyHint, hint_string: String, usage_flags: int, wide: bool) -> bool:
	if(name == "position"):
		var _custon_control := preload("res://addons/GlobalPosition2DGS/GlobalPosition.tscn").instantiate()
		add_custom_control(_custon_control)
		if(_custon_control.has_method("_set_object")):
			_custon_control._set_object(object)
	
	return false


  • res://addons/GlobalPosition2DGS/global_position_plugin_2d.gd
@tool
extends EditorPlugin

var plugin

func _enter_tree() -> void:
	plugin = preload("res://addons/GlobalPosition2DGS/global_position_inspector_plugin.gd").new()
	add_inspector_plugin(plugin)
func  _exit_tree() -> void:
	remove_inspector_plugin(plugin)

  • res://addons/GlobalPosition2DGS/plugin.cfg
[plugin]

name="GlobalPosition2D with GDScript"
description="This plugin adds the global position to the inspector for all Node2D nodes."
author="Moreus"
version="0.1"
script="global_position_plugin_2d.gd"

obraz

1 Like