Interfaces and Scriptable Objects

I am making a game and most of the basic systems (weapons, characters, etc) inherit from an interface (IWeapon etc) and im trying to make the class that inherits from this weapon interface a scriptable object as well along the lines public class BaseWeapon : ScriptableObject, IWeapon. The issue im having is making the necessary parameters (name, icon, stats, etc) show up to set in the inspector. I know interfaces dont allow to set stuff in inspector but i was wondering if there is a workaround to implement scriptable objects with interfaces to get the memory saving benefits from them. I created the asset menu for BaseWeapon and tried doing [SerialisedField] and then doing a private set for one of the parameters but to no luck getting to do it in the inspector.

Show us your code.

Cause as is… this should work:

public interface ITestInterface
{
    public int Value { get; set; } //EDIT - typo, no public here
}

[CreateAssetMenu(fileName = "ConcreteTest")]
public class ConcreteTest : ScriptableObject, ITestInterface
{

    [SerializeField()]
    private int _value;
 
    public int Value
    {
        get => _value;
        set => _value; //EDIT - typo, should be 'set => _value = value;'
    }

}

But I don’t know what your code looks like.

1 Like

Sorry for the late reply didnt think id have gotten a response this fast.
My code is similar to yours but im getting a CS0201 error on the sets.
7949782--1017811--upload_2022-3-8_15-24-27.png

7949782--1017805--upload_2022-3-8_15-23-5.png

7949782--1017805--upload_2022-3-8_15-23-5.png

Sorry, I typed my code in the browser and made a typo… the setter should be:

set => _name = value;

Also no public in the interface.

The fact you had literally duplicted my typos, says to me, you had NO code before posting.

1 Like

I apologise if it came off the wrong way but i had public in the interface cause thats how it was made initially (new to coding c# so wrote it based on how someone showed me initially so thank you for your correction there) and i honestly did just have the set wrong as i was unaware i needed to have the set to value as well so thank you for your insight there. This has been the second time you have helped me here so i appreciate the knowledge.

Question.

You say in your original post:

What memory saving benefits are you hoping to get?

I’m unaware of memory saving benefits that interfaces offer directly (I can possibly think of design patterns that could potentially save memory that happen to use interfaces. But the interface isn’t really the memory saver there.)

Or are you referring to the memory saving benefits of ScriptableObject? Versus what though? Versus GameObject’s and components, technically true. But versus plain old objects? Not really.

Memory saving that ive read from scriptable objects. So because the game would have a large number of weapons i thought it would be better to have the weapons be scriptable objects rather than making multiple new scripts from an abstract class to house each unique weapon. The interface system part comes in because the person (much more knowledgable than I on coding architecture) implemented the interface systems when making some of these core components of the game. Initially it was such that an abstract class would inherit from the interface and we’d call a constructor from the abstract class to make new weapons in the concrete class for each weapon.