Class Objects & Constructors | Godot GDScript Tutorial | Ep 16

Godot Tutorials
21 Apr 202011:50

Summary

TLDRThis tutorial delves into the concept of objects and constructors in GDScript, explaining how objects are instances of classes and the process of instantiation. It clarifies the role of instance variables, unique to each object, and demonstrates how to create and use class objects with examples. The video also covers the use of constructors to initialize objects with unique properties, showcasing the flexibility and power of object-oriented programming in GDScript.

Takeaways

  • 📚 An object in GDScript is an instance of a class, also known as a class instance, instance object, or class object.
  • 🔨 Instantiation, or construction, is the process of creating an instance of a predefined object with a unique name for use in a program.
  • 🔑 Instance variables are member variables that each object has a separate copy of, ensuring changes in one do not affect others.
  • 🧱 The compiler in GDScript will attempt to typecast values to match the declared data type of an instance variable if a different type is passed.
  • 🔄 When creating multiple objects from a class, each object has its own isolated copy of the member variables.
  • 🛠️ Instance objects are useful when you need to work with multiple items or copies of a class, like having multiple enemies on the screen in a game.
  • 📝 To create an object in GDScript, you can use the registered class name with the 'new' keyword or load the class via file paths and then use 'new'.
  • 🎯 The 'initialize' method, or '__init__' for short, is used in GDScript to set up instance objects with specific properties during instantiation.
  • 🛑 If parameters are declared in the '__init__' method, they must be specified when creating an instance object to avoid errors; default values can be set to prevent this.
  • 📌 Using the constructor method allows for variation in object properties, making each instance unique, by passing values during the instantiation process.
  • 📑 The script provided in the tutorial demonstrates creating class objects with unique names and properties, showcasing the power of instance objects in GDScript.

Q & A

  • What is an object in the context of this script?

    -An object is an instance of a class, also referred to as a class instance, instance object, or class object. It is the realization of a predefined object through a process known as instantiation or construction.

  • What is the term used for the process of creating an instance of a class?

    -The term used for creating an instance of a class is 'instantiation', also known as 'construction'. It involves declaring an instance of an object with a unique name for use in a program.

  • What is an instance variable and how does it differ from other variables?

    -An instance variable is a member variable that has a separate copy for each instantiated object of the class. Changes to the data in one instance variable will not affect the same variable in another instance object, ensuring data isolation.

  • How does the compiler handle a mismatch between the declared data type of an instance variable and the actual value passed at runtime?

    -If an instance variable is declared with a specific data type and a different data type is passed at runtime, the compiler will attempt to typecast the passed value to the specified data type, for example, wrapping an integer in double quotes to convert it into a string.

  • What is the purpose of using non-static variables in the context of instance variables?

    -Non-static variables are important when discussing instance variables because they ensure that each instance of a class has its own copy of the variable, supporting code isolation and preventing unintended data sharing between instances.

  • When might you choose to use instance objects in your program?

    -Instance objects are useful when you need to work with multiple items or copies of a class, such as having multiple enemies on the screen in a game, allowing for unique properties and behaviors for each object.

  • How do you create an instance object using a registered class name?

    -To create an instance object using a registered class name, you use the registered name followed by the 'new' keyword or 'new' method, which instantiates the class.

  • What is the difference between using a registered class name and file paths to create an instance object?

    -Using a registered class name to create an instance object requires only one line of code with the 'new' method. In contrast, using file paths involves loading the class into the current class with a 'load' method and then calling the 'new' method, which takes two lines of code.

  • What is the purpose of the 'initialize' method or '__init__' in a class?

    -The 'initialize' method, or '__init__', is a constructor method in a class that is called when the class is instantiated. It allows for setting initial values for member variables and is particularly useful for creating unique instances with different properties.

  • How can you avoid errors when using the 'initialize' method with parameters but not passing any values during instantiation?

    -To avoid errors when using the 'initialize' method with parameters, you can assign default values to the parameters or declare the parameters with inferred data types using the colon and equal sign syntax, ensuring that instantiation can occur without passing values.

  • What happens if you assign a value of a different datatype to a member variable that is declared to only accept string values?

    -If you assign a value of a different datatype to a member variable declared to only accept strings, the system will attempt to cast the value as a string, effectively converting the integer to a string representation when used in string-based operations.

Outlines

00:00

📚 Introduction to Objects and Constructors in GDScript

This paragraph introduces the concept of objects in GDScript as instances of a class, also known as class instances, instance objects, or class objects. The process of creating an object is termed 'instantiation' or 'construction'. Instance variables are explained as separate copies within each object, meaning changes to one instance do not affect others. The script also discusses typecasting by the compiler when a mismatch occurs between declared and assigned data types. An example is given where a non-static integer variable is assigned a string value, which the compiler converts to a string. The importance of understanding static versus non-static variables in GDScript is highlighted, with a focus on non-static variables for this tutorial.

05:01

🔍 Utilizing Instance Objects and Constructors for Uniqueness

This section delves into the practical use of instance objects, particularly when multiple items or copies of a class are needed, such as multiple enemies on a screen. It explains how to create a class and register it with the editor, emphasizing the use of class name keywords for cleaner code. The paragraph outlines the process of instantiating objects using the 'new' keyword or file paths, with a preference for the former due to its conciseness. The power of instance objects lies in the ability to vary object properties, which is achieved through the use of a constructor method, specifically the '_init' function. The importance of handling parameters within the constructor to avoid errors is discussed, along with the use of default values and data type inference. An example is provided to illustrate the initialization process and the ability to create objects with unique properties.

10:03

🛠️ Practical Implementation of Constructors and Instance Objects

The final paragraph focuses on the practical application of constructors and instance objects in GDScript. It provides a step-by-step guide on creating a class with a constructor method that accepts parameters and assigns them to member variables. The paragraph also explains how to create instance objects with or without passing values to the constructor, resulting in either default or custom values for the member variables. An example script is given, demonstrating the creation of multiple enemy objects with unique names, and how to access and modify member variables and call functions on these objects. The importance of proper data type handling is reiterated, with an example of how the system attempts to cast an integer to a string when assigned to a string-typed member variable. The tutorial concludes with an invitation to download the provided scripts and experiment with instance objects, aiming to enhance the understanding of class objects, constructor methods, and creating unique instances.

Mindmap

Keywords

💡Object

In the context of the video, an 'Object' refers to an instance of a class. It is a fundamental concept in object-oriented programming, representing a specific occurrence of a class with its own state and behavior. The video script discusses objects in relation to classes, emphasizing that each object can have unique values for its instance variables, as seen when 'object one' and 'object two' are instantiated from the 'player' class with different values for variable 'a'.

💡Instantiation

Instantiation, also known as construction in the script, is the process of creating an instance of a class. It is a key action in object-oriented programming that allows developers to create individual objects from a blueprint (class). The script explains that instantiation gives life to a predefined object, allowing it to be used in a program, with the example of creating 'object one' and 'object two' from the 'player' class.

💡Instance Variable

An 'Instance Variable' is a member variable within a class, where each instantiated object has its own separate copy. The video script clarifies that changes to an instance variable in one object do not affect the same variable in another object, ensuring data isolation. This is demonstrated when the value of 'a' in 'object one' is changed to a string, leaving 'object two's 'a' value unchanged.

💡Typecasting

Typecasting in the script refers to the process where the compiler automatically converts one data type to another. For instance, if an integer is passed to a variable declared as a string, the compiler wraps the integer in double quotes, turning it into a string value. This is shown in the script when an integer value is mistakenly passed to a string-typed instance variable, and the compiler converts it without error.

💡Non-Static Variables

Non-static variables are variables that belong to individual instances of a class, as opposed to static variables, which are shared among all instances. The script emphasizes the importance of non-static variables for creating unique instances with isolated member variables. The 'player' class's variable 'a' is an example of a non-static variable, with 'object one' and 'object two' each having their own copy of 'a'.

💡Constructor

A 'Constructor' in the script is a special method used to initialize the state of an object after creation. It is introduced with the term 'initialize' or '_init' in GDScript. The video explains how constructors can be used to pass values to member variables when an object is instantiated, such as setting a default name for an 'enemy' class object or allowing a custom name to be provided.

💡Inheritance

Although not the main focus of the video, 'Inheritance' is mentioned in the context of not needing an 'extends' keyword for the 'enemy' class since it does not inherit from another class. Inheritance is a fundamental concept in object-oriented programming where a class can inherit properties and methods from another class. The script notes that the 'enemy' class does not extend another class, indicating it is a base class.

💡Class Name Keyword

The 'Class Name Keyword' in the script refers to the practice of using the class name as a keyword for creating new instances of that class. This approach is presented as a cleaner way of writing code, as it allows for a single line of code to instantiate an object, versus the two lines required when using file paths. The script illustrates this with the instantiation of 'enemy' class objects.

