Position is different than what is displayed

Godot Version

v4.2.1

Question

`I have code that creates an instance of a Slot. When i drag a Card on top of it, it seemingly does not go on top of the slot. But, when i printed out the positions, the card and the slot are apparently on top of each other. Is this a bug that I can fix?


Slot Code =

func _process(_delta):
	
	
	if self.get_child_count() > 0:
		var card = self.get_children()[0]
		print("------------------------")
		print(card.position)
		print(position)
		card.position += (position - card.position) /2
		print(card.position)

Card Code:

func _process(_delta):
	if IsClicked == true:
		var MousePosition = get_global_mouse_position()
		position += (MousePosition - position) / 2		

func _on_button_button_down():
	IsClicked = true
	print("Clicked!")
func _on_button_button_up():
	IsClicked = false
	print("Released!")
	var CardSlots = get_node("/root/fight/CardSlots").get_children()
	var ClosestSlot 
	var ClosestSlotDistance
	for slot in CardSlots:
		var slotPosition = slot.position
		var slotDistanceVector2 = abs(slotPosition - position)
		var slotDistance = sqrt(slotDistanceVector2.x**2 + slotDistanceVector2.y**2)
		if (ClosestSlot == null or slotDistance < ClosestSlotDistance) and "cardSlot" in slot.name:
			ClosestSlot = slot
			ClosestSlotDistance = slotDistance
	print(ClosestSlot.name)
	get_parent().remove_child(self)
	ClosestSlot.add_child(self)

Instancing Code:

var cardSlotPrefab = preload("res://prefab/card_slot.tscn")
var baseTilePrefab = preload("res://prefab/base_tile.tscn")

var BoardPreset = [
	[1,2,"baseTile",1],
	[2,1.5,"cardSlot",1],
	[2,2.5,"cardSlot",1],
	[3,1,"cardSlot",0],
	[3,2,"cardSlot",0],
	[3,3,"cardSlot",0],
	[4,1,"cardSlot",0],
	[4,2,"cardSlot",0],
	[4,3,"cardSlot",0],
	[6,2,"baseTile",2],
	[5,1.5,"cardSlot",2],
	[5,2.5,"cardSlot",2],]

func _ready():
	var highestXY = Vector2()
	var lowestXY = Vector2()
	var centeredOffset = DisplayServer.screen_get_size() / 2
	
	for i in range(len(BoardPreset)):
		var tile = BoardPreset[i]
		var instance
		var type = tile[2]
		var _team = tile[3]
		var tileX = tile[0]
		var tileY = tile[1]
		
		
		if (highestXY.x == null or highestXY.x < tileX):
			highestXY.x = tileX 
		if (highestXY.y == null or highestXY.y < tileY):
			highestXY.y = tileY
			
		if (lowestXY.x == null or lowestXY.x > tileX):
			lowestXY.x = tileX 
		if (lowestXY.y == null or lowestXY.y > tileY):
			lowestXY.y = tileY
		
		
		
		if type == "cardSlot":
			instance = cardSlotPrefab.instantiate()
			instance.name = "cardSlot"+str(i)
		elif type == "baseTile":
			instance = baseTilePrefab.instantiate()
			instance.name = "baseTile"+str(i)
		
			
		instance.position.x = tileX * 134 
		instance.position.y = tileY * 134 
		get_node("CardSlots").add_child(instance)
		
	print(highestXY,"BEFORE")
	highestXY.x -= lowestXY.x
	highestXY.y -= lowestXY.y
	print(highestXY,"after")
	centeredOffset.x -= highestXY.x * 33.5
	centeredOffset.y -= highestXY.y * 33.5
	
	var CardSlots = get_node("CardSlots")
	
	for slot in CardSlots.get_children():
		slot.position.x += centeredOffset.x
		slot.position.y += centeredOffset.y

`

I dont see why your card doesnt show, but i recommend to use the Godot built-in Drag’n’Drop sytem

1 Like

Check the ordering of the nodes under the Visibility section, and increase the order of the one you want on-top.
There in the same section is also a ‘Always On Top’ option you can try enabling which should always make it visible above the other nodes.

1 Like

I found out that i had accidentally moved the root node. That messed everything up.

1 Like