Working with the programming language

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jond

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 .

Create or

var touchpos = event.position

or use


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

:bust_in_silhouette: Reply From: Zylann

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.

For example:

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 get_node("Thing") or $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

Jond | 2018-06-20 20:29