Topic was automatically imported from the old Question2Answer platform.
Asked By
extcFPjkCLsDC2F
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
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.