ppb / pursuedpybear

A python game engine.
https://ppb.dev/
Artistic License 2.0
258 stars 98 forks source link

Make Vectors immutable #188

Closed AstraLuma closed 5 years ago

AstraLuma commented 5 years ago

Should we just make vectors immutable things? That seems like best practice?

pathunstrom commented 5 years ago

An issue for ppb_vector, but yeah. I think that's a thing we should probably do.

AstraLuma commented 5 years ago

An issue for both. It's pretty easy on the ppb-vector side. Problem is making sure all the use in ppb conforms to it.

ReblochonMasque commented 5 years ago

What is the rationale for making vectors immutable? Why is it important? Maybe it is a good idea, but in view of the many problems it creates (opened issues), maybe it is okay not to?

nbraud commented 5 years ago

@ReblochonMasque ppb-vector was changed to make the vectors it provides immutable, and that change is first available in version 1.0a1. There are a couple of places where pursuedpybear was mutating vectors in-place, and those needed to be updated (which is done in #204) for compatibility with newer versions of ppb-vector.

I'm not sure what are the issues you are referring to (esp. in ppb itself), but the rationale is pretty simple: it's both convenient and efficient to reuse vectors (say, if you are teleporting a player to a location, player.position = teleporter.position is the natural thing to do), but if those vectors are later-on mutated in-place, it will have unexpected effects (like someone moving the teleporter could also move players who teleported).

Since ppb is an engine that focuses on ergonomics and learnability, it makes sense to simply avoid surprising behaviours such as this one. Especially when the only cases of mutation we could find where internal to the engine itself.

ReblochonMasque commented 5 years ago

okay, thank you!

pathunstrom commented 5 years ago

I already liked @nbraud's response, but to confirm: as the person who made the decision to make them mutable in the first place, the least surprise principle absolutely applies and is why I've been behind this change since shortly after it was suggested.