I started focusing on my towers in my Tower Defense game and since I am using a custom Resource TowerStats for the stats of the tower I have a small issue with the upgrading. So When I place a tower and upgrade it, the stats change without an issue but that only counts for the placed towers. If I have three towers and I upgrade one, the rest will stay with the old stats. The issue comes when I press the build button for a new tower. The new towers that I build after upgrading a tower will begin with the max amount that was upgraded.
tower_stats.Damage += 10
While writing this a thought came to mind that I haven’t tried and that is to have a damage and max damage in my tower script but I don’t know if that would be a solution since I am using a custom resource and just upgrade it directly. Is there a special way that I need to go around for custom resources so they don’t all read the same stats?
You have to make sure every resource is a ‘unique’ resource / not linked to other instances. You can set a resource to be ‘unique’ by right clicking it in the editor and choosing ‘make unique’. This is also possible via code:
# make a resource unique
@export var my_resource : Resource
func make_unique():
my_resource= my_resource.duplicate()
If you are instantiating your tower scene when a new tower is bought you just have to make sure all resource the instantiated tower is using are not linked anymore. So just make sure to use my_resource.duplicate() in the _ready() function of your tower on all its resources:
Thank You so much, This was the solution for sure. I took Your advice and created a make_unique function which had the duplicate resource and it works.