💡Default Value

A 'Default Value' is a pre-assigned value to a parameter in a function or method, including constructors. The script explains that providing a default value in the constructor method, such as 'enemy' for the 'name' variable, prevents errors when an instance is created without specifying a parameter. It also demonstrates type inference using default values.

💡Member Function

A 'Member Function' is a function that is associated with an object and can access its member variables. The script discusses member functions in the context of the 'print_a' function in the 'enemy' class, which prints the value of the 'a' variable. The video shows how to call member functions using dot notation on an instance object, such as 'enemy_one.print_a()'.

💡Data Type Inference

Data Type Inference is the process by which the compiler deduces the data type of a variable based on the value assigned to it. In the script, this is shown when a default value is assigned in the constructor, allowing the compiler to infer the data type of the 'name' variable as a string, based on the literal value 'enemy'.

Highlights

Introduction to objects and constructors in GD Script.

An object is an instance of a class, also known as class instances, instance objects, or class objects.

Instantiation is the process of creating an instance of a predefined object.

Instance variables are unique to each object instance, ensuring data changes in one instance do not affect others.

Compiler typecasting to ensure variables maintain their declared data types.

Example of instance variables in a 'player' class, demonstrating how they are isolated in each object.

Use of instance objects when working with multiple items or copies of a class, such as multiple enemies on screen.

Creating a class and registering its name in the editor for cleaner code.

Instantiating objects using the 'new' keyword or through file paths.

Using class constructors to initialize object properties and make each object unique.

The 'initialize' method is called when a class is instantiated, allowing for parameterized object creation.

Assigning default values to constructor parameters to avoid errors when no values are passed.

Using the 'new' keyword to pass values to the constructor method during object instantiation.

Example code demonstrating the use of constructors to set member variables and print custom names.

Accessing member variables and functions from an object using dot notation.

The compiler's ability to cast different data types to match member variable requirements.

Conclusion summarizing the tutorial on class objects, constructors, and making objects unique.

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:09

talking about objects and constructors

play00:12

let's start with objects what exactly is

play00:14

an object well an object is an instance

play00:16

of a class they can be referred to as

play00:19

class instances instance objects or

play00:22

class objects now we need to introduce a

play00:24

new word which is called instantiation

play00:26

now instantiation also known as

play00:29

construction is the realization of a

play00:32

predefined object an instance of an

play00:35

object may be declared given a unique

play00:37

name so that it may be used in a program

play00:40

now we need to also understand what

play00:42

exactly an instance variable is now an

play00:44

instance variable is a member variable

play00:47

in which each instantiated object of the

play00:49

class has a separate copy or instance

play00:52

this means that data changes to one

play00:54

variable in one instance object will not

play00:57

change another variable in another

play01:00

instance object now if you declare a

play01:02

member variable with a fixed data type

play01:05

the compiler will do its best to

play01:07

typecast to a specified datatype if you

play01:10

were to accidentally try to pass in a

play01:12

different data type so let's say your

play01:13

instance variable you declared it with a

play01:16

data type string

play01:17

but during runtime you accidently pass

play01:20

it an integer value what the compiler

play01:22

will do is change your integer value

play01:25

into a string value basically wrapping

play01:28

your integer value inside double

play01:30

quotations let's go ahead and take a

play01:32

look at an example of an instance

play01:34

variable keep in mind for this example

play01:36

we'll be looking at non-static variables

play01:38

in our player class non-static forces

play01:41

static variables are out of topic for

play01:44

this video and GT script as a language

play01:46

however for those of you that know

play01:48

programming a non-static variable is

play01:51

pretty important when we're talking

play01:53

about instance variables and code

play01:56

isolation of our member variables

play01:58

knowing the difference between static

play02:00

versa non-static is important however in

play02:02

GT script we always deal with non-static

play02:05

variables so our non-static variable a

play02:07

from our player class has the initial

play02:11

value of the literal integer 100 now

play02:14

we're creating two

play02:15

variables object one in object two and

play02:18

we're instantiating the player class

play02:20

into these objects now object one has

play02:23

the value a and of course it has a

play02:25

hundred because we're basically copying

play02:27

the class into our variable and the same

play02:30

thing for object two we are taking the

play02:32

variable name and value assigned to it

play02:35

from player class into our object two

play02:37

now when we change the value of object 1

play02:41

in this case assigned a string value to

play02:43

our a notice how we're not changing the

play02:45

value in object to this is because each

play02:48

object has a separate copy of the

