Treat result outcome as variable

public MyClass myVar;

public MyClass MyFunction(int some, string thing) {
    // some important internal processing
    return this.myVar;
}

public void Start() {
    Debug.Log(MyFunction)
    MyFunction() = new MyClass(1, 2, 3, 4);
    // effectively: myVar = new MyClass(1, 2, 3, 4);
}

Is there a way to treat function like a variable? I know that if a function returns a class, I can access/assign/modify internal variables of that class, but not reassign/manage the class itself, meaning its just one functionality short from a typical variable.

I knew I could do this:

MyClass myClass = MyFunction();
myClass = new MyClass(1, 2, 3, 4);

But that’s exactly what I’m trying to avoid.

Is there a way to assign variable to a class, that has been returned from a function that returns that type?

I believe out/ref could be a solution but I wouldn’t know how to figure it out and I couldn’t actually search it online, I don’t know which terms I should be looking for.

I’m not entirely sure what you are trying to do. Can you give a more elaborate example and perhaps explain what Problem you are trying to solve?

No, you can’t assign to a method like that to change its return value. You need to create two different methods, one for returning the value and one for setting the value.

[SerializeField]
private MyClass myVar;

public MyClass GetMyVar(int some, string thing)
{
    // some important internal processing
  
    return myVar;
}

public void SetMyVar(MyClass value)
{
    myVar = value;
}

C# does have a thing called properties, which are pretty close to what you are talking about. They function as if they were a field, but when you get or set their value, it actually calls a method. They are however limited in that you cannot provide any arguments when calling the get or set accessor methods.

[SerializeField]
private MyClass myVar;

public MyClass MyVar
{
    get
    {
        // some important internal processing
      
        return myVar;
    }

    set
    {
        myVar = value;
    }
}

It is also possible to assign a method into a variable as a delegate. You could change what value your variable returns when invoked by swapping the contained delegate with another one.

using UnityEngine;

public delegate MyClass MyFunctionHandler(int some, string thing);

public class DelegateExample : MonoBehaviour
{
    private MyFunctionHandler myFunction;

    private void Start()
    {
        myFunction = MyFunction;

        Debug.Log("MyFunction: "+myFunction(1, "2"));
  
        myFunction = (some, thing)=>
        {
            MyFunctionInternalProcessing(some, thing);
            return new MyClass(5, 6, 7, 8);
        };

        Debug.Log("MyFunction: " + myFunction(1, "2"));
    }

    public MyClass MyFunction(int some, string thing)
    {
        MyFunctionInternalProcessing(some, thing);  
        return new MyClass(1, 2, 3, 4);
    }

    private void MyFunctionInternalProcessing(int some, string thing)
    {
        // some important internal processing
    }
}
1 Like

The out parameter modifier (C# Reference) might help you with your problem:

    public MyClass myVar;
    
    public void MyFunction(int some, string thing, out MyClass myVar) {
        // some important internal processing
    }
    
    public void Start() {
        Debug.Log("MyFunction")
        MyFunction(0, "someString", out myVar);
    }

However, guessing what you want to achieve, this is an odd way to do it. It seems what you try to do is a Factory Pattern. Take a look at this:

    public static MyClass instance;
     
    public static MyClass Create(int some, string thing) {
        // some important internal processing
        return new MyClass (/*constructor parameter stuff*/);
    }

    public MyClass instance { get { if(instance == null){ instance = Create(/**/); } return instance; }
    }
1 Like

Well that sucks. Thanks for answers.