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.
0 votes

In Computer Science classes, I've been taught that variables should be declared before any processes take place.
In Godot however, you will see people write code in opposition of this.
Is there any significance in actually declaring variables before or during processing?

in Engine by (73 points)

1 Answer

+2 votes
Best answer

It depends on the scope of the variable.

A variable may be used only by :
- a few lines of code (a "for loop" use a local variable with implicit declaration)
- a function
- a class

You can also define a global variable with the singleton (AutoLoad) technic in Godot.

As a variable consumes memory, it is better for efficiency and performance to choose the right scope for each variable.

This advice is general regardless of the programming language you use.
Today we have computers with a large memory, but when I started to learn programming, it was not.

by (61 points)
selected by

Thanks for the answer. This is helpful to know.

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.