Skip to content
Advertisement

Why is a hg.Vec3() vector passed to a Python function modified by it?

I’m working on a 3D project in Python using the HARFANG 3D wheel (I got from Pypi).

I have a function where I pass a series of data, including a vector3:

JavaScript

I don’t return position and it is not a global variable. However, when returning from this function, position is modified within the scope of the caller.

How to I prevent this to happen :(

Advertisement

Answer

What happens here is that your Vec3 is passed by reference. This is the expected behavior, you can experiment further:

JavaScript

Design-wise, you might question why Vec3 are passed by reference and not by copy. My answer is that Vec3 is the most wanted type of data in a 3D project/3D engine. You potentially process hundreds if not thousands of vectors per frame, so you’d better have an engine that, by default, does its best to improve performance.

How to I prevent this to happen :(

You need to instruct HARFANG that you want a copy of your vector. For example, the function invocation could look like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement