I have a list of characters stored in two places. One, inside a state file which can be saved and loaded at will. Two, inside a temporary list of characters for use in a camping scene. I thought that these two different arrays should both be storing references to they same character objects in memory. However, through testing, I have determined that the objects are somehow being duplicated instead of referenced.
I hope it’s not necessary to post my full code. I think the problem relates to the way I’m setting up and storing the separate date, not the way I’m changing the data. By using print() statements, I’ve determined that modifications to characters in the campCalendar do not affect the same characters in the State. I am not using a .duplicate() function anywhere. So, what am I doing wrong? And how do I fix it?
SAVE STATE
class_name State extends Resource
@export var characters: Array[Character]
DATA MANAGER
extends Node
@onready var state := State.new()
var campTasks : Array[ CampTask ]
var campCalendar : Array[ Array ]
func setupCampCalendar():
for character in state.characters:
campCalendar.append( [character, campTasks[0] ] )
class_name Character extends Resource
@export var stats:Stats
@export var identity:Identity
@export var profession:Profession
@export var experience:Experience
func _init( ):
stats = Stats.new()
identity = Identity.new()
profession = Profession.new()
experience = Experience.new()
func setup( fName:String, pName:String ):
identity.familyName = fName
identity.personalName = pName
stats.initializeMeters()
The State is being created at the start of the program. There are save/load functions, but they aren’t being used right now.
The differences between characters are being checked with this function, after characters have been modified via functions using campCalendar:
var character = DataManager.state.characters[0]
print( "%s : %d" % [character.identity.familyName, character.stats.stamina.x])
character = DataManager.campCalendar[0][0]
print( "%s : %d" % [character.identity.familyName, character.stats.stamina.x])
I originally noticed this problem because the UI is only showing the original states of the characters and not detecting any changes to them made via campCalendar. (The UI only takes information from the State.)
How are you filling the State.characters array and when?
When are you calling DataManager.setupCampCalendar()? Is it before or after you fill the State.characters array?
What’s the output of those prints?
I think the issue might be happening somewhere else in your code rather than during object creation. Since you’re using Character.new() for each character, they should be separate instances.
I’d suggest checking where stats.endurance is being modified and whether newGame() or any setup function is being called more than once.
Also, try printing get_instance_id() for both references to confirm they’re pointing to the same object or different ones.
The key is to find where the value changes from 127 to 112. That should help identify the actual cause.