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

Question C#: How to make all references to an object change together? Pointers in C#?

Discussion in 'Editor & General Support' started by mikejm_, Jun 3, 2023.

  1. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    I am struggling with a basic C# issue I am not sure how to get around.

    Let's say you have:

    Code (csharp):
    1. CustomClass object = new CustomClass();
    2. CustomClass anotherObject = new CustomClass();
    3.  
    4. List<CustomClass> objectList = new List<CustomClass>();
    5. objectList.Add(object);
    6.  
    7. objectList[0] = anotherObject;
    8.  
    9. if (objectList[0] == object){
    10. Debug.Log("ORIGINAL REFERENCE CHANGED"); //will not debug out
    11. }
    You will end up changing the entry in objectList to "anotherObject" but, what if you want this operation to change both "objectList[0]" and "object" to "anotherObject" through the line "objectList[0] = anotherObject"?

    Is it possible to make a list or pointer or reference of some kind that truly represents "object" at its core? so when you change "objectList[0] = anotherObject" you are actually changing "object", and not just a new reference in the List?

    I think in C++ this was the point of references and pointers and they solved this somehow. I am curious if anyone can explain if so - I was never good with references or pointers but I feel like that is what they fixed. This might be a good example to explain with if so.

    For example, I think if you had:

    Code (csharp):
    1. List<CustomClass*> objectList = new List<CustomClass*>()
    as a list of pointers in C++ this would solve it. But I don't think we have pointers in C# do we?

    How is this meant to be done or possible?

    I have read:

    So we cannot use pointers in a situation like this. Is there some other way? Or do you just need to be careful to ensure you are always changing all the references you want at once with good careful coding?

    Let's say you had the example above. Would using an array of CustomClass work? Or is it only arrays of value types?

    Sometimes I find putting things in a list temporarily so I can iterate through them logically is faster because instead of writing a bunch of:

    Code (csharp):
    1. if (something == object){
    2.     object = //do something
    3. }
    4. if(somethingElse == anotherObject){
    5.     somethingElse = //do something
    6. }
    I can do:

    Code (csharp):
    1. for (int i=0;i < somethingList.Count;i++){
    2.     if (somethingList[i] == objectList[i]){
    3.         objectList[i] = //do something
    4.     }
    5. }
    And cover much more in fewer lines. But then since it's not a pointer, it doesn't change the original "object" as given in the original code above. So this is just breaking function by creating new references that are handled independently rather than still linking the original reference/object I want all the way through.

    Or as another example, if I have:

    CustomClass object;

    Thanks for any help. Hopefully that makes sense.
     
    Last edited: Jun 3, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    mikejm_ likes this.
  3. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Ah. I see. So you just containerize some more. I understand I think. In this case, doesn't help reduce complexity. So I won't. But I understand I think what you mean.

    I was hoping you would reply. Thanks. :)
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Well that's a nice thing to hear. Thanks!!

    I still do a fair amount of C / C++ coding to maintain my old games, all of which now compile under Unity in my KurtMaster2D application, so I still trip myself up trying to remember the rules of indirection.

    Speaking to your original problem, I think of it as a directory.

    If you have a few different directories of things, eg, the same thing in different directories, and you want that same thing to change everywhere, you don't want to think about changing the many directories. You want to think about changing the actual thing, which means it must have one more level of indirection.

    If you're curious, here's KurtMaster2D:

    Apple iTunes: https://itunes.apple.com/us/app/kurtmaster2d/id1015692678

    Google Play (including TV): https://play.google.com/store/apps/details?id=com.plbm.plbm1

    Preliminary WebGL release (kinda rough still):

    Itch.io: https://kurtdekker.itch.io/kurtmaster2d