Game Dev Diary

Day 4: Vectors with Daniel Shiffman! #

Today I watched the first four videos of Nature of Code 2 about vectors with A. I already know vectors fairly well but it’s always nice to watch Shiffman and also a refresher never hurts! But the main reason to watch them with A is to allow her to ask me any questions about parts she might not have understood well enough from the videos!

I did go on a bit of tangent to explain to A how Rust methods and functions differ from JavaScript’s everything-goes nature though!

The last video we watched is fully dedicated to static functions in p5.js. I wanted to take that opportunity to explain that what p5 does is nothing but a convention. There’s no reason why instance.add() couldn’t return a new instance instead of modifying the instance the method is being called on. I also explained how each JS library has to pick a convention and go with it and that the only way to find which convention is being used by a specific library is either try the library out or read the documentations for possible mentions of the behavior of its instance methods.

However, in Rust things are much nicer! I take us to the Bevy docs where I point to four different functions that each have a slightly different method signature.

And I explain the difference between each of those, firstly by explaining that the same behavior of JS static methods exists in JS but it’s easier to know a method is static because it doesn’t mention self in its parameters.

Next I jumped directly to borrowing and I explained that if a function is borrowing it can read the instance’s data but not modify it, so if we had a vec2.add(vec2) like in p5 and its signature used &self then we know for sure that function will not be able to touch our vec2 instance! (Yes internal mutability makes this MUCH more complicated but simplifying the explanation for now is for the better of learning.)

By contract mutating methods says they will mutate the instance! And usually when a function asks for mutable access that means it will actually use it because otherwise!

Finally consuming methods are a bit unique to Rust (at least compared to JS), and they are functions that consume the instance, the instance cannot be used after they are called. I still never explained Copy types so if you’re reading this and you wonder how the heck vec2's + is consuming but you can still use the vector afterwards, that’s because Copy types can be copied before they are consumed!

And that’s all for the day! ❤️