Sprite2d not duplicating

Godot Version

4

Question

i have a sprite2d i use as a item for my inventory, i want to clone it when a enemy dies for a drop, but when i use the “duplicate()” function it does not duplicate. can someone help with this? Thanks.

And also here is the code

	var Drop = Possible_Drop.duplicate()
	Drop.position = position
	Drop.visible = true
	print(Drop.position,position)

what do you mean with this?
maybe there are some sub-resources that you want to have duplicated as well? In this case you need to call
duplicate(true) (the optional boolean argument specifies whether subresources are duplicated as well)

when i use duplicate(true) it still does not work, im printing the position and it says its dropped right on the enemy position, but it does not show.

have you added it to the scene_tree?

add_child(drop)
2 Likes

That worked, but now the script for it is not working.

Does it duplicate the script with it or is it a separate function for that?

I think it should, if you use duplicate(true)

i am using duplicate(true).

or is it because the area2D? do i have to set that position again?

this.

I thought you wanted to duplicate a resource. If you want to duplicate a node, you also have a different argument for the duplicate call - so just remove the true argument for now (unless you want to duplicate groups/signals… in that case have a look at the duplicate flags)

What exactly does the script do

aah, I’m too slow with answering.
If you remove the argument in duplicate(), it should copy the script as well

1 Like

the script is inside the enemy character and controls stuff like how it dies, Health, Movement.

Does it work without the “true”?

no it does not

Okay what do you expect to happen? and what is actually happening?

i expect the item to drop and let me pick it up from the area 2d, to go into my inventory system. whats happening is the item is visible but will not let me pick it up and the script will not print anything.

i will also give my entire enemy script if that helps

class_name Red_Enemy
extends CharacterBody2D

@export var Speed : int = 100.0

@export var Health : int = 50
@export var MHP : int = 50
@export var Damage : int = 10
@onready var Possible_Drop = $"../Inventory_Item2"
@onready var node_2d = $".."

var Xp_Drop : int = 25
var Player_Chase = false
var Spawn_Pos = position
@onready var player_sprite = $"../Player_Character/Player_Sprite"


@onready var player_character = $"../Player_Character"
@onready var enemy_sprite = $Enemy_Sprite


func _process(delta):
	if Health <= 0:
		Died(Spawn_Pos)
	if Player_Chase == true:
		Update_Position()
		Update_Animations()
		


func Update_Position():
	position += (player_character.position - position)/Speed
	move_and_slide()

func Update_Animations():
	if player_sprite.animation == "Idle_Side":
		enemy_sprite.play("Idle_Side")
	elif player_sprite.animation == "Idle_Back":
		enemy_sprite.play("Idle_Back")
	elif player_sprite.animation == "Idle_Front":
		enemy_sprite.play("Idle_Front")
	if player_sprite.flip_h == true:
		enemy_sprite.flip_h = true
	elif player_sprite.flip_h == false:
		enemy_sprite.flip_h = false




func _on_detection_area_body_entered(body):
	if body.name == "Player_Character":
		Player_Chase = true



func _on_detection_area_body_exited(body):
	if body.name == "Player_Character":
		Player_Chase = false


func Died(Original_Pos):
	var Death_Reward_Given = false
	Calculate_Drops(Xp_Drop) # Calculating the drops
	Health = MHP
	if Death_Reward_Given == false:
		Death_Reward_Given = true # the player got the reward.
		
	position = Vector2(999,999)
	await get_tree().create_timer(5).timeout
	
	position = Original_Pos
	

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)

The drop script would be more helpful :sweat_smile: