Godot Version
Godot 4.3 mac os
Question
Hi all, I am a very novice game dev who’s switching to Godot from an early prototype built in unity. I am making a turn based RPG and I am trying to use custom resources to create a stats class that I can have all of my units inherit that will provide health, attack, defense etc. I have created a custom script battler_stats.gd that contains the following:
extends Resource
class_name BattlerStats
enum BattlerType{
PLAYER,
ENEMY
}
@export var max_health : int
@export var min_damage : int
@export var max_damage : int
@export var turn_speed : int
And then from there I create multiple .tres resources named something like player_battler_1, enemy_battler_2 etc. that are of type BattlerStats
My question is, when I change the value of max_health or something on the player_battler_1 .tres in the inspector it seems to then change the value of ALL the max_health vars in all the .tres files that are of type BattlerStats. Is this the intended behavior for resources of a custom type? I was hoping that I could just have an individual resource that I can attach to each character that would allow me to give them all the same stats but with custom values.
Another thought I had is that I could just have something like slime_battler_stats that extends BattlerStats and have all the slime type enemies all have the same values and so on but this seems more complicated.
Appreciate any insights or suggestions as to how I can solve this problem. Thanks!
Yes, this is by design. Resources are shared and can be thought of as an implementation of the Flyweight design pattern.
Unless I’m misunderstanding the following quote:
something on the player_battler_1 .tres in the inspector it seems to then change the value of ALL the max_health vars in all the .tres files
Editing “player_battler_1.tres” should not change “slime_battler_1.tres”.
Editing “player_battler_1.tres” will be shared among all scenes/resources using “player_battler_1.tres”.
Additionally, look into two important capabilities of resources:
- “Local to Scene”
- “Make Unique”
Let me know if this helps.
Hi thanks for the reply! So if I have player_battler_1 and player_battler_2 that both inherit the script/class “battler_stats.gd” then they will all share the same value for any data that is held in the battler_stats class is that correct?
If so how would you suggest creating a class with stats that can be shared across multiple units but they can all have different values? Sorry if I’m not phrasing that well and I appreciate any feedback. Thanks!
Basically I’d just like all enemies, players, npcs etc to all inherit certain common stats, but then be able to extend that class to enemies, player characters etc. in a way that allows enemies to have properties that players don’t but they can all share name, health, etc. etc. Does that make sense?
To clarify further I’m pretty sure that this:
Editing “player_battler_1.tres” should not change “slime_battler_1.tres”.
Is the exact behavior I’m seeing. I create two different .tres files that are just both of type “BattlerStats” which is defined in the battler_stats.gd file. But then when I edit the player_battler_1.tres values, they ALSO change on the slime_battler_1.tres file in the inspector.
I could also be a huge idiot and not have been realizing that you need to double click on the item in the inspector to switch from one to the other. I think that was the issue. I am able to edit player battler .tres without changing the other ones. Thanks for the help though!
1 Like
No, the .tres
files are unique and their exported values can differ if changed in the Inspector.
Yep this turned out to be user error on my part. Appreciate the help though!