Why are so many coordinates

so im watching this video and ive noticed that he uses some variables types and names that are used more than once. are these supposed to be referring to the same thing? for instance he has the vector2int coordinates in the create grid method but then he uses vector2int coordinates again in the get method. can someone explain why he is using it multiple times its really confusing

Variables which are declared in a method are unique to that method. The value passed in to GetNode(Vector2Int coordinates) doesn’t need to use the same name as the Vector2Int from CreateGrid() and you can check this by changing one or both variable names to different things.

The reason to use “coordinates” for both variables is because that’s what they are, it’s a logical name for them. It’s definitely confusing when you’re first starting out, but once you get the hang of things you’ll likely find it useful to reuse variable names like this so you can easily see the connection. Although they’re different variables, ultimately GetNode is going to be looking for the coordinates which were put into the Grid made from CreateGrid

I hope that made a little sense!

1 Like

yes it does he just zooms thru the lecture so its hard to understand why things are done when im trying to keep up with the lecture. thanks. i also have one more question if your up for it. could you tell me what null means. i looked it up and it seems to represent any value. i just wanna be sure that i understand fully so when i have to write null i know exactly what it is im saying and when i need to use it

1 Like

I’m definitely not the best person to answer that, but my own understanding of null is that it’s a non-value. When it’s used for null checks like if(variable == null) we’re seeing if something exists, usually to avoid errors. If we’re using return null; it means we’re expected to return something to satisfy a method and we don’t want to send specific information.

If we look at the GetNode method in GridManager from this point in the project:

    public Node GetNode(Vector2Int coordinates) // This method MUST return a Node
    {
        if(grid.ContainsKey(coordinates)) // We use the Vector2Int which was sent when calling the method to check if they exist within our grid
        {
            return grid[coordinates]; // If the coordinates exist in the grid we can return the Node which they match to
        }

        return null; // If the coordinates do not exist we still have to return something to satisfy the method. We return null, basically telling the method 'the Node at these coordinates doesn't exist'
    }

hmm so if we put null it means we want a value returned but may not know what data type it is? i think i may have also heard that it could mean zero but we dont want to necessarily put something to equal 0 because of the decimals that may make it hard to equal 0 exactly so we put null to be the smallest number we can get

The data type will always be what was asked for, in this case it’s a Node but it could be anything. Because of this, different variables will interpret a null value in different ways. For example a bool will consider null to mean false, and (I’m pretty sure) an int will consider null to be 0.

I’m not familiar with using null to get a number close to zero. This sounds somewhat like comparing a float to 0 which can be tricky to do, but that doesn’t involve using null. In that case, instead of checking if(float == 0) it would be safer to check if(float > 0.1)

oh i see whats confusing me now is how do we know what null is supposed to be for node. i dont think the instructor said what it was supposed to mean so its just a random word sitting there in code confusing me. i just finished the 137 video and my labels are not changing color so im working to figure that out but it would be nice if i knew what null was supposed to be so i could determine if that was the problem

In this case we’re using null just to check if a Node exists or not. The place it’s being used is the CoordinateLabeler script in the SetLabelColor() method:

void SetLabelColor()
    {
        if(gridManager == null) { return; }

        Node node = gridManager.GetNode(coordinates);

        if(node == null) { return; }

        if(!node.isWalkable)
        {
            label.color = blockedColor;
        }
        else if(node.isPath)
        {
            label.color = pathColor;
        }
        else if(node.isExplored)
        {
            label.color = exploredColor;
        }
        else
        {
            label.color = defaultColor;
        }
    }

You can see there are two different null checks near the top of the method.

The first checks if gridManager is null, if it is then the method stops there with a return; Without that check, the code would then try to call gridManager.GetNode(coordinates) which will give an error if gridManager doesn’t exist.

Likewise the second check: if(node == null) { return; } This is where the return null; from the GridManager Node GetNode(Vector2Int coordinates) becomes relevant. It’s specifically asking whether the Node is null, and if it is null it needs to stop running this method because the following lines want to work with a Node which actually exists.

what your saying makes sense im just having a hard time wrapping my head around it on my own. like when i see some of the things weve been doing in the lectures lately i need to break it down to myself in individual parts then combine them to understand what these different variables or lines mean

I know how you feel! This section of the course felt like a jump in difficulty for me as well and I definitely didn’t understand all of the code myself. Take your time and ask questions like you’re doing. :slight_smile: You’ll grasp more and more as you continue to code, for me it took a while until certain simple things clicked long after I’d first started to learn about them.

yea im using this course as a way to get these concepts in my head cus ive only been studying coding since december. i dont need to understand everything in this course just yet cus im gonna be doing at least a full year of studying so that i can grasp what is being taught but i also dont want the things being taught to fly in one ear and out the other. but thanks for all the help youve been given me i really appreciate it. i do ask a ton of questions its the best way for me to learn us it allows me to put what people say into my own words instead of just copying and pasting what the instructors are doing

1 Like

Yes, please do keep asking questions if you want to know/learn something. Other people might have the same questions as you, and they’ll very likely benefit from your questions and the answers. :slight_smile:


Realm Rush appears to be complex but, actually, it is fairly straightforward. Just the pathfinding algorithm is a bit more complex.

If you ignore the code and Unity for a moment and just think about how you would define the rules and concepts with pen and paper, you’ll very likely come up with a similar logic flow as Gary. And if you read his code again, you will notice that most if not all your ideas are reflected by his code.

If you write your own code based on your concept on paper, it might be that it looks a bit different than Gary’s. That’s fine. In the end, you identify cases, and tell the computer what it is supposed to do depending on the cases. The difficult part is usually to identify all cases including edge cases. The SetLabelColor() method @Satinel shared is a good example of what I mean.


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms