Bit confused about making the list/array type WayPoint

Hey,

I am not fully sure why did we make the list of type WayPoint when the WayPoint.cs script only contains “isPlaceable” functionality…

i get that the Waypoint script is on each tile, but would any other script that would have been there function the same?

if someone can please explain me in details what actually happens when we did it and what was the rational behind it I would be thankful.

M

Hi! Welcome to the community!

Some could argue that it’s to be sure that every object added to the list is of the desired type, but Gary’s code doesn’t include any type of safety measure to avoid adding different type of objects, so, it’s not that.

I suppose it’s to get a little ahead of what’s to come, if I remember correctly, you’ll use the Waypoint reference in a more specific way later on. Gary, the instructor, probably decided that it was better to get ahead of things so students would have to make some heavy changes in a lot of scripts, I’ve seen students get lost very easily when that happens.

At this point in the course, you could use the Transform component instead of the Waypoint, there’s nothing special about this approach, but as I said, it’s probably not a good idea to do that if Gary is planning to use some of the Waypoint’s functionality later on.

1 Like

So practically when we make a list of type of any component in a certain game object it will create in that list all the gameObjects in the project that contains this specific component?

Just for an example to be sure I get it, would a:
List would collect inside all the items with Rigidbody in the project?

Also, how would the Transform suggested by you will look like?

Thank you for taking the time and responding/welcoming me :pray:t2:

M

1 Like

Yes, and no, a List<>, as the one created in the project (there are other types of lists), can contain objects of a specified type, that the yes part, Which objects it contains? The ones you put in it. Lists are null by default, and new lists are empty until you put something in them.

I’ll explain further.

[SerializeField] List<Waypoint> path = new List<Waypoint>();

This line of code creates a new list that can hold objects of the type Waypoint.

List<Waypoint> path;

The line above would throw an error because that variable is null, it doesn’t have a list, yet, you have to create the list with the keyword new, just as Gary did.

If you leave the list as in the first example, the one I took from the course, you can see that it is empty in the inspector. To populate it Gary searched for every object with the tag “Path”, and then added it to the list.

    void FindPath()
    {
        path.Clear();

        GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");

        foreach(GameObject waypoint in waypoints) 
        {
            path.Add(waypoint.GetComponent<Waypoint>()); //Here
        }
    }

He’s not looking for the Waypoint component, he’s looking for the tag, that’s why you need to use the GetComponent method so that you can add them to the list, otherwise, it wouldn’t work, you can’t add objects that aren’t of the same type as the one the list wants.

I’ll give you an example using the Transform component in a few hours.

1 Like

Thank you very much.
Before anything your effort responding to me is REALLY appropriated and not taken for granted.

Just one last thing for me to understand.

Does List create a type WayPoint or is there something else that defined WayPoint as a type that I miss?

Essentially what confuse me is the lack of understanding of the script WayPoint attached to tiles is what made this Type exist or if having “WayPoint” in the List deceleration is what created it and in this case it could have been also List and it wouldn’t matter as long as we populated it with the “path” tag.

1 Like

The type exists because of the script, the moment you create a script you are also creating a new type.

If you want to know more about types I highly suggest you read this, it’s long but contains great information:

Yes, the type, at this point, doesn’t really matter, just keep in mind that is not the tag that is populating because tags are not a type, they are a label to easily access an object with that tag. Tags are of type string, you can fill a list with strings, but that wouldn’t give you a reference to the object in the scene, which is what we need in this case.

You can use the transform component of the objects as the reference instead.

    [SerializeField] List<Transform> path = new List<Transform>();

    void FindPath()
    {
        path.Clear();

        GameObject[] tiles = GameObject.FindGameObjectsWithTag("Path");

        foreach(GameObject tile in tiles) 
        {
            path.Add(tile.transform);
        }
    }

This example code would result in the same as using waypoints, but don’t do it unless you are confident you won’t have any trouble later on.

2 Likes

Thank you, I went over the the microsoft link you sent me, it was very helpful (and painful lol).
so if I got it right (i think I did), the key point here is about inheritance and the fact that when I am creating a list of a certain object I am also gaining access to all other attributes that werent marked as sealed, hence it wouldnt matter if I were using the Transform (though seems like a safer option as evey gameObject contain it) or Waypoints or any other component added in the future, as all would give me access to the transform.position later on.

1 Like

Yes, you pretty much nailed it, just keep in mind that using Waypoint might not be as safe but it’s actually better, because you don’t want to accidentally add other type of object in the list.

1 Like

Great. Thanks again :pray:t2:

1 Like

I’m glad Yee was able to help you. Are all your questions answered? :slight_smile:


See also:

yeah, he is amazing. its all clear now, but I think its worth adding some pause and clarification to the video of such things to make sure students really understand the idea in depth rather than just following and not being able to use this later on when they try their own things.

also, on the following course there is an objectPolling video done with array where I found that there is a much better built-in ObjectPool functionality that handle the heap in a more effiecient way and works a bit better than aray decleration even though its based on the same idea.

2 Likes

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

Privacy & Terms