Constants | Godot GDScript Tutorial | Ep 03

Godot Tutorials
8 Apr 202007:10

Summary

TLDRThe video script explains the concept of constants in computer science, specifically within the context of GDScript used in game development. It details how to declare constants with the 'Const' keyword, emphasizing their immutability post-declaration. The script advocates for the use of constants over literals for better code readability, easier debugging, and efficient code maintenance. It illustrates the benefits with examples, such as changing game item speeds by modifying a single constant instead of numerous hardcoded values, and discusses the importance of naming conventions to visually distinguish constants from variables.

Takeaways

  • 📝 Constants in computer science are values that do not change.
  • 🔑 In GDScript, constants are declared with the keyword 'Const' followed by a unique name, an equal sign, and a value.
  • 🈲️ Assigning an empty constant or a constant without a value is not allowed.
  • 🔄 The difference between a variable and a constant is the keyword used; everything else is the same.
  • 📊 Data types can be explicitly added to constants with a colon and data type name, but it's optional as the type is inferred.
  • 🚫 Constants are read-only and cannot be modified after declaration and assignment.
  • 👍 Using constants is beneficial for debugging, editing, and reading code.
  • 💡 Beginners often use literals instead of constants, but constants improve code readability and maintainability.
  • 🔍 Constants should be visually distinct from variables, often using uppercase letters for easy identification.
  • 🔄 Changing a constant's value automatically propagates the change throughout the code where the constant is used.
  • 🎮 Constants are ideal for values that will not change during the lifecycle of a game, such as player dimensions.

Q & A

  • What is a constant in computer science?

    -In computer science, a constant is a value that does not change throughout the execution of a program.

  • How do you declare a constant in GDScript?

    -In GDScript, constants are declared using the keyword 'const' followed by a unique name, an equal sign, and a value.

  • Is it possible to assign an empty value to a constant in GDScript?

    -No, you cannot assign an empty constant; it must have an assigned value upon declaration.

  • What is the difference between a variable and a constant in GDScript?

    -The only difference between a variable and a constant in GDScript is the keyword used for declaration; 'var' for variables and 'const' for constants.

  • Can you modify the value of a constant after it has been declared in GDScript?

    -No, constants in GDScript are read-only after declaration, and modifications are not permitted.

  • Is it necessary to specify a data type when declaring a constant in GDScript?

    -Specifying a data type when declaring a constant is not necessary as the data type is inferred from the assigned value, but you can explicitly add a data type for clarity.

  • Why should beginners avoid using literal values and instead use constants?

    -Using constants instead of literals makes code more readable, easier to debug, and simplifies the process of editing code, especially when the same value is used in multiple places.

  • What is the advantage of using constants over literals in a large codebase?

    -Using constants in a large codebase allows for easier maintenance and updating of values. Changing the value of a constant will propagate the change throughout the code where the constant is used, instead of having to find and replace every instance of the literal value.

  • What is the recommended naming convention for constants in GDScript to differentiate them from variables?

    -A recommended naming convention for constants is to start their names with an uppercase letter, while variables start with a lowercase letter, making it visually clear which is which when reading the code.

  • Can you provide an example of how using constants can simplify changing a value in a large codebase?

    -If a game's speed value is initially set as a literal 5 in multiple variables, changing it to 10 later would require finding and editing each instance of the literal 5. However, if the speed is declared as a constant, changing it to 10 would automatically update all instances where the constant is used, reducing the potential for errors and saving time.

Outlines

00:00

📝 Understanding Constants in GDScript

This paragraph explains the concept of constants in computer science, specifically within the context of GDScript used in Godot game engine development. Constants are fixed values that do not change throughout the program's execution. The declaration of a constant involves using the 'const' keyword, followed by a unique name, an equal sign, and a value. Assigning a value to a constant is mandatory, and attempting to change a constant's value post-declaration results in an error. The paragraph also touches on the importance of using constants over literals for better code readability, easier debugging, and maintenance. An example is provided where changing a game item's speed from 5 to 10 requires altering multiple lines of code when using literals, versus changing just one line when using a constant.

