Hi guys,
What is the best practice for functions that need to return a PVector, given that it’s a reference type?
Consider the below code
void SomeBigCalculation () { // Just temporarily need to know the relative position of the ship PVector relativePos = CalculateRelativePos(ship.position); \\ do something with relativePos... } PVector CalculateRelativePos(PVector location) { return new PVector(location.x + 1.0, location.y + 1.0); }
So in this case, we’re minting a new PVector with every call to CalculateRelativePos(). That’s not so bad here, but it feels very inefficient to do this many times in a frame.
So in my program I have created vectors that exist purely as a “cache” for vector calculations, like this:
PVector _vecCache = new PVector(); void SomeBigCalculation () { CalculateRelativePos(ship.position, _vecCache); \\ do something with _vecCache... } void CalculateRelativePos(PVector location, PVector result) { result.x = location.x + 1.0; result.y = location.y + 1.0; }
…but this is obviously very ugly, as for one thing, we’ve lost the clear naming in the calling function. For another, I can no longer chain these function calls together.
that’s what you want to avoid. (I think Divitiacus overlooked this.)
to make it less inefficient (as you wrote), we don’t want to use new throughout but only set() command.
I don’t know if this is really more efficient though.
Here is my idea:
Making relativePos global, only one new command needed. Good.
Use set() to assign it a new value. Hopefully faster than new.
Disadvantage: an additional global var. Not so nice.
Nice way of calling CalculateRelativePos() without any other PVector involved imho.
PVector relativePos = new PVector(0, 0);
void SomeBigCalculation () {
// Just temporarily need to know the relative position of the ship
relativePos.set ( CalculateRelativePos(ship.position) );
\\ do something with relativePos...
}
PVector CalculateRelativePos(PVector location) {
return new PVector(location.x + 1.0, location.y + 1.0);
}
to make it less inefficient (as you wrote), we don’t want to use new throughout but only set() command.
I don’t know if this is really more efficient though.
I would be interested to know, if there is an efficiency loss, when “creating” all the variables new with each call of the function.
I do that intentionally that I “create” all variables new, which I need in a function, with each call. This way I can be sure, that there are no headaches with carry over values from previous uses of the function.
I would find it very inefficient from a programming perspective to make all variables globals and then to police them through the whole program code.
But I would like to know, if that really slows anything down.
Great thanks for the responses, and sorry for the poor code formatting on my part.
I will try the “use target if non-null, otherwise return new” approach