Scope Levels | Godot GDScript Tutorial | Ep 14

Godot Tutorials
19 Apr 202007:38

Summary

TLDRThis episode of the GD Script Fundamental tutorial series delves into the concept of scopes in programming. Scopes are regions where named bindings, associating names with entities like variables, are valid. The tutorial explains four key scopes: global, class, function, and code block scopes, each with varying levels of accessibility. The presenter illustrates the scope hierarchy and how variables can propagate down but not up through these levels. Practical examples and code snippets clarify how variable accessibility works within each scope, emphasizing the intentional design to limit access for better program organization and functionality.

Takeaways

  • 📚 A scope is a region in a computer program where a name binding is valid, and it associates a name with an entity like a variable.
  • 🌐 Scopes can range from small loops to an entire application, with varying levels of scope that are important for a programmer to understand.
  • 🔍 Four fundamental levels of scopes are global, class, function, and code block scopes, each with specific accessibility rules.
  • 🌟 Global scope variables, values, or functions are accessible throughout the entire program, such as the 'Node' class in GDScript.
  • 📁 Class scope variables, values, or functions are accessible only within the file they are declared, like the '.gd' file in GDScript.
  • 🔑 Function scope variables are only visible within the function they are declared in, including parameters and local variable declarations.
  • 🔄 Code block scope variables are only visible within the code blocks like if statements, while loops, and other control structures.
  • ⬆️ Scopes have a hierarchy, with global at the top and code block at the bottom, allowing variables to propagate down but not up the hierarchy.
  • 🚫 Variables declared in lower scopes are not accessible in higher scopes, enforcing encapsulation and preventing unintended access.
  • 🔍 Understanding scope hierarchy helps in determining the accessibility of variables, functions, and classes within different parts of a program.
  • 💡 Declaring variables higher in the scope hierarchy makes them more accessible, while those declared lower are intentionally less accessible to limit their use to specific contexts.
  • 🛠️ Practical examples in the script illustrate how to work with different scopes and the errors that occur when trying to access variables outside their declared scope.

Q & A

  • What is a scope in the context of programming?

    -A scope is a region of a computer program where a named binding is valid. It refers to the visibility and accessibility of variables, functions, and classes within different parts of the code.

  • What are the four levels of scopes mentioned in the script?

    -The four levels of scopes mentioned are global scope, class scope, function scope, and code block scope.

  • Can you explain the global scope and provide an example?

    -The global scope refers to variables, values, or functions that can be used anywhere in the entire program. An example given in the script is the 'Node' class, which is globally accessible.

  • What is the class scope and how does it differ from the global scope?

    -The class scope consists of variables, values, or functions that can only be accessed from within the file, such as a .GD file in GDScript. It differs from the global scope in that it is limited to the file's context, not the entire program.

  • What is a function scope and how does it relate to variable visibility?

    -A function scope refers to variables, values, or parameters that are only visible inside the function where they are declared. This means they cannot be accessed outside of the function.

  • Can you describe the code block scope and its significance?

    -The code block scope pertains to variables that are only visible inside code blocks, such as those declared in if statements, while loops, match statements, and for loops. It signifies the limited visibility of these variables to the specific block they are declared in.

  • How does the scope hierarchy work in programming?

    -Scope hierarchy in programming defines the order and accessibility of different scopes. At the top is the global scope, followed by file module, class, function, and code block scopes at the bottom. Variables can propagate down the hierarchy but not up.

  • Why is it important to understand scope hierarchy when programming?

    -Understanding scope hierarchy is crucial because it dictates where and how variables, functions, and classes can be accessed within a program. This helps in avoiding naming conflicts and ensures proper data encapsulation and security.

  • What happens if you declare a variable in a scope that is lower in the hierarchy, like a code block?

    -Declaring a variable in a lower scope, such as a code block, means that this variable is not accessible to scopes above it. It is confined to the specific block it is declared in and cannot propagate up the scope hierarchy.

  • Can you provide an example of how a class member variable is used in GDScript?

    -In the script, a class member variable is declared within a class. This variable can be accessed within the class's functions and even within loops, demonstrating its accessibility based on the class scope.

  • What error might occur if you try to access a function-scoped variable outside of its function in GDScript?

    -Attempting to access a function-scoped variable outside of its function will result in an error. The GDScript compiler will throw an 'unknown variable name' error because the function scope does not have access to variables declared in scopes above it.

