This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+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 (155 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 (22,704 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 Frequently asked questions and 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 [email protected] with your username.