is it possible to pass a Vector2 by reference?

I’m attempting to add child objects to objects in my 2D game world.
So far I’ve just passed in their parents positions whenever they’re updated in move functions, and I just move them in relation to that.

However I believe that it would be nicer if I can just pass them the vector2 when they’re created by reference, which would prevent the need for passing in the parent’s current position as a parameter.

However when attempting this the values within the vector2 aren’t updated, x and y always = 0.

For example:

public class Child
{
        private Vector2 parentPosition;
        public void init(ref vector2 _parentPos) {
          parentPosition = _parentPos
        }
}

I apologise if this is more of a language question

This doesn’t work. When you do the assignment the value of _parentPos is stored, not a reference to it.

C# doesn’t support this. One approach is to make the parent’s position a property. You can define the setter such that when the position of the parent is set, the children’s positions are also modified.

Quick example:

public class Parent
{
    private List<Child> Children;

    private Vector2 position;

    public override Vector2 Position
    {
        get => position;
        set
        {
            Vector2 cur = position;
            position = value;

            Vector2 diff = position - cur;

            for (int i = 0; i < Children.Count; i++)
            {
                Children[i].position += diff;
            }
        }
    }
}
1 Like

Yeah I’m aware, I’ve gotten around it for now by passing the entire parent class in, but that feels close to becoming circular and it only works if they’re all in the same namespace.

E.G:

public class Child
{
        private Character parent;

        public void init(Character _parent) {
          parent = _parent;
        }
}

this isn’t nice though

thanks, I think this is the best solution :slight_smile:

You can only link objects not structs like that.

I do it like this for a parent child but usually with abstract classes.
This way even though the parent contains the child either change thru the parent or child directly is reflected.

    public class Item
    {
        public string itemName = "None";
        public int itemData = 0;

        public Item parent;
        public List<Item> children = new List<Item>();

        public void Add(Item child)
        {
            child.parent = this;
            children.Add(child);
        }
    }



            Item TheParent = new Item();
            TheParent.itemName = "Bob";

            Item TheChild = new Item();
            TheChild.itemName = "lil bobby jr";
            TheChild.itemData = 10;

            TheParent.Add(TheChild);

            Console.WriteLine(TheChild.itemName + " my dad is " + TheChild.parent.itemName);
            Console.WriteLine(TheParent.itemName + " my son is " + TheParent.children[0].itemName);

            TheChild.itemName = "Big bobby jr";
            Console.WriteLine("Son did you just change your name without going thru me oh i see all grown up now? " +TheParent.children[0].itemName);

            TheParent.children[0].itemName = "Robert";
            Console.WriteLine("Dad im all grown up now what did you just call me " + TheChild.itemName);


            Console.WriteLine(TheParent.children[0].itemData);
            Console.WriteLine(TheChild.itemData);

            TheParent.children[0].itemData = 11;
            Console.WriteLine(TheParent.children[0].itemData);
            Console.WriteLine(TheChild.itemData);

/* Output…

lil bobby jr my dad is Bob
Bob my son is lil bobby jr
Son did you just change your name without going thru me oh i see all grown up now? Big bobby jr
Dad im all grown up now what did you just call me Robert

10
10
11
11
*/