0

I'm striking a roadblock in coding. I'm coding in C#, making a program with Monogame.

I have a list of units, and I'm trying to do mouse picking with them. I shoot a ray out from the mouse into the screen, and find which object is the first one it hits.

//  A method to find which units are currently under the mouse.
    static public void FindPickedUnits(string ID)
    {

        foreach (Unit unit in ListDictionary[ID])
        {

            //  Run the code to check whether or not it is picked, and returns a float representing how close it is.
            unit.IsPicked = Cursor.Pick(unit.Collisions);

            //  And if it is picked...
            if (unit.IsPicked != null)
            {

                //  We will clone it as the closest unit if none exist, or...
                if (ClosestUnit == null)
                {

                    ClosestUnit = unit;

                }

                //  if one already does, overwrite it if it's even closer.
                else if (unit.IsPicked < ClosestUnit.IsPicked)
                {

                    ClosestUnit = unit;
                    Console.WriteLine("The new closest unit is at X" + unit.Position.X + " Y" + unit.Position.Y);

                }
            }
        }
    }

    //  elsewhere...

    Console.WriteLine("The selected unit's color is " + ClosestUnit.Tint);

This code clones the picked unit into the ClosestUnit object, which then I can read from whenever I want, no problem.

HOWEVER

I don't want to purely read the values of the closest unit, I want to WRITE to them, and change some of them. But if I change the values of the ClosestUnit object, it doesn't reflect back to the actual unit in the list of units.

TL;DR I want to be able to single a unit out of a list of units, and then write to it elsewhere in the code. If the ClosestUnit object could function akin to a ref parameter does, directly referencing the actual unit in the list instead of being a clone, this would be easy, but I don't know how to make it do that.

What is the most efficient way to handle this problem, or avoid it?

  • Is `Unit` a class or a struct? – Klinger Dec 26 '16 at 23:22
  • I by default habit made it a class... but I could probably change it to a struct, tbh. Why? – Charybdizs Dec 26 '16 at 23:24
  • Because one way I can see you having this problem would be if Unit is a struct. A class is passed by reference and if you have access to a reference of an object you can change its properties. The other possibility is that ListDictionary is cloning instances of `Unit` before returning it. – Klinger Dec 26 '16 at 23:30
  • Also your code is not actually "cloning" the unit object, just assigning it to `ClosestUnit`. – Klinger Dec 26 '16 at 23:33
  • ...My bad. I just naturally assumed that changes made to ClosestUnit wouldn't reflect back to the original unit. I hadn't even bothered trying. But actually, they do. So cool! I never thought about how the nature of reference types means that. Guess I learned something new! – Charybdizs Dec 26 '16 at 23:41
  • Now what if I wanted to copy the VALUE to ClosestUnit, and... not the reference itself? As long as the subject is on hand, how would I ACTUALLY create a clone of the value as opposed to the reference? – Charybdizs Dec 26 '16 at 23:42
  • For the cloning take a look on this SO post: http://stackoverflow.com/questions/78536/deep-cloning-objects – Klinger Dec 26 '16 at 23:44
  • Awesome, that's perfect. Thank you! – Charybdizs Dec 26 '16 at 23:47

1 Answers1

0

Ironically, there was nothing wrong with my code the whole time. I didn't realize that the ClosestUnit object, by nature of being a class (a reference type), directly referenced the unit in the ListDictionary, as opposed to being a clone of it.

I could just change the values on the ClosestUnit object and it would reflect back to the original. Yay!