scene with localized script has changed variable affect other instances

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

i’m making a factory game

i added a machine for converting an item to another

but for some reason if i have 2 items enter at the same time, the other machine which has 0 items clones the inventory

i store the inventory with set_meta and get_meta

i have set the resources to be unique and the script is set to be local, i did .duplicate() on every set_meta() i used, but it still happens
i’ve tried using a variable inside the script to store the inventory but it just keeps happening

how do i make it so this stops happening, i’ve been trying for hours

Edit:
i did some testing and found that this only happens while updaing the values outside the scene itself i don’t know why

code i use (really messy, its my first time using godot):

if get_tree().current_scene.get_meta("metadata").money >= placemeta.buy:
			var update = get_tree().current_scene.get_meta("metadata").duplicate()
			update.money -= placemeta.buy
			get_tree().current_scene.set_meta("metadata", update.duplicate())
			var scene3 = scene2.instance().duplicate()
			scene3.set_meta("metadata", placemeta.duplicate()) # this is what broke it for some reason
			#if scene3.get("metadata") != null:
			#	scene3.metadata = placemeta.duplicate()
			scene3.position = get_global_mouse_position()
			get_tree().current_scene.add_child(scene3)
			scene3.set_owner(get_tree().current_scene)
			var stuff = int(scene3.position.x) % 64
			if stuff != 0:
				scene3.position.x = int(scene3.position.x) + 0 - stuff;
			var stuff2 = int(scene3.position.y) % 64
			if stuff2 != 0:
				scene3.position.y = int(scene3.position.y) + 0 - stuff2;
			return

I did not fully understand your question. Did you add “Group” to the same items?
(sorry i use “translate”)

ramazan | 2022-03-14 08:27

I have a few questions for clarification:

  1. Why are you using duplicate(), and what are you using it on?
  2. Does the inventory get copied if only 1 item enters at a time?
  3. What does your scene tree look like? Where is the script?
  4. What happens when items enter the other machine?

CardboardComputers | 2022-03-14 14:23

  1. i’m using duplicate because, for some reason, when i didn’t the metadata kept using the dict that was user as a ref that changed constantly, when i needed it to have an original copy
  2. yeah, but the item only comes out of one, which means for some reason the inventory is shared
  3. the machine gets added as a child, and the script is inside an area2d
    enter image description here
  1. same thing, the inventory acts as if it was shared

lettucing | 2022-03-14 21:30

:bust_in_silhouette: Reply From: lettucing

i might be stupid

i forgot to add “true” to duplicate()