I'm trying to set up a card game system, where each card uses a separate stencil buffer reference to have its own "window" effect. I'm trying to accomplish this in the _ready function, by giving each card an ID, and using that ID as the stencil_reference for the card and the objects in the stencil buffer.
Notably, when I run the following code on multiple cards, each card updates ALL of the other cards to its stencil_reference. When I print the stencil_reference in the ready function, it seems to work, but then if I print it again later, everything is set to the last card's ID.
I'm not sure how to stop it from doing this. I've tried using set_local_to_scene(true) but that doesn't seem to be it. Any help is appreciated!
Code excerpt below:
@export var card_id: int
func _ready() -> void:
$CardObject.get_mesh().surface_get_material(1).set_local_to_scene(true)
$CardObject.get_mesh().surface_get_material(1).stencil_reference = card_id
$CardInnerObject.get_mesh().surface_get_material(0).set_local_to_scene(true)
$CardInnerObject.get_mesh().surface_get_material(0).stencil_reference = card_id
print($CardObject.get_mesh().surface_get_material(1).stencil_reference, $CardInnerObject.get_mesh().surface_get_material(0).stencil_reference)
await get_tree().create_timer(1.0).timeout
print($CardObject.get_mesh().surface_get_material(1).stencil_reference, $CardInnerObject.get_mesh().surface_get_material(0).stencil_reference)