Spectralle
Spectralle

Reputation: 69

How to set the Value variable of this custom Odin Inspector class

I know, the title is bad, but I couldn't think if a better one. The question is very specific.

Ok, so I'm using a class in my game identical to Odin Inspector's example RPG Skills classes. But it's set up in a way I don't quite understand and I can't work out how to set the value (I can get it, and there is a setter, so it's possible to set too). Also, all the skill classes/structs/etc are in the same .cs file.

The SkillList function I use to get the Value: (I get it with skills[Strength].Value; in other classes)

public int this[SkillType type]
{
    get
    {
        for (int i = 0; i < this.skills.Count; i++)
        {
            if (this.skills[i].Type == type)
                return this.skills[i].Value;
        }
        return 0;
    }
    set
    {
        for (int i = 0; i < this.skills.Count; i++)
        {
            if (this.skills[i].Type == type)
            {
                var val = this.skills[i];
                val.Value = value;
                this.skills[i] = val;
                return;
            }
        }
        this.skills.Add(new SkillValue(type, value));
    }
}

SkillValue struct:

[Serializable]
public struct SkillValue : IEquatable<SkillValue>
{
    public SkillType Type;
    public int Value;

    public SkillValue(SkillType type, int value)
    {
        this.Type = type;
        this.Value = value;
    }

    public SkillValue(SkillType type)
    {
        this.Type = type;
        this.Value = 0;
    }

    public bool Equals(SkillValue other)
    { return this.Type == other.Type && this.Value == other.Value; }
}

SkillType enum:

public enum SkillType
{
    Science,
    Technology,
    Arts,
    Language,
}

I've tried:

skills[Science].Value = 10;

skills[Science] = new SkillValue(Science, 10);

skills[Science, 10]; (using a new function made by me)

skills[Science](10);

skills[Science].Value(10);

skills[Science] = 10;

But none work, and I'm just guessing randomly now. How can I set the value? Thanks


The solution: character.skills[Rorschach.Character.Skills.SkillType.Science] = value;

Upvotes: 0

Views: 2949

Answers (1)

derHugo
derHugo

Reputation: 90620

Your property is of type int and expects a key of type SkillType so it should probably be

SkillList skills;

skills[SkillType.Science] = 10;

actually also

I get it with skills[Strength].Value;

seems odd with the code you provided. As said the property returns an int which has no property Value so it should actually be

int x = skills[SkillType.Strength];

Now knowing the full implementation code and your actual usage:

public SkillList skills;

...

public int Science 
{ 
    get { return this.Character.skills[Science].Value; } 
    set { this.Character.skills[Science].Value(10); }
}

ATTENTION!

What you did here by accident is using the other property

public SkillValue this[int index]
{
    get { return this.skills[index]; }
    set { this.skills[index] = value; }
}

which takes an int index and returns a SkillValue.

BUT you are causing a runtime StackOverlowExeption due to a recursive call of Science.

You can't use Science inside of the getter or setter of equally called property!

Imagine using the getter as example:

  • You would call

    var test = Science;
    
  • so it executes the getter

    return Character.skills[Science].Value;
    
  • but well ... in order to know the value of Science in order to use it here as the index it would again have to execute the getter so again

    return Character.skills[Science].Value;
    

and by now you hopefully get what I mean.

Solution

You property should actually as guessed before rather look like

public int Science 
{ 
    get { return Character.skills[SkillType.Science]; } 
    set { Character.skills[SkillType.Science] = value; }
}

Upvotes: 1

Related Questions