play02:50

variables they receive from the main

play02:53

class or the instantiated class in this

play02:55

case our player class so just keep in

play02:57

mind when you create object variables or

play02:59

rather if you instantiate class objects

play03:01

all member variables are isolated to

play03:04

their own object now moving on when

play03:07

exactly is it a good time to use

play03:09

instance objects well a good time to

play03:11

know you need to use instance objects is

play03:13

when you want to work with multiple

play03:15

items or copies of the class

play03:17

let's take having multiple enemies on

play03:20

the screen as an example now first

play03:22

before you can actually instantiate an

play03:24

object you need to create a class in

play03:26

this case we've created our enemy class

play03:28

and we are registering a name into the

play03:32

editor I personally find using class

play03:34

name keywords' as a cleaner way of

play03:37

writing code now again we have our class

play03:40

name and we are registering the name

play03:43

enemy to the editor we have a simple

play03:45

variable a and we have a function called

play03:47

print a that basically just prints the

play03:50

variable a or 100 to the console one

play03:53

thing to note is that there's no extends

play03:54

keyword that's because we don't need to

play03:57

inherit anything if we don't plan on

play03:59

adding this class to the scene tree now

play04:02

moving on if you want to create an

play04:04

object first you use the registered name

play04:06

in this case enemy followed by the new

play04:09

keyword or the new method now you can

play04:12

also create a class object with file

play04:14

paths however it's going to take you two

play04:16

lines of code to do that let me show you

play04:17

what first you need to load your class

play04:20

into the current class in this case we

play04:22

need to load the enemy class into our

play04:24

scene script class and we do that we

play04:27

load the class by simply using the low

play04:29

keyword followed by an absolute or

play04:31

relative path inside notice the double

play04:34

quotations it's a string value you have

play04:36

to pass inside your load method the

play04:39

global load method now after you've

play04:41

loaded your class and assigned it to a

play04:43

variable in this case load class what

play04:45

you then do is you call that variable

play04:47

and then you use the new method

play04:49

basically this is no different than what

play04:52

we have earlier except now we have to

play04:55

use two lines of code instead of one now

play04:58

the class name when we register our name

play05:01

to the editor behind the scenes we are

play05:04

essentially loading it the loading is

play05:07

done for you and that's why all you need

play05:09

to do is declare the new method however

play05:12

the choice is yours

play05:13

I personally like to keep my code clean

play05:15

so I like to use the class name keyword

play05:18

so I only have to write one line of code

play05:20

instead of two but the choice is up to

play05:22

you moving on if you want to use a class

play05:25

object simply use the variable name of

play05:29

your instance object in this case the

play05:31

variable we've we've created a copy of

play05:33

is the object variable so use the object

play05:36

variable followed by the function name

play05:39

in your instance object or you can even

play05:42

use member variables just remember to

play05:44

omit the parentheses moving on it's

play05:47

pretty boring to have multiple objects

play05:49

with the same exact values now the power

play05:52

of instance objects is the variation you

play05:55

can have in your object properties to

play05:58

make objects unique from other objects

play06:00

simply use the built-in class

play06:02

constructor to do that simply use the

play06:05

initialize method or an issue for short

play06:08

when you use this method inside your

play06:10

class that class constructor method is

play06:13

called when the class is instantiated

play06:15

let's go ahead and take a look at an

play06:17

example what we have is a function in

play06:19

ish and we take in a parameter now you

play06:21

don't have to take in a parameter but at

play06:23

that point why bother use the initialize

play06:25

function one thing to keep in mind is

play06:27

that if you declare a parameter inside

play06:29

your initialized method but when you

play06:31

decide to create an instance object of

play06:34

that class without a parameter you will

play06:36

throw an error so keep that in mind if

play06:39

you use an initializer and you declare

play06:41

parameters

play06:42

when you create an instance object you

play06:44

need to specify the parameters now to

play06:47

avoid this error we simply assign a

play06:50

default value to assign a default value

play06:52

to a parameter simply just use the equal

play06:55

sign followed by the literal value this

play06:58

will avoid an error if you forget to

play06:59

pass a value you can also infer datatype

play07:02

by simply using the colon symbol

play07:04

followed by the equal sign and not only

play07:06

will you've assigned a default value to

play07:08

Perimeter but you would have also

play07:09

declared a data type which is inferred

play07:12

by the literal value you're passing it

play07:14

to if you decide to use the constructor

play07:17

method in your class to pass a value to

play07:21

your constructor method all you need to

