Skip to main content
Post Reopened by DMGregory
Post Undeleted by DMGregory
Focusing on the problem you want to solve, not general C# syntax
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Scriptable Object Inheritance (new keyword) Extending a ScriptableObject to have a get property instead of a field, without losing serialized data

I have two Scriptable Objectsa ScriptableObject that's already used extensively in my project:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}
 

I want to extend it to a derived version something like this:

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    newoverride public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another classBut of course, is does not return a random clip fromI can't do that without making the list butclip field on the original object into a clipvirtual property.

  If I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achievingdo that, I'll lose all the desired effectAudioClip references in existing SFXClip instances in my project's Assets.

To prove that this problem does not occur in vanilla C#:

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila it works asIs there a way I would expect.can implement this change without losing that data?

Scriptable Object Inheritance (new keyword)

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}
 
public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

  I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

To prove that this problem does not occur in vanilla C#:

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila it works as I would expect.

Extending a ScriptableObject to have a get property instead of a field, without losing serialized data

I have a ScriptableObject that's already used extensively in my project:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

I want to extend it to a derived version something like this:

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    override public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

But of course, I can't do that without making the clip field on the original object into a virtual property. If I do that, I'll lose all the AudioClip references in existing SFXClip instances in my project's Assets.

Is there a way I can implement this change without losing that data?

Post Deleted by Commodore Yournero
removed expletives and caps
Source Link
user35344
user35344

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

TO PROVE THAT THIS PROBLEM DOES NOT OCCUR IN VANILLATo prove that this problem does not occur in vanilla C#:

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila IT FUCKING WORKS AS EXPECTEDit works as I would expect.

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

TO PROVE THAT THIS PROBLEM DOES NOT OCCUR IN VANILLA C#

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila IT FUCKING WORKS AS EXPECTED

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

To prove that this problem does not occur in vanilla C#:

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila it works as I would expect.

added 424 characters in body
Source Link

Scriptable Object Inheritance override (new keyword)

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

TO PROVE THAT THIS PROBLEM DOES NOT OCCUR IN VANILLA C#

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila IT FUCKING WORKS AS EXPECTED

Scriptable Object Inheritance override (new)

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

Scriptable Object Inheritance (new keyword)

I have two Scriptable Objects:

public class SFXClip : ScriptableObject
{
    public AudioClip clip;

    // other properties
}

public class SFXRandom : SFXClip
{
    [SerializeField] List<AudioClip> audioClips;

    new public AudioClip clip
    {
        get
        {
            return audioClips[Random.Range(0, audioClips.Count)];
        }
    }
}

When accessing the clip property from SFXRandom in another class, is does not return a random clip from the list but the original clip property.

I am looking for an explanation why using new won't hide the inherited member and/or suggestions for achieving the desired effect.

TO PROVE THAT THIS PROBLEM DOES NOT OCCUR IN VANILLA C#

Consider the following code:

public class Test1
{
    public int x = 1;
}

public class Test2 : Test1
{
    new public int x
    {
        get
        {
            return 2;
        }
    }
}

Now create an instance of both classes, print x and voila IT FUCKING WORKS AS EXPECTED

added 6 characters in body
Source Link
Loading
Post Closed as "Not suitable for this site" by DMGregory
Source Link
Loading