+1 vote

If I want a Vector3 to be reset to all zeros every time I call physicsprocess(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.

in Engine by (153 points)

1 Answer

+1 vote
Best answer

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.

by (10,856 points)
selected by

Fantastic. Thanks

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.