05:00

🔑 The Power of Constants in Code Efficiency

This paragraph emphasizes the benefits of using constants over hardcoded literal values in programming, particularly in game development. It illustrates how constants can significantly reduce the time and effort required for code changes. By using a constant for values that are unlikely to change, such as a player's height or width, any necessary adjustments can be made by simply altering one line of code, which then propagates throughout the entire codebase. The paragraph also discusses the importance of naming conventions for constants, suggesting that they should be visually distinct from variables to aid in code recognition and maintenance. Examples of such conventions include starting constants with uppercase letters, while variables begin with lowercase, although personal preference is acknowledged in choosing a convention.

Mindmap

Keywords

💡Constants

In computer science, 'constants' refer to values that are set once and do not change throughout the program's execution. In the context of the video, constants in GDScript are declared with the 'Const' keyword, followed by a unique name and an assigned value. The video emphasizes the importance of using constants for values that remain unchanged, such as game settings or parameters, to improve code readability and maintainability.

💡Declaration

The term 'declaration' in programming is the act of introducing a new variable or constant to the code. In the video, it explains how to declare a constant in GDScript using the 'Const' keyword, an equal sign, and a value. Proper declaration is crucial for defining what the constant represents and ensuring it is used correctly throughout the code.

💡Unique Name

A 'unique name' in programming ensures that each constant or variable has a distinct identifier. The video script highlights the necessity of using unique names for constants to avoid conflicts and errors in the code. This is essential for organizing code and ensuring that each constant can be easily referenced without ambiguity.

💡Datatype

A 'datatype' defines the kind of value a variable or constant can hold, such as integer, float, or string. The video mentions that while it's not mandatory to explicitly declare a datatype for a constant in GDScript, as the type is inferred from the assigned value, doing so can enhance code clarity. For example, declaring a constant with an integer datatype makes it clear that the constant holds a numerical value.

💡Literal

A 'literal' in programming is a representation of a fixed value in code, such as the number 1 or the string 'hello'. The video contrasts the use of literals with constants, suggesting that while literals can be used, constants are preferable for values that should not change, as they provide a centralized point of reference in the code.

💡Read-Only

'Read-Only' refers to the property of a value that cannot be modified after its initial assignment. In the video, it is mentioned that constants are read-only in GDScript, meaning that once a constant is declared and assigned a value, it cannot be changed, which helps prevent accidental modifications and ensures the stability of the program.

💡Debugging

Debugging is the process of finding and fixing errors in code. The video script suggests that using constants can make debugging easier because if a constant value needs to be changed for testing or adjustment, only the constant declaration needs to be modified, and the change will propagate throughout the code where the constant is used.

💡Maintainability

Maintainability refers to how easy it is to update, modify, or debug a piece of software. The video emphasizes the role of constants in improving maintainability by centralizing the definition of values that are used throughout the code, making it easier to make global changes without manually editing each instance.

💡Naming Convention

A 'naming convention' is a set of rules for how identifiers in code should be named to make the code more readable and understandable. The video recommends using a naming convention that visually distinguishes constants from variables, such as starting constants with an uppercase letter, to help programmers quickly identify the nature of the identifier at a glance.

💡Hard-Coding

Hard-coding is the practice of embedding values directly into the source code. The video discourages hard-coding values like game settings, instead advocating for the use of constants. This approach allows for easier adjustments and reduces the risk of errors when values need to be changed, as demonstrated by the example of changing game item speeds.

Highlights

A constant in computer science is a value that never changes.

In GD script, constants are declared with the keyword 'Const' followed by a unique name, an equal sign, and a value.

Assigning an empty constant is not allowed; a constant must have an assigned value.

The distinction between a variable and a constant is the keyword used; otherwise, the declaration is the same.

Data type can be explicitly added to a constant declaration for clarity.

The data type of a constant is inferred from the assigned value if not explicitly stated.

The format for declaring a constant includes the keyword 'Const', a unique name, optional data type, and an equal sign followed by a literal or data object.

Constants are read-only and cannot be modified after declaration and assignment.

Using constants can simplify debugging and code editing.

Constants should be used instead of literals for better code readability and easier debugging.

Hard-coding literal values can lead to more work when changes are needed across multiple lines of code.

Using constants can reduce the effort needed to propagate changes across a codebase.

Constants are recommended for values that will not change throughout the lifecycle of a game.

Naming conventions for constants should visually distinguish them from variables.

Using uppercase for constants and lowercase for variables can help in quickly identifying them in the code.

The choice of naming convention should be comfortable for the programmer and visually distinct.

Transcripts

play00:01

so what exactly are constants in

play00:04

computer science a constant is a value

play00:06

that never changes so how exactly do you

play00:10

declare a constant in GD script

play00:12

constants are declared with the keyword

play00:15

Const spelled Co n st followed by a

play00:19

unique name an equal sign symbol and a

play00:23

value all of these are required you

play00:25

cannot assign an empty constant you

play00:29

cannot have a constant without an

play00:32

assigned value the only thing separating

play00:34

a variable from a constant is the

play00:36

keyword everything else is exactly the

play00:39

same just like variables keep in mind

play00:42

that each and every variable in constant

play00:45

you declare must have a unique name or

play00:49

an error will be thrown

play00:50

now just like variables you may also add

play00:53

a datatype explicitly by adding a colon

play00:57

science symbol followed by the data type

play00:59

in this case we have declared a constant

play01:02

called value never changes and assigned

play01:04

it the datatype integer which is

play01:07

assigned a literal integer 1 assigning a

play01:10

datatype to a constant is not necessary

play01:13

as the datatype is inferred to the

play01:15

constant with the assigned value

play01:18

basically a constant declaration format

play01:20

looks like the following you have the

play01:22

keyword Const followed by a unique name

play01:25

for the constant with the option to a

play01:29

data type but this must be followed by

play01:32

an equal sign symbol followed by a

play01:34

literal or data object everything in

play01:37

yellow is required while everything in

play01:40

white and the parentheses is optional

play01:41

one thing to note is that constants are

play01:44

read only after declaration therefore

play01:47

modifications are not permitted after

play01:49

Declaration and assignment let's take a

play01:52

look at a quick example we declared a

play01:54

constant value never changes and

play01:56

assigned it the literal integer 1

play01:58

somewhere in the code after the

play01:59

declaration if we try to assign a new

play02:03

value to our constant value never

play02:05

changes in this case we're trying to

play02:07

assign it the value 100 this will throw

play02:10

an error in GDD script there are pros

play02:13

when using constants

play02:15

one thing I noticed with beginners is

play02:17

the use of literal values beginners tend

play02:20

to use literal values instead of

play02:22

assigning literal values to constants I

play02:25

recommend using constants because you'll

play02:27

have an easier time debugging you'll

play02:29

save time when editing code and of

play02:31

course easier time reading code one

play02:34

thing I noticed with beginners is this

play02:36

question aren't constants the same as

play02:38

literals and yes you can use literals

play02:41

instead of constants to the compiler it

play02:43

doesn't make a difference how values are

play02:45

given however for the sake of readable

play02:47

code and easier debugging you should use

play02:50

constants let's take a look at some

play02:52

examples so to a beginner let's say that

play02:55

they have a speed for their game items

play02:58

and in this case they need to use an

play03:00

integer value of 5 what a beginner

play03:02

typically does is they just hard inject

play03:05

literal values in this case you can see

play03:07

we have three variables a player speed a

play03:10

laser speed and a boomerang speed all

play03:12

being assigned the literal integer 5 now

play03:15

let's say we need to change our main

play03:17

speed for the game to be 10 instead of 5

play03:20

now we've gone ahead and changed the

play03:22

speed to 10 in our case to change our

play03:26

main speed for game items we needed to

play03:28

manually find and edit three lines of

play03:31

code now this doesn't seem to be a lot

play03:33

of work for three lines of code however

play03:35

if we had more code this would take more

play03:38

time in this case we have 20 variables

play03:41

that rely on the speed value 10 as you

play03:46

can see we have 20 lines of code now

play03:48

imagine if we had to change 20 lines of

play03:51

code when we decided that the speed is

play03:53

too fast you're in trouble now because

play03:55

you need to change 20 lines of code that

play03:58

means you need to take the time to

play03:59

manually find in your script any code

play04:02

that uses your speed and you have to

play04:05

find them all and if you fail to find

play04:07

them all if you even forget about one

play04:10

line of code that uses the old value

play04:12

speed 10

play04:13

your game may perform as it's not

play04:16

intended to perform because you forgot

play04:18

one line of code let's go ahead and fix

play04:20

this instead of hard-coding the speed

play04:23

for the game as a literal value instead

play04:25

we will assign that value to a constant

play04:28

and

play04:28

case we have a constant speed which is

play04:30

equal to five five being the value for

play04:34

our speed in our game now that we have

play04:36

our constant called speed which holds

play04:39

our speed for the game in this case five

play04:41

we can go ahead and work on our game as

play04:44

time passes by maybe we have a thousand

play04:47

line of code that rely on our constant

play04:50

speed as you can see here we have

play04:52

changed one line of code we have changed

play04:54

the speed from five to ten and now this

play04:57

change in our value to our constant

play05:00

speed will propagate to the rest of our

play05:02

code as you can see here we have

play05:04

essentially edited a thousand line of

play05:07

code by just changing one line of code

play05:09

as you can see here this is the power of

play05:12

using constants instead of injecting

play05:14

literal values I recommend using

play05:16

constants when you want to declare a

play05:18

value that you know will never change in

play05:21

the life cycle of your game for example

play05:23

you may have a player and that player

play05:25

may have a height and a width instead of

play05:28

injecting the raw literal value all over

play05:31

your code assign that value to a

play05:34

constant and then assign that constant

play05:36

to your code in this case this is good

play05:39

practice because the player height and

play05:41

player width will most likely never

play05:43

change throughout the game lifecycle and

play05:45

if you do happen to want to change the

play05:47

value all you have to do is change one

play05:49

line of code and it propagates to the

play05:51

rest of your code one thing to keep in

play05:54

mind is the naming convention when using

play05:56

constants make sure that naming

play05:58

conventions for constants are visually

play06:00

different than variables this is

play06:02

recommended to help you as a programmer

play06:05

recognize what is a constant versus what

play06:08

is a variable when looking through your

play06:11

code let's take a look notice how all my

play06:14

constants start with an uppercase just

play06:17

so that I can visually know that when I

play06:19

read anything with an uppercase in its

play06:21

name that it is a constant and that I

play06:25

intend to never change its value now

play06:27

this is different than how I declare my

play06:30

variables as you notice I declare my

play06:32

variables starting in lowercase this is

play06:35

so when I see anything starting with

play06:37

lowercase I know that the value can

play06:40

change another example

play06:42

of a constant being visually different

play06:44

than a variable as some people tend to

play06:47

use uppercase to name their constants

play06:49

while using whatever they want with

play06:52

their variables again you don't have to

play06:54

use this

play06:54

they are visually different use whatever

play06:56

you are comfortable with again just make

play06:59

sure the to help you as a programmer

play07:01

that the constant is visually different

play07:03

than the variable

Rate This

5.0 / 5 (0 votes)

Related Tags
GDScriptConstantsGame DevelopmentCode EfficiencyDebuggingLiteral ValuesNaming ConventionsCode ReadabilityVariable DeclarationBest Practices