Okay, we're close now. We're close now okay um so
Here's the thing maybe hopefully you worked on that exercise
What do we have now? We have an array?
It's got two spots in it. Pretend for a moment that it had three spots in it.
And what does our code look like? It looks like this. Let's say we want to display all of the bubbles.
We have to say bubbles index zero display, bubbles index one display, bubbles index two display. We...
With the knowledge that we have right now the way that we've done everything we have three lines of code
This leads us to wonder when we have a thousand bubbles will we have a thousand lines of code?
I think I've said that in every single video up until now. Maybe I'm just repeating myself.
I have no idea, but this is the concept. This is where we're going. This is the gold. This is very exciting.
Pretend you're excited. Just try. I'm trying. I'm trying to pretend. I do feel genuinely excited.
Sort of. Okay, so...
How we gonna do this? Let's look at...
Something we learned about a few videos back. Let's look at a loop.
Let's... I'm gonna write out this loop for you. for
Actually, this is exciting. I just got excited about it. Okay. int t equals 0. i is less than 3. i plus
plus, and I'm gonna write inside this line of code. I mean this loop. Inside the structure.
I'm going to write print
ln i.
Okay, let's actually... This is... this is momentous. Let's actually go and and actually and take a look at the output of this.
println(i). When I run this...
What's down at the bottom? Down here if you could see that. 0, 1 & 2. What prints out? First
i is 0. Is i less... 0 less than 3? Yes it is. Print i 0. i equals 0. Then...
Then... then i plus plus. i is 1. Is 1 less than 3?
Yes, print i. 1. Is.... i plus plus. i is 2. Is 2 less than 3. Yes print
i. Yes, i plus plus. i equals 3. Is 3 less than 3. No. Exit the loop.
This is the output we get from this loop.
Notice that.... notice notice anything similar? Take a look at these values and take a look at these values.
This just so happens to be a structure where we have a variable called
i... that starts at 0 then becomes 1 then becomes 2 and then completes. Well
isn't that what we needed to do? We needed to display Bubble 0, then display Bubble 1, then display bubbles 2. What if
instead of saying print i here. We said bubbles...
index i dot display.
What we've now done is we've essentially said...
"Instead of
individually referencing each
index with a hard-coded number, let's use a variable that counts through all those numbers and
repeat over and over again." Bubbles index i for when i is 0. Bubbles index
i for when i is 1. Bubbles index i for when i is 2 and we have now condensed these three lines of code
essentially into one line of code.
It's 3 with the loop part, it's actually four with the print,
but, you know,
once this goes all the way up to 999.
This is still going to be the same length. The only thing that's going to change here is we want i to go up to
a thousand. So let's look. Let's actually... This is this is huge! By the way.
You know... you... Let's put this into our program and see what happens.
Ok
We don't need to save that.
So let's go down here and say ok for int i equals 0, i is less than... and right now
we have two, i plus plus and
we don't need both of these anymore.
All we need... is to reference index i.
Let's run this program and see if we get the same result.
We get the same result. Instead of having to say: "ascend, display and top for 0.
ascend, display and top for one. We now just say it for i.
For i. i
Okay
Pretty good so far, but we still initialized each bubble individually you know, what if we want it to have...
Different sizes? What are we going to do? Well, let's, don't worry about that right now. Let's take this
and go up here and do the same thing for initializing
Bubbles index i. Let's run this again. Now instead of having to initialize each one individually
we're initializing each one in that loop. They all have the same size, but we'll get to that in a moment.
Great. We're close five minutes 30 seconds. Actually that's probably not the time you see. That's a arbitrary time that I put up there.
Let's change this now. Let's not get too crazy. Let's just make this 20, okay now. We got 20 bubbles.
Let's run it. Let's run. It let's run it two bubbles. Shoot what happened?
Well, we updated the array to have 20 spots in it, but our loop is still only looping to two.
So we're only using two parts of the array even though the array itself has 20 spots.
So we could fix this by changing this to 20 and this to 20.
Now let's run this. Ah
20 bubbles, oh my god, okay. There's 20 bubbles now and look... by the way
I should really point out something very very important incredibly important notice this bubble tab over here.
We haven't touched this once.
This whole... all of these videos about arrays, we've never touched this bubble class. We're not doing anything to the object that's different.
It's the same object. All we're doing is figuring out how to make a list of many of them to have one or many the
object is still the same. So...
Let's go. Let's go for it. Let's go for let's just go for a thousand.
And by the way, this is driving me crazy.
I'm gonna have a thousando. I got to change the thousand here
and then here and then here. I'm going to change twenty one thousand, twenty one thousand. This is kind of a problem because
really you could be referencing this array in a
zillion places in your code.
Well one of the things about an array that I haven't pointed out to you is it has a property.
A length property. We can actually access the length of that array directly dynamically.
So instead of typing 20 here I can say...
bubbles dot length and
I can say bubbles
dot length
And now we have 20 bubbles. It's the same program, but watch this I could go back and say 3.
3 bubbles and here we go here we go. oh
Get ready, you can ready to like stand up and applaud.
Which you're totally not gonna do because why would you that would be weird. Um....
We got a thousand. Oh, it's so sad.
There's a thousand bubbles there, but it's running kind of slow. So we kind of ran into something here.
We ran into the fact that it's slow to draw a ton of stuff on the screen.
This is actually kind of a little bit of an aside, but an important point nonetheless. That...
Generally speaking the thing that's going to make your Processing program run slow is drawing too much stuff.
Not calculating too many numbers or having too many lines of code, but drawing too much stuff. Now at some pointwe'll have to have a video.
I'm sure we could get... I know for a fact we could get this to run faster through various
optimizations in particular using a different drawing mode with hardware acceleration which I'm just kind of rambling about right now.
We could even just try that by the way by saying, I'm gonna change it to P2D.
Which is just throwing a random thing into this video.
Yeah, that's better. It's kind of working anyways it's a little bit faster.
But more likely... so what... what... by the way let me just mention what I did.
P2D is a
What I've done here in the size function is in the last parameter I can specify what
behind-the-scenes rendering engine do I want to use. This really is nothing to do with this the array topic.
But I can't stop. Um... and... and what's the deal with that. It's, you know, you have to imagine like there's this team...
this team of people sitting inside of Processing and
they're just waiting to like draw the pixels on the screen. But there's like two separate teams.
There's the default team when you don't say anything in size, and there's a P2D team.
There's also the P3D team for when you're like: "I need to draw stuff in 3D."
And they jump into action and start drawing stuff. P2D and P3D use the graphics card in a particular way.
Farm out a lot of the rendering to the graphics card which can make things a lot faster.
They use opengl which is a term you can now google look up if you're not familiar with it.
But let's just go back and make 300 bubbles and get rid of P2D.
And this is a nice place to sort of just like watch our bubbles.
Uh... you know, 300. I can't tell actually if that's running slow or fast or what. I think all that alpha.
You know it's actually slowing things down.
This is quite...
All that alpha makes things pretty slow.
To make it look like a bubble. We don't need a fill and watch how much faster it's gonna be. Look at that.
Lhat's nice and fast now I think.
I don't know what's slow, what's fast anymore. So they don't really look like bubbles.
But whatever. Um so... It's funny because my I did this the other day
they really look like bubbles, and I don't know what this looks like.
They're probably gonna shut off because I think I need to stop now. Okay, so...
There's a.... there's okay.... There's a couple of nuances to this. Edit the last one minute out
please. There's a couple of nuances to this.
One is that
when you're initializing a bunch of objects this way... You only have one line of code to initialize the objects. In essence
we're initializing every object identically. So one thing that we can look at here is that?
Why are they? Why are there? Why is there variation?
It's because in the constructor a lot of the parameters are set randomly. x is a random location. The speed is a random value.
So this is a way
we have a wide variety and there might be some other algorithm for which you would initialize these parameters.
But one thing that you can do that is quite common. Let's just go back to like
25 Bubbles for a second.
There's 25 bubbles, okay. Let's go back and see those 25 bubbles
What if I did this and this is going to be a little bit confusing and i
times
four. 4. Let's run this.
Notice that I have
bubbles of varying size. In fact each bubble has a particularly unique size. The first one is 0.
The second one is four. The next one is 8. The next one is
12... right? The next one is 16. The next one is 20, right?
I'm using the fact that I'm counting through bubbles 0. I'm using the fact that its index 0 to do it, assign a size.
Index is 1 to assign a size. This is one way of doing it. I could also have just done something random right here.
And now I have bubbles of all random sizes. So the point is
what's happening now, and I can't believe this is - oh that stopped recording a while ago
or just now. Alright, in a minute.
I'm just over here. This... this stopped, but this is definitely gonna cut out of me any second. Um so try this.
Try now, make your objects, make an array.
Use a loop and see how do you initialize that array of objects. Are they all the same?
Is it through random? Is it an algorithm based on counting? Give that a try and see how you do.