play07:23

simply do is use the new keyword and

play07:25

then just put your value inside the

play07:28

parentheses and if you have multiple

play07:30

parameters simply separate each value

play07:33

with a comma symbol to do this with file

play07:36

paths you simply wait let me add the

play07:39

value you simply pass the value into

play07:41

your new method it's the same thing

play07:43

again one line of code if you register

play07:47

your class name to the editor and two

play07:50

lines of code if you decide to use file

play07:52

paths to create your instance object now

play07:54

let's go ahead and look at some code so

play07:56

I have here a class name and I've

play07:59

declared a member variable with the data

play08:01

type string so our variable only accepts

play08:04

data types of string values now we have

play08:08

our constructor method and we we can

play08:10

pass a value and I've also defaulted the

play08:13

value to the string literal value called

play08:16

enemy most of the time when you take in

play08:18

a value in your class constructor you

play08:21

basically want to pass that value and

play08:22

assign it to a member variable so we

play08:24

take that value and we assign it to our

play08:26

member variable name basically you as a

play08:29

programmer if you want to use this

play08:31

method you have a choice you can choose

play08:33

to create an instance object with no

play08:35

values passed in the constructor and our

play08:38

name simply defaults to enemy or you can

play08:41

create an instance object and pass in a

play08:43

new name to car enemy now what we have

play08:46

is a function and it just simply prints

play08:49

to the console my name is followed by

play08:51

the name you pass so it will either be

play08:53

enemy by default or a custom name

play08:56

you choose by itself this doesn't do

play08:58

anything as you notice here we don't

play09:00

have an extend because we're not going

play09:02

to assign this to the scene tree in real

play09:05

life production you most likely will but

play09:07

in this example we aren't now let's go

play09:10

ahead and take a look at our main script

play09:11

which will be used in the same tree

play09:13

notice the extends note key word as you

play09:16

can see here we can create multiple

play09:18

instance objects we have two variables

play09:21

enemy one enemy two and we're going to

play09:24

create an instance object of our enemy

play09:26

class in this case the first line will

play09:29

have the name default and in the second

play09:31

line the name is jean grey now when we

play09:34

press the play button notice how enemy

play09:37

one's name

play09:38

notice how the member variable can be

play09:40

changed if we call it directly from the

play09:42

object in this case we can access member

play09:46

variables from our object or we can

play09:49

access functions from our object simply

play09:51

use the variable name followed by dot

play09:53

notation followed by either the member

play09:55

variable name or the function name now

play09:57

in this case we have print enemy Oh No

play10:03

let me change that in this case we're

play10:05

printing to the screen no name now it's

play10:08

going to print enemy if we don't change

play10:10

this value here this is the example I

play10:13

want it to show

play10:14

now we're gonna print enemy because we

play10:16

didn't change the member variable after

play10:18

declaration if you initialize a value

play10:21

that's the value it is a sign and then

play10:23

after initialization you can go ahead

play10:25

and feel free to change the initialized

play10:28

value for your instance variable now in

play10:30

the second line we have a again we're

play10:32

doing a simple function but in this case

play10:34

from enemy to object and that prints out

play10:37

Jean Grae now this is an interesting

play10:39

thing if you were to assign or

play10:42

accidentally assign a value of a

play10:44

different datatype keep in mind our

play10:46

member variable name can only accept

play10:49

datatype strings so if we try to assign

play10:52

the value 100 to the value name we're

play10:55

not going to throw an error what's going

play10:57

to happen is we're going to or rather

play10:58

the system is going to try it's best to

play11:00

cast this as a string in this case when

play11:03

we call the simple function out which

play11:06

prints to the console what we're going

play11:08

to print is

play11:09

100 and I put double quotations because

play11:12

what what the compiler ends up doing is

play11:15

it takes your integer value and just

play11:18

typecast as a string well that's all I

play11:21

have for you in this episode I'm gonna

play11:23

go ahead and upload this script or

play11:25

rather both scripts to get up so feel

play11:28

free to download that file or the

play11:30

project and play around with instance

play11:32

objects I hope you learned a lot about

play11:34

class objects and constructor methods

play11:37

and how to make your class objects

play11:39

unique thank you for joining me in this

play11:41

episode I'll see you in the next

Rate This

5.0 / 5 (0 votes)

Related Tags
GDScriptObject-OrientedInstantiationConstructorsInstance VariablesClass InstancesProgramming TutorialGame DevelopmentScript FundamentalsCode IsolationCustomization