Outlines

00:00

📚 Understanding Scopes in Programming

This paragraph introduces the concept of scopes in programming, explaining that a scope is a region where a name binding is valid. It discusses four primary levels of scopes: global, class, function, and code block scopes, each with different accessibility rules. The global scope allows access to variables, functions, or classes throughout the entire program, while class scope is limited to the file in which it's declared. Function scope is restricted to the function itself, including parameters and local variables, and code block scope is confined to the block, such as loops or conditionals. The hierarchy of scopes is also explained, with global at the top and code block at the bottom, illustrating how variables declared at higher levels can be accessed at lower levels but not vice versa. An example is given to demonstrate how variable access works across different scopes.

05:01

🔍 Deep Dive into Scope Hierarchy and Variable Accessibility

This paragraph delves deeper into the scope hierarchy and how it affects variable accessibility. It uses an example to show how variables declared at different levels of the hierarchy can be accessed. The global scope can access all variables, but variables declared in the file module scope cannot be accessed from the global scope. The class scope has access to both global and file module variables, but not to those declared within functions or code blocks. Function scope can access class and global variables but not those within code blocks. The paragraph emphasizes the intentional design of scope levels to limit the accessibility of variables to maintain logical organization and prevent unintended interactions between different parts of the code. It concludes with an invitation to download a GitHub file for further exploration of scopes and variable access.

Mindmap

Keywords

💡Scope

Scope refers to the region of a computer program where a name binding is valid. It is a fundamental concept in programming that defines the visibility and accessibility of variables, functions, and classes. In the video, scope is the central theme, with different levels such as global, class, function, and code block scopes being discussed to explain how variables can be accessed at various points in the code.

💡Named Binding

A named binding is the association of a name to an entity like a variable. It is a way to label and reference data within a program. The video emphasizes the importance of named bindings in relation to scopes, as they determine where and how data can be accessed throughout the program.

💡Global Scope

The global scope is the highest level of scope in a program, where variables, values, or functions declared are accessible from anywhere in the code. The video uses the 'Node' class as an example of a globally accessible entity, illustrating how global scope variables can be used across different parts of a program.

💡Class Scope

Class scope pertains to variables, values, or functions that are accessible only within the class they are defined in. The video explains that class scope variables are not accessible outside of their class, unlike global scope variables, which can be accessed anywhere.

💡Function Scope

Function scope is the level of scope where variables are only visible inside the function they are declared in. This includes parameters and local variable declarations. The video script provides an example of a function scope variable 'function scope' that can be used within a specific function but not outside of it.

💡Code Block Scope

Code block scope refers to variables that are only visible within the code blocks such as if statements, while loops, etc. The video clarifies that these variables do not propagate up to higher scopes and are only accessible within the block they are declared in.

💡Scope Hierarchy

Scope hierarchy is the ordering of scopes from the most global to the most local. The video script describes the hierarchy starting with global, followed by file module, class, function, and finally code block scopes. It explains how variables declared at higher levels in the hierarchy can be accessed at lower levels, but not vice versa.

💡Variable Propagation

Variable propagation in the context of scopes refers to the accessibility of a variable from one scope to another. The video script explains that variables declared at higher levels in the scope hierarchy can be accessed in lower levels, but variables declared at lower levels cannot be accessed in higher levels.

💡Class Variable

A class variable is a variable that is declared within a class and is accessible to all instances of the class. The video script uses a class variable as an example to demonstrate how variables declared at the class level can be accessed within functions and loops.

💡Member Variable

A member variable is a variable that is part of a class and is accessible to methods within that class. The video script mentions a member variable declared in a class, which can be called in functions and for loops, indicating its accessibility within the class scope.

💡Error Handling

Error handling in the context of scopes refers to the process of managing errors that occur when trying to access variables that are out of scope. The video script illustrates this with examples where attempting to access a function scope variable outside its function or a code block scope variable outside its block results in an error.

Highlights

Scopes are regions in a computer program where named bindings are valid.

Scopes range from small loops to entire applications.

Understanding four levels of scopes is essential: global, class, function, and code block scopes.

Global scope variables are accessible throughout the entire program.

