#Prefab composition issue.

1 messages · Page 1 of 1 (latest)

rigid sinew
#

Hello, I'm having more of a code-appearance issue than a logical problem itself. The thing is that i'm doing sort of a RTS/Tower defense building system.
As a base, I'm using an SO to store the common fields of every building or troop in the game

        [Header("General Information")]
        [SerializeField] protected Sprite _icon;
        [SerializeField] protected string _name;
        [TextArea][SerializeField] protected string _description;
                
        public string Name => _name;
        public string Description => _description; 
        public Sprite Icon => _icon;

From this class I'm defining some inheritance for every type of entity with some preexisting data, for example:

    [CreateAssetMenu(menuName = "Towers/Tower Config")]
    public class TowerConfig : Config
    {
        [SerializeField] private TowerStats _stats;
        public TowerStats Stats => _stats;
    }

For the tower, this is an example of the Monobehaviour driving the SO data:

    public class Tower : MonoBehaviour
    {
        [Header("General Data")]
        [SerializeField] private TowerConfig towerConfig;
        [SerializeField] private Troop troopPrefab;
    }

Some of the prefabs are buildings, so I also define another MonoBehaviour with the preset data to use in the Building System script:

#
    public class Building : MonoBehaviour
    {
        [Header("General Data")]
        [SerializeField] private BuildingPlacement _buildingPlacement;
        [SerializeField] private MeshRenderer _renderer;
        [SerializeField] private Material _originalMaterial;
        [SerializeField] private int _cost;
        
        public Renderer Renderer => _renderer;
        public Material OriginalMaterial => _originalMaterial;
        public int Cost => _cost;
        
        [Header("Lifecycle Data")]
        private int _collidingObjects;
        public int CollidingObjects => _collidingObjects;
        
        private bool _onGround;
        public bool OnGround => _onGround;
        
    }

The problem is here: How could I make the Icon field on the Config a common field, easy to access for both the Building script and the Tower script (or any other driving script).

#

I asked GPT (not much of a fan of it) and solved it with an interface on the MonoBehaviour driving the Config SO inherited class, but I don't know if this is the best solution.

tropic light
#

Just wanna quickly slide in here and let you know you can serialize autoproperties like this

[field: SerializeField] public MyType MyPropertyName { get; private set; }
and you can play around with the access modifiers as needed. no need for two fields per thing like you have going on there 😄

nocturne timber
#

the first codebock is class Config : ScriptableObject, right?

rigid sinew
#

Yes, sorry, I just notice. The first codeblock is Config.

nocturne timber
#

couldn't you just have Building hold a reference to Config as well?

rigid sinew
#

Yes, I thought about it, but I don't know if it affects in something that Building gets all the data in the config, as it only needs the Icon.

nocturne timber
#

Config is a class, Building would only be storing a reference. there's no extra memory cost

#

the size of a reference Config is identical to the size of a reference to Sprite

rigid sinew
#

Ohh, I thought that bigger the class (or more populated), it affected more or less the memory cost of referencing it.

nocturne timber
#

nah, the point of referencing is to avoid that

#

the more data the class holds, the more memory is used to just store each instance - but the cost of referencing the instance doesn't change

rigid sinew
#

And, on more of a personal way, I didn't want to mix up data, but probably is a bad practice of mine. I always tend to do that and overkill simple references.

#

But yeah, I didn't know about the referencing memory cost. That's a life saver. Thank you so much. I'll work around with referencing the Config.