What happens when I create a certain object every time I call a method?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maxwellc055

If I want a Vector3 to be reset to all zeros every time I call _physics_process(delta), is it wasteful/inefficient to say
func _physics_process(delta): -> var x = Vector3.ZERO
Should I instead place var x = Vector3.ZERO in my member variables and then simply reset it each time?
I’m just trying to figure out the advantages and disadvantages of each!
I see the first option quite often, but something about creating a new one each time just doesn’t sit right with me. On the contrary, I’d rather not clutter up my member variables if I don’t need to.

:bust_in_silhouette: Reply From: jgodfrey

It’s not wasteful or inefficient to create local variables. If the variable is only needed locally, create it locally (as in your above example). If it’s needed in multiple places in the script, it’s typically created globally (outside of any function) and referenced as needed throughout the script.

Fantastic. Thanks

maxwellc055 | 2020-09-11 19:42