Regarding the issue of generating child objects that share the same variable.

Today, when testing the card generation issue in the card game, I found that when using the following method to generate multiple cards, a problem occurred where each card shared the same health value. When one card’s health drops to zero, all objects disappear.
and the process of being destroyed repeats continuously, followed by an error.

extends GameScene
@onready var pre_card_scn = preload("res://卡片保存/卡片.tscn")
@onready var card_test = preload("res://卡牌对象新建测试.tscn")
var dragging_card: Card  = null
var mounse_card_offset : Vector2 = Vector2.ZERO


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	_new_card_test(Vector2(600, 500))
	_new_card_test(Vector2(700, 500))
	_new_card_test(Vector2(800, 500))
	pass # Replace with function body.



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	#if  dragging_card:
		#dragging_card.position = get_global_mouse_position() + mounse_card_offset
	pass
		
func _new_card_test(posititon: Vector2):
	var card_test = load("res://卡牌对象新建测试.tscn")
	var card = card_test.instantiate()
	card.position = posititon  # 设置卡牌位置
	add_child(card)
	pass



I want to find a way to generate a large number of cards to build the deck of my game. When using the Visual Studio Editor as the script, generating objects can only be done by specifying the generation path, and calling database data can only be specified by constants, which troubles me. When I tried to batch generate using the add_child method, I encountered such an error. I would like to ask the experts in the forum if there is a good solution.
If you can reply, I would be extremely grateful.

Can this issue be reproduced when using VS scripts to generate multiple child objects?

According to your suggestion, I used the script in VS to generate three card objects. This time, the console did not issue an error command, but an interesting phenomenon occurred. When the first card was destroyed, it triggered the destruction animations of the other two cards. I originally thought the same thing would happen, but I noticed that after the destruction animation, the button node I used to trigger it remained in place. Then, after I clicked it, the remaining two cards returned to their initial animation state. Later, I repeated the experiment with the second card, and the same effect occurred: the two cards simultaneously entered the destruction animation, and clicking would make them return to the initial state again. However, the first two cards were indeed destroyed, which also confused me because the variable conditions I set were for their own variables, and this would affect the generated identical GameObjects.




I suggest continuing to narrow down the scope and demonstrate the issue of associated variable values in a brand new blank project. Report the bug using this minimal reproduction project. This way, perhaps once this bug is fixed, all problems will be resolved.

Thank you for your suggestion. I will try to reproduce it when I have time and submit it as a bug issue.

An accidental attempt might have led me to a solution. It is possible that when editing the shader in the animation, I modified the shader_parameter’s dissolve_value to achieve the desired dissolution effect. After checking resource_local_to_scene, the issue was resolved. This suggests that generating objects from the VS script can address the problem, but this generation method is not flexible. Additionally, the add_child() method still throws an error. We can only look forward to more features or other solutions.


I believe resource_local_to_scene is not the ultimate solution to the problem. It just happens to bypass the issue. I feel it might be due to some resource being shared, but I don’t know what it is yet. Why do the values of variables between different objects affect each other? If a minimal reproducible project can be provided, then this issue can be fixed.

1 Like

Thank you for your suggestion. I have created a minimal reproducible project and sent it to the bug report section.