Class scope variables are accessible only within the file they are declared.

Function scope variables are visible only within the function they are declared.

Code block scope variables are limited to the code block they are declared in.

Scopes have a hierarchy, with global at the top and code block at the bottom.

Variables can propagate down the scope hierarchy but not up.

Variable declarations at lower scopes are not accessible at higher scopes.

An example demonstrates how variable access works across different scopes.

Declaring a variable in a higher scope makes it more accessible.

Variables declared in a lower scope are less accessible.

Intention behind variable declaration is to limit or expand its accessibility based on the scope.

A class member variable example shows how it can be accessed in functions and loops.

Function scope example illustrates variable access limitations within a function.

Code block scope example shows how variables are limited to the block they are declared in.

Scope hierarchy rules prevent access to variables declared in lower scopes from higher scopes.

The tutorial concludes with a summary of scope hierarchy and its practical implications.

Transcripts

play00:01

hello and welcome to another episode in

play00:04

the GD script fundamental tutorial

play00:07

series in this episode we will be

play00:08

discussing scopes now a scope is a

play00:11

region of a computer program where named

play00:13

binding is valid name bindings are an

play00:16

association of a name to an entity such

play00:18

as a variable

play00:19

now scopes can vary in range from small

play00:21

loops to as much as an entire

play00:23

application there are many different

play00:25

levels of scopes however as a programmer

play00:27

you'll benefit from understanding at

play00:29

least these four levels of scopes in the

play00:32

list they are global scopes class scopes

play00:35

function scopes and code block scopes

play00:37

now a global scope is a class variable

play00:39

value or function that can be used

play00:42

anywhere in the entire program an

play00:44

example would be the node class which is

play00:46

globally accessible now your class Co is

play00:49

basically variables values or functions

play00:51

that can only be accessed from within

play00:53

the file in this case your file would be

play00:56

the dot GD file since you're programming

play00:58

in Gd script now the function scope is

play01:01

basically a variable value that is only

play01:03

visible inside the function that it is

play01:06

declared in these would include

play01:07

parameters and variable declarations

play01:09

lastly you have your code block scope

play01:11

and that is a variable value that is

play01:13

only visible inside code blocks these

play01:15

would be variables declared in your if

play01:17

statements and your while loops and of

play01:20

course the variables that come

play01:21

prepackaged for example in your match

play01:23

statements and your for loops now let's

play01:25

take a look at scope hierarchy

play01:27

now scopes do you have hierarchies as

play01:30

you notice at the top the global scope

play01:32

is the highest level in the scope

play01:34

hierarchy as we move down file module

play01:37

class go is second highest our functions

play01:40

are beneath class scopes and our code

play01:42

block scope is at the bottom now notice

play01:44

how we have arrows pointing from each

play01:46

scope what this means is that our

play01:48

variable can propagate down to the

play01:51

scopes beneath them however variables

play01:54

declared at scopes at the bottom of the

play01:57

hierarchy do not propagate up this

play02:00

basically means that when you declare a

play02:02

variable from a hierarchy beneath the

play02:04

current level you are not able to access

play02:06

those variables let's go ahead and take

play02:09

a look at an example we'll start with

play02:10

the global scope the global scope

play02:12

declares the variable a and

play02:15

variable a can propagate down the

play02:18

variable a can be used in your classes

play02:20

your variable a can be used in the

play02:22

functions in your classes and the

play02:24

variable it can be used in your code

play02:26

block such as a for or while loop

play02:27

however let's take a look at the file

play02:30

module the variable B in our file module

play02:33

will not propagate up there's no arrow

play02:35

pointing up this means that our global

play02:38

scope does not have access to variable B

play02:41

or as a matter of fact anything declared

play02:44

in our file module and of course

play02:46

everything beneath it now looking at the

play02:48

file module class we've created the

play02:49

variable beam and in our file module we

play02:52

have access to everything in the global

play02:54

scope including the variable a so while

play02:57

on file module we have access to

play02:58

variable a and B we do not have access

play03:01

to variables C and D as we move down

play03:04

you'll notice our access to variables

play03:06

expand or grow our function scope has a

play03:10

variable C and we have access to

play03:12

variables B and a however we do not have

play03:16

access to variable deme because the code

play03:18

