I have an interface and I have declared a structure inside it. I want the interface to handle events. At first I had it like this:
public interface IEventInfo
{
public int Health { set; get; }
public int Lives { set; get; }
public int Damage { set; get; }
public int Coin { set; get; }
public int Tokens { set; get; }
public int Score { set; get; }
public string Power { set; get; }
public string Weapon { set; get; }
public string Armor { set; get; }
}
But after using it in a couple of classes I have thought that it would be much more comfortable, tidy and clean if I did it this way.
public interface IEventInfo
{
public struct SData
{
public int Health;
public int Lives;
public int Damage;
public int Coin;
public int Tokens;
public int Score;
public string Power;
public string Weapon;
public string Armor;
}
public SData Data { set; get; }
}
And so I would only have to implement the following in all my classes.
public SData Data { set; get; }
The issue is that it is giving me an error when trying to access the value of the variables:
public class myClass : MonoBehaviour, IEventInfo
{
public SData Data { set; get; }
public myClass()
{
Data = new IEventInfo.SData();
Data.Health = 100;
Data.Lives = 1;
Data.Damage = 10;
Data.Coin = 5;
Data.Tokens = 1;
Data.Score = 50;
}
}
Compiler error CS1612 Cannot modify return value of 'expression' because it is not a variable
And it tells me the following:
To modify the structure, first assign it to a local variable, modify the variable, and then assign the variable back to the collection element.
I do this but the error persists:
Data = new IEventInfo.SData();
int tmp = Data.Health;
tmp = 100;
Data.Health = tmp;
Does anyone know how to do it correctly? Thank you so much!!
I don't know if it's exactly what you want, but I'll give you an answer.
Types cannot be defined in interfaces, that is prohibited. What you can do is the following:
Define your structure in a separate class, not in the interface:
Your interface would then look like this:
The way to use it would be something like this: