An array isnt being duplicated when using the duplicate(true) func

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dragon20C

Here is a video link to my problem that I have uploaded to youtube: https://www.youtube.com/watch?v=_8Pa_wz4bfk

	if dropable:
	if Input.is_action_just_pressed("right_click"): 
		if inventory[dropable_index] != null: # If input and slot isnt empty add and remove 1
			if inventory[dropable_index].amount < inventory[dropable_index].max_amount:
				if hand_array[0].amount > 1:
					inventory[dropable_index].amount += 1
					hand_array[0].amount -= 1
					update_slot(dropable_index)
					update_held_item()
		else:
			if hand_array[0].amount > 1:
				var saved = hand_array.duplicate(true) # A dup that SHOULDNT TOUCH THE ORIGNAL ARRAY
				saved[0].amount = 1 # sets it to one ONCE
				print(hand_array[0].amount) # GETS TOUCHED ANYWAY
				inventory[dropable_index] = saved[0] # Setting the new inventory slot to new item
				hand_array[0].amount -= 1 # Removes one to make it look like its dropping a item
				update_slot(dropable_index)
				update_held_item()

for what ever reason this website cant format my code correctly…

anyways I am trying to duplicate an item in the array between two different arrays one being the inventory and one being the held item array but for what ever reason it keeps touching the held item array when I SPECIFICALLY TOLD IT, ITS A DUPLICATE and it still touches the array like its still referencing it when i told it, it shouldn’t!

:bust_in_silhouette: Reply From: Andrew Wilkes

I can’t see any problem with your code. But I had an idea: maybe your hand_array is referencing some object that is pending an update on the next frame? If that is a cause of the problem then call_deferred may be a solution. Calling the update code later.

Actually figured it out with some help from Reddit, the issue is that the data stored is a custom class that doesn’t get stored but referenced which is why this happens.

Thanks for the comment though!

Dragon20C | 2021-06-21 11:29

Happy that you solved your problem :slight_smile:

Andrew Wilkes | 2021-06-25 10:17