block in this case perhaps a while for

play03:21

loop although it declares a variable D

play03:23

it does not propagate that variable back

play03:25

up the scope hierarchy so our function

play03:28

doesn't see anything inside the code

play03:30

block now lastly we have our code block

play03:32

and it's declared a variable D and as

play03:35

you'll notice we have access to

play03:36

variables c BN a however everything in

play03:40

the scope hierarchy that is above our

play03:42

code block does not have access to

play03:44

variable D so at the top were basically

play03:47

blind to all variable declarations at

play03:49

scope levels beneath the global and at

play03:52

the bottom we have access to all

play03:54

variables declared in scope levels above

play03:57

our current hierarchy or in this case

play04:00

the bottom hierarchy it's a little

play04:02

confusing at first one way to make it

play04:04

easier when working with the scope level

play04:07

hierarchy is that the higher in the

play04:09

scope hierarchy chain you declare your

play04:11

variable the more or readily accessible

play04:14

that variable / function / classes

play04:18

whereas variables towards the bottom of

play04:20

the scope level hierarchy are less

play04:23

accessible this is intentional in

play04:25

programming as when you declare a

play04:27

variable

play04:28

for example a while loop thus the code

play04:31

block scope most likely your intention

play04:33

is to use that variable just for the

play04:35

while loop same thing with your function

play04:37

if you declare a variable in your

play04:38

function it's most likely that your

play04:40

intention is to use that variable only

play04:41

in the function that you've declared it

play04:44

in same thing with the class and lastly

play04:46

if you're putting something in the

play04:48

global scope it's because you want that

play04:50

variable function or class to be

play04:52

accessible everywhere in your program

play04:55

now let's go ahead and look at some code

play04:57

now here we have a class we've declared

play05:00

a member variable and this member

play05:04

variable would be in the classical

play05:06

hierarchy level what that basically

play05:08

means is that this class and variable

play05:11

name or rather this member variable name

play05:13

declared in our class level can be

play05:15

called in our functions and even our for

play05:19

loop you can see up here our function

play05:22

calls the class scope and it close it

play05:26

down here let's take a look at the

play05:28

function scope now as you can see here

play05:31

in the function scope and I've called

play05:33

this function scope I've declared a

play05:35

variable called function scope and it's

play05:37

got a string now this function scope can

play05:40

be called

play05:41

inside our function of course and we've

play05:43

called it here in the print statement

play05:44

and we put it down here in the for loop

play05:48

as you can see our variable can be

play05:50

declared here and in a hierarchy beneath

play05:52

us and of course our class scope

play05:54

variable can be called in our functions

play05:56

and for loop as well however if I were

play05:58

to take this function scope and say type

play06:02

it outside notice how it will or rather

play06:07

if you were to type this in the Gd

play06:09

script compiler it will throw an error

play06:11

now the reason why this throws an error

play06:13

is because our function reading only has

play06:16

access to variables declared in our

play06:18

current function plus variables declared

play06:20

in scopes higher than our function in

play06:23

this case our class variable gonna

play06:25

delete that moving on to the for loop

play06:30

our for loop has a variable I and our I

play06:33

is accessible in our code block our code

play06:36

block scope now we also have access

play06:38

again to the variables declared in our

play06:40

current function

play06:41

and our variables declared at the class

play06:44

level or our member variables however

play06:47

the variable I cannot be declared in

play06:50

scope levels higher than our current

play06:52

scope level in this case if I were to

play06:54

print a out or even just use I this

play06:57

would throw an error it would give you

play06:59

an unknown variable name error so again

play07:03

to reiterate scopes at the top of the

play07:05

hierarchy do not have access to

play07:08

variables declared in scopes beneath

play07:10

them and scopes at the bottom of the

play07:12

hierarchy have access to variables

play07:14

declared in scopes above them well

play07:17

that's all I have for this episode don't

play07:20

forget to download the github file to

play07:22

play around with scopes and how

play07:24

accessing variables work thank you for

play07:27

watching this episode I hope to see you

play07:29

in the next

Rate This

5.0 / 5 (0 votes)

Related Tags
ScopesGD ScriptProgrammingVariable AccessCode HierarchyScope LevelsGlobal ScopeClass ScopeFunction ScopeCode BlockTutorial