Great job! I originally had it render instantly but I was having issues with how they were developing over time and so I added the slowed bit to help me solve those issues. It looked cool so I left it in.
I’m sure you could get it to render from top to bottom if you wanted, instead of main bolt then extra bits… and I have been thinking I should make the brightness of each strand decrease as a function of its length instead of just a set incremental decrease… I’m gonna try some stuff!
Ok I think I want to make a larger tutorial style explanation of classes, and I wrote a lot, like, a lot a lot of explanation, but I think I’m gonna make a larger “how to use classes” post (if that doesn’t exist already?) and maybe even a series of technical explanations, so yeah. The short answer is that they are like boxes for data basically, or more like instructions for how to create a box of data, and then you use the “new” keyword to create the boxes. And you’re right, they are VERY useful! The long answer is, um, whatever this rambling idiot wrote down there.
Ok so a class is basically...
Ok so a class is basically a definition for a data structure. Think of it like code that defines what a box should look like, a box that you put data into. Sprites, for example, are a class with variables like x, y, z, velocity, acceleration, image, hitbox… basically the class defines all that stuff, and then you use the keyword “new” to create a new box and shove the data into it.
The “constructor” is code that runs only when the box is first created, aka when the “new” keyword is used. You use the constructor to set up the box for doing stuff, like how when you create a sprite you have to give it an image and a sprite type, but the constructor does stuff like automatically place your new sprite in the center of the screen, give it a hitbox, set its velocity to 0, etc.
In the code for the lightning, I called the class “point” because I am unoriginal and boring, and you can see that the constructor takes in variables like a function does (it is a function) for the x, y, and the parent point. Then the constructor handles setting up all the variables that are all defaults when the object is created. I do a fancy thing in this code that makes it a little harder to understand, which is using the word “public” before the parameters in the constructor to basically make them variables of the class without ever defining them before the constructor. Normally you would define these variables and then set them in the constructor, but the way I did it just shortcuts that whole thing, which is convenient. Normally you would write it like this:
The word “public” can also be used before the variable definitions (the var: type things) elsewhere, but it’s not often necessary in classes because variables without a declared visibility are automatically public. The word “private”, on the other hand, is used before variable definitions to make that variable invisible to code outside of the class. This is useful when a feature of a class would break if a variable is used outside of it incorrectly, but personally I don’t often use it because it’s my code and I’m not really gonna share it with other people, especially in tiny projects like this. No, that is not good code practice, thanks for pointing that out! I do not care.
I used the word “static” in this project as well, which is basically saying that the variable is attached to the instructions for creating the box, and is not a different variable for every different box created. This is basically just an organization thing. It could just be a variable called something like “allPoints” that is a normal variable outside of the class, but instead I attached it to the class, so you use the code “point.all” to get the list of all points insead.
Then something that I didn’t do here is that you can attach functions to a class as well, so it doesn’t have to be just variables. This is great because attached functions can see any private variables that you have in the class and do stuff with them. I’m not gonna go too much into that, and there are also getter and setter functions and some other key words that I’m not gonna get into for this short (ha!) explanation.
This explanation could be soooo much longer but I’m gonna stop here… for now :3