0 votes

Do exist a performance difference between following code example parts ? Basically do exist performance difference if to make variables inside function block or outside.

var something = Vector2(1.0,1.0)
var something2 = Vector2(1.0,1.0)

func _process(delta):
    var forecast_pos : Vector2
    forecast_pos = something + something2

or

var something = Vector2(1.0,1.0)
var something2 = Vector2(1.0,1.0)
var forecast_pos : Vector2   

func _process(delta):
    forecast_pos = something + something2
in Engine by (20 points)

1 Answer

0 votes

Any performance differences will be generally be too minor to be concerned about. So, don't make that decision based on performance. Rather, make it based on limiting access to a given variable only to the minimum scope required by your design.

If a variable is only used within the scope of a single function, then (most definitely) define it within the function. On the other hand, if a variable is needed in multiple functions, then define it at the global scope of the script.

Bottom line - there are valid reasons to have a variables scope be either local or global, but performance is not one of them.

by (10,916 points)
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.