Teleport closest node?

Godot Version

Godot 4.5.1

Question

So I’m coding an inventory system for my game, but when I run the below script, it teleports ALL the items to the slot node. How do I only teleport the nearest?

Item Code:

extends Node2D
var mouse_is_in = 0

func _ready() -> void:
	pass


func _process(_delta) -> void:
	if mouse_is_in == 1:
		scale = Vector2(0.22, 0.22)
		if Input.is_action_pressed("Click"):
			self.global_position = get_global_mouse_position()
	if mouse_is_in == 0:
		scale = Vector2(0.2, 0.2)


func _on_area_2d_body_entered(body) -> void:
	print("Entered")
	if Input.is_action_pressed("Click"):
		self.global_position = get_global_mouse_position()


func _on_area_2d_mouse_entered() -> void:
	mouse_is_in += 1


func _on_area_2d_mouse_exited() -> void:
	mouse_is_in = 0

And this is the slot code:

extends Area2D
var is_in = 0
@onready var Item = $“../Item”

func _on_area_entered(area: Area2D) → void:
is_in += 1

func _on_area_exited(area: Area2D) → void:
is_in = 0

func _process(_body) → void:
if is_in == 1:
if !Input.is_action_pressed(“Click”):
print(“if works”)
is_in = 0
Item.global_position = get_global_position()

All code is written by me, and I’m pretty new, so it may not be terribly efficient.

What happened to the indentations in your second code block?

I’m not seeing a distance comparison in your code. How are you determining “nearest”?

At first glance, these two lines look like your problem. When you capitalize a variable name, Godot assumes it’s a class name. I don’t see a class_name in your item script, but still, I’d suggest this:

class_name Item extends Node2D

var mouse_is_in = 0


func _process(_delta) -> void:
	if mouse_is_in == 1:
		scale = Vector2(0.22, 0.22)
		if Input.is_action_pressed("Click"):
			self.global_position = get_global_mouse_position()
	if mouse_is_in == 0:
		scale = Vector2(0.2, 0.2)


func _on_area_2d_body_entered(body) -> void:
	print("Entered")
	if Input.is_action_pressed("Click"):
		global_position = get_global_mouse_position()


func _on_area_2d_mouse_entered() -> void:
	mouse_is_in += 1


func _on_area_2d_mouse_exited() -> void:
	mouse_is_in = 0

Then…

@onready var item: Item = $“../Item”
# Other code
item.global_position = get_global_position()

This will prevent naming conflicts,

I think just an error when I copied and pasted, but they’re in the actual code, don’t worry.

I haven’t done that yet, as I’m not quite sure the best way to go about it, thus the post.

I’m not worried about your code. I can’t really read yours without them. If there’s an indentation issue, I cannot tell.

Where do I put these in my code? When I tried to put them at the top, it gave the error

Error at (4, 27): Invalid character ““” (U+201C).

Assuming “nearest” is based on pixel distance to the slot, I would approach the problem like this:

  • Create an array that holds the inventory items which are not stored
  • Search each member of the array to find the closest one
  • Move the inventory item into the slot
  • Remove the inventory item from the array

Ah, okay. Here are screenshots

Okay, thanks. New problem: I have NO CLUE what an array even is, and certainly not how to use it.

:slight_smile:

Don’t worry about not knowing what an array is. All of us were there at some point.

2 Likes

Thanks! I’ll read this after work, but my lunch break’s almost over.

Try reading this as well :slight_smile:

They replace the lines I quoted. Lines 3 and 18 from your screen shot. The problem with a screen shot of your code is that I cannot copy and paste it and edit it. I’d have to re-type it all, and I’m not doing that. Still, that should help.

This is where I put everything, but it gives me the error Trying to assign value of type ‘New_Item.gd’ to a variable of type ‘Inven_slot.gd’.

On what line does it give that error?

To my understanding it’s happening at 1:31

Well line 1 clearly doesn’t have 31 characters in it. Can you copy and paste the exact error?

I think it’s this:

E 0:00:00:859 Item.@implicit_ready: Trying to assign value of type ‘New_Item.gd’ to a variable of type ‘Inven_slot.gd’.
Inven_slot.gd:3 @ Item.@implicit_ready()
Inven_slot.gd:3 @ @implicit_ready()