Recommended Posts

I feel like I should know this, but I tried looking it up to make sure I wasn't confusing myself, and couldn't seem to find a clear answer that made sense to me. When assigning something like this:

GridSquare randomPosition;

randomPosition = gridPositions[Random.Range(0,gridPositions.GetLength(0)),Random.Range(0,gridPositions.GetLength(1))]; 

Am I putting a copy of the GridSquare in gridPositions into the GridSquare randomPosition, or is randomPosition referencing the GridSquare at this location? If it's a copy, is there a way to reference it, like a pointer in c++, because that's what I want to do.

Share on other sites

You said "like c++" which suggests that your example code isn't c++. So... What language is it?

Share on other sites
Just now, masskonfuzion said:

You said "like c++" which suggests that your example code isn't c++. So... What language is it?

I'm sorry, it's c#. I guess I forgot to tag it.

Share on other sites

What is the type of gridPositions? is GridSquare a class or a struct?

Share on other sites

gridPositions is type GridSquare and GridSquare is a class.

Share on other sites
1 minute ago, ethancodes said:

gridPositions is type GridSquare and GridSquare is a class.

So you're saying that GridSquare has an index operator that returns another GridSquare?

public class GridSquare
{
public GridSquare this[int x, int y]
{
returns ?????
}
}

if GridSquare is a class then it is a reference type and unless you are doing something funky in the index operator (e.g. constructing a new GridSquare) then it will most likely return a reference to an existing object.

Share on other sites

I'm not sure what you mean with your above code? But in any case, you answered my question. I am using randomPosition to find a GridSquare from my array, and randomPosition is a reference to that GridSquare, not a copy of it. Thank you.

Share on other sites
5 hours ago, ethancodes said:

I'm not sure what you mean with your above code?

1

you said

17 hours ago, ethancodes said:

gridPositions is type GridSquare

I thought that seemed kind of odd, but what you meant was gridPositions is an array of GridSquares. That might seem pedantic, but it's an important detail.

Create an account

Register a new account

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• Forum Statistics

• Total Topics
634151
• Total Posts
3015819
×