|Topic was automatically imported from the old Question2Answer platform.
Hi, I’m not good at programming.
Tell me how to properly, for performance.
I know it doesn’t make much difference but the difference should be .
var touchpos = event.position
if I already know, why create a new variable? Or will the compiler remove the garbage?
Or how it works
This applies not only to Godot but also to other languages
There isn’t much I can say here, but I personally tend to use local vars if I have to rewrite the same expression multiple times.
var pos = event.position
print("Moved to ", pos)
something.scrolling.x = pos.x
This way I don’t have to rewrite
event.position every time.
In GDScript, this has also a very small performance benefit if you access the variable many times, because when you write
something.position, there is a lookup cost involved in order to access
position. While when you write just
pos, the value is accessible directly.
This doesn’t apply to compiled, typed languages because they know the type of things beforehands so the lookup cost can be avoided at compilation time.
This is even more relevant when you do
$Thing, because storing this in a variable eliminates the cost of getting the node again (regardless of the language here, because nodes are a Godot thing).
I don’t know what you mean by “garbage”.
garbage - I thought maybe there is a compilation of gds, where the local vars are grouped if for example, you create 10 of the same in one script