Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A class that return value called by his name ?

Discussion in 'Scripting' started by Deceleris, Jul 4, 2019.

  1. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    Hello there, I don't even know if that's possible, assuming that's a complexe topic, but I want to create class, that can handle a global getter and setter.

    This is an example, but I wish I could generalize it
    The [Expression] is the part I'm searching for, I have no idea of what it could be

    Code (CSharp):
    1. public class intP
    2. {
    3.        public int intValue;
    4.        [Expression] { get { return intValue; } set { intValue = value; }
    5. }
    6.  
    Code (CSharp):
    1. public class Test
    2. {
    3.       public intP money;
    4.       public void AddMoney (int amount) { money += amount; }
    5. }
    Because I want to create my custom properties, working like float or int but also with the ability to have level, or utilities x)
    I want to use that system to make some item databases, dialog editors etc
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Make the property static.
    Code (CSharp):
    1. public class Example {
    2.    public static int Foo { get; set; }
    3. }
    Then from anywhere in any script, you only need to call:
    Code (CSharp):
    1. Example.Foo
     
  3. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    Thanks Vryken for your reply !
    I know that issue but that not the same topic, finally in another forum someone gave me the solution x)

    The issue is by using parameters

    Code (CSharp):
    1.  
    2. public class FloatP {
    3. public float value;
    4. public static FloatP operator + (FloatP l, FloatP r) { l.value += r.value; return l; }
    5. }
    6.  
    7. // then you can have :
    8.  
    9. FloatP a, b;
    10. a + b; // Working great ^^
    11.