Sprite2d not duplicating

ok


extends Sprite2D

@export var ITEMNAME : String

@onready var player_character = $"../Player_Character"
@onready var inventory_button_1 = $"../Player_Character/inventory_Button_1"
@onready var inventory_button_2 = $"../Player_Character/inventory_Button_2"
@onready var inventory_button_3 = $"../Player_Character/inventory_Button_3"
@onready var drink_sound = $Drink_Sound




func _process(delta):

	if get_parent() is Node2D:
		pass
	else:
		if get_parent().Pressed == true: # function goes in here.
			if player_character.Health < player_character.MHP:
				player_character.Health = player_character.MHP
				drink_sound.play()
				await get_tree().create_timer(0.5).timeout

				print("Button pressed")
				
				queue_free()
				









func _on_pickup_radius_body_entered(body):
	print("Entered")
	if get_parent() is Node2D:
		print("Character")
		if body.name == "Player_Character":
			
			if inventory_button_1.Free_Space == true:
				print("HI")
				
				get_parent().remove_child(self)
				inventory_button_1.add_child(self)
				position = inventory_button_1.get_node("Position_For_Items").position
				scale = Vector2(0.5,0.5)
			else: 
				if inventory_button_2.Free_Space == true:
					print("HI")
				
					get_parent().remove_child(self)
					inventory_button_2.add_child(self)
					position = inventory_button_2.get_node("Position_For_Items").position
					scale = Vector2(0.5,0.5)
	else:
		pass



func _on_pickup_radius_body_exited(body):
	print("Entered")
	if get_parent() is Node2D:
		print("Character")
		if body.name == "Player_Character":
			
			if inventory_button_1.Free_Space == true:
				print("HI")
				
				get_parent().remove_child(self)
				inventory_button_1.add_child(self)
				position = inventory_button_1.get_node("Position_For_Items").position
				scale = Vector2(0.5,0.5)
			else: 
				if inventory_button_2.Free_Space == true:
					print("HI")
				
					get_parent().remove_child(self)
					inventory_button_2.add_child(self)
					position = inventory_button_2.get_node("Position_For_Items").position
					scale = Vector2(0.5,0.5)
	else:
		pass

can you give the script that is on the drop a class_name, and do static type checking to make sure the item has the script? e.g.

  • name the script DropItem
  • change the following line

to
var Drop: DropItem = PossibleDrop.duplicate()

and see if it runs?

It says “Could not find type “DropItem” in current scope”

I think you forgot to change the name of the script on the item.
If it has a class_name on it, use that name, otherwise add the following line at the top of the the script:
class_name DropItem

edit: this one:

Still does not work, gives the same error.

its fine i dont think its going to work. Thank you for taking the time to try to help though!

Don’t give up too early :wink:

can you change this:

func Calculate_Drops(Xp_Drop_Amount : int):
	var Random_Number = randi() % 100
	if Random_Number <= 100: # The Player got the drop. the value is 100 because the testing for picking it up.
		Xp_Drop_Amount *= 2
		player_character.XP += Xp_Drop_Amount
		var Drop = Possible_Drop.duplicate()
		Drop.position = position
		node_2d.add_child(Drop)
		Drop.visible = true
		print(Drop.position,position,Drop.get_node("Pickup_Radius").position)

to

func Calculate_Drops(Xp_Drop_Amount : int):
	var Random_Number = randi() % 100
	if Random_Number <= 100: # The Player got the drop. the value is 100 because the testing for picking it up.
		Xp_Drop_Amount *= 2
		player_character.XP += Xp_Drop_Amount
		var Drop = Possible_Drop.duplicate(DUPLICATE_SCRIPTS)
		print("dropped item: '", Drop.name, "', has script func?", Drop.has_method("_on_pickup_radius_body_entered")) 
		Drop.position = position
		node_2d.add_child(Drop)
		Drop.visible = true
		print(Drop.position,position,Drop.get_node("Pickup_Radius").position)

i changed it, this is what it printed

dropped item: ‘Inventory_Item2’, has script func?true

it still didint work though

okay, that means that the script should be copied when we pass the flag explicitely

lets change this back to

   var Drop = Possible_Drop.duplicate()

I’ve tested duplicating nodes locally, and it seems like not providing any arguments would be what you want - it also copies signals from child nodes.

My guess is that everything is duplicated correctly and the issue is somewhere in the script / node that you copy. E.g. maybe there is no area on the Drop item that has a signal to the_on_pickup_radius_body_entered() function.

Did you already test putting the Drop node somewhere in your level to test pick up?

_
Lastly, you also might want to consider

  • using different nodes for pickup and held item, this saves you from having different behaviours mixed together in your code.
  • creating a scene for the drop item and instantiate this scene instead of duplicating a node. You can pass the necessary data in the same way you already pass the position information
1 Like

i dont know what happened but it works now! i copied your script that you again put and removed the “DUPLICATE SCRIPTS” and it works! thank you for taking the time to reply like almost 30 times.

1 Like

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