About counting Blocks

I really didn’t understand about this course,

in this course, we learn how to count the block right.
but I can’t understand the logic behind this what is a correlation between block and level.

Can someone explain about this?

Thanks you

2 Likes

In the next lecture Rick will explain what’s the relation between how many blocks you have in your scene and the level, but if you want a spoiler.

The amount of blocks in the scene dictate if you have or haven’t finished the level

sorry that is what i mean, how the code can count the brick?

the code seem didnt call anything else?

Let say if i change the all block word into ball(from level.cs and block.cs), is it still counting block?

it is me again, after learn about the next course i learn about

// chaced reference

what is the function?
usually we put another game object on serialized field to connect with code.
is that related with counting blocks?

1 Like

Cached reference is a term that means to save something for future and quick reference, in this case, we are caching the Level script in each of the blocks because we need them to communicate with that script at least two times during a game session. There’s no particular method or function to do that since it’s just a term, but in this particular case we are using this line to cache a reference of the Level script:

level = FindObjectOfType<Level>();

Now onto how the game is counting blocks:

Each block has a script called block, inside it, in the Start function, there’s a line of code that adds a block to the count:

    private void Start()
    {
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks(); //<--- This one here
    }

That line is calling another method inside the Level script which adds one to the count:

    public void CountBreakableBlocks()
    {
        breakableBlocks++;
    }

Rick is using this method instead of using the serialize field property because each level will, probably, have a different amount of blocks, this removes the work of having to add the blocks manually in each level, which also removes potential bugs like adding fewer blocks than the ones that are on the screen or adding more blocks, thus creating unexpected behavior like the game ending earlier or never finishing.

1 Like

I see.
another question.

for I understand about to assign the variable.

is " chaced reference" like an “assign variable”?

if yes when I assign a variable, I can use int, float, bool, and string (anything else?)
what i can use for “chaced”?

1 Like

When someone talks about caching, it basically means that you need to store a reference to an object, script, or component, while assigning means well, the same, but not necessarily for objects. This means that caching is the same as assigning but assigning is not the same as caching.

Example: This code is assigning or caching the reference of the Rigidbody component.

Rigidbody rb;

rb = GetComponent<Rigidbody>();

In here the code is assigning the value of a variable of type float.

float _myInt;

_myInt = GetComponent<Rigidbody>().velocity.x;

Don’t get too caught up on this, this is just coders talk, I really haven’t seen a case in Object Oriented programming where using the term cache or assign had any sort of repercussion, neither negative nor positive, this are terms that are more important in other types of programming, so, say assign, say cache, it truly doesn’t matter since the syntax can be pretty much identical, it’s just a technicality.

2 Likes

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

Privacy & Terms