#Either pass it as a `ref` or `out` or

1 messages · Page 1 of 1 (latest)

supple plume
#

ooo okay, everything makes so much more sense now, I see you spent a lil while writing that for me, I really appreciate a ton, thank you so much man :D

magic pawn
#

Passing by ref

public class Caller
{
  public static bool x;
  
  public void Call()
  {
    Creator.CreateX(ref x);
  }
}

public class Creator
{
  public static void CreateX(ref bool x) // You have a reference to the original
  {
    x = true;
  }
}

Passing by out

public class Caller
{
  public static bool x;
  
  public void Call()
  {
    Creator.CreateX(out x);
  }
}

public class Creator
{
  public static void CreateX(out bool x) // x has to be assigned
  {
    x = true;
  }
}

Passing by return

public class Caller
{
  public static bool x;
  
  public void Call()
  {
    x = Creator.CreateX();
  }
}

public class Creator
{
  public static bool CreateX()
  {
    return true;
  }
}
#

Here are some examples of how to use all 3 of these

#

The most practical way tho is via return

supple plume
#

ooo, what are the differences or upside/downsides of using out or ref?

magic pawn
#

You only pass something via value if you want to give the method some kind of informations, by ref I don't really know - never had to use it - but basically whenever you want to edit the original as if you were using it directly in the calling class, and by out if you really want to, look up TryGetComponent in the docs for an example usage of it

supple plume
#

okay okay, thank you really really much, I extremely appreciate a ton, tysm :D