Class Objects & Constructors | Godot GDScript Tutorial | Ep 16
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
📚 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.
🔍 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.
🛠️ 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
💡Instantiation
💡Instance Variable
💡Typecasting
💡Non-Static Variables
💡Constructor
💡Inheritance
💡Class Name Keyword
💡Default Value
💡Member Function
💡Data Type Inference
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
hello and welcome to another episode in
the GD script fundamental tutorial
series in this episode we will be
talking about objects and constructors
let's start with objects what exactly is
an object well an object is an instance
of a class they can be referred to as
class instances instance objects or
class objects now we need to introduce a
new word which is called instantiation
now instantiation also known as
construction is the realization of a
predefined object an instance of an
object may be declared given a unique
name so that it may be used in a program
now we need to also understand what
exactly an instance variable is now an
instance variable is a member variable
in which each instantiated object of the
class has a separate copy or instance
this means that data changes to one
variable in one instance object will not
change another variable in another
instance object now if you declare a
member variable with a fixed data type
the compiler will do its best to
typecast to a specified datatype if you
were to accidentally try to pass in a
different data type so let's say your
instance variable you declared it with a
data type string
but during runtime you accidently pass
it an integer value what the compiler
will do is change your integer value
into a string value basically wrapping
your integer value inside double
quotations let's go ahead and take a
look at an example of an instance
variable keep in mind for this example
we'll be looking at non-static variables
in our player class non-static forces
static variables are out of topic for
this video and GT script as a language
however for those of you that know
programming a non-static variable is
pretty important when we're talking
about instance variables and code
isolation of our member variables
knowing the difference between static
versa non-static is important however in
GT script we always deal with non-static
variables so our non-static variable a
from our player class has the initial
value of the literal integer 100 now
we're creating two
variables object one in object two and
we're instantiating the player class
into these objects now object one has
the value a and of course it has a
hundred because we're basically copying
the class into our variable and the same
thing for object two we are taking the
variable name and value assigned to it
from player class into our object two
now when we change the value of object 1
in this case assigned a string value to
our a notice how we're not changing the
value in object to this is because each
object has a separate copy of the
variables they receive from the main
class or the instantiated class in this
case our player class so just keep in
mind when you create object variables or
rather if you instantiate class objects
all member variables are isolated to
their own object now moving on when
exactly is it a good time to use
instance objects well a good time to
know you need to use instance objects is
when you want to work with multiple
items or copies of the class
let's take having multiple enemies on
the screen as an example now first
before you can actually instantiate an
object you need to create a class in
this case we've created our enemy class
and we are registering a name into the
editor I personally find using class
name keywords' as a cleaner way of
writing code now again we have our class
name and we are registering the name
enemy to the editor we have a simple
variable a and we have a function called
print a that basically just prints the
variable a or 100 to the console one
thing to note is that there's no extends
keyword that's because we don't need to
inherit anything if we don't plan on
adding this class to the scene tree now
moving on if you want to create an
object first you use the registered name
in this case enemy followed by the new
keyword or the new method now you can
also create a class object with file
paths however it's going to take you two
lines of code to do that let me show you
what first you need to load your class
into the current class in this case we
need to load the enemy class into our
scene script class and we do that we
load the class by simply using the low
keyword followed by an absolute or
relative path inside notice the double
quotations it's a string value you have
to pass inside your load method the
global load method now after you've
loaded your class and assigned it to a
variable in this case load class what
you then do is you call that variable
and then you use the new method
basically this is no different than what
we have earlier except now we have to
use two lines of code instead of one now
the class name when we register our name
to the editor behind the scenes we are
essentially loading it the loading is
done for you and that's why all you need
to do is declare the new method however
the choice is yours
I personally like to keep my code clean
so I like to use the class name keyword
so I only have to write one line of code
instead of two but the choice is up to
you moving on if you want to use a class
object simply use the variable name of
your instance object in this case the
variable we've we've created a copy of
is the object variable so use the object
variable followed by the function name
in your instance object or you can even
use member variables just remember to
omit the parentheses moving on it's
pretty boring to have multiple objects
with the same exact values now the power
of instance objects is the variation you
can have in your object properties to
make objects unique from other objects
simply use the built-in class
constructor to do that simply use the
initialize method or an issue for short
when you use this method inside your
class that class constructor method is
called when the class is instantiated
let's go ahead and take a look at an
example what we have is a function in
ish and we take in a parameter now you
don't have to take in a parameter but at
that point why bother use the initialize
function one thing to keep in mind is
that if you declare a parameter inside
your initialized method but when you
decide to create an instance object of
that class without a parameter you will
throw an error so keep that in mind if
you use an initializer and you declare
parameters
when you create an instance object you
need to specify the parameters now to
avoid this error we simply assign a
default value to assign a default value
to a parameter simply just use the equal
sign followed by the literal value this
will avoid an error if you forget to
pass a value you can also infer datatype
by simply using the colon symbol
followed by the equal sign and not only
will you've assigned a default value to
Perimeter but you would have also
declared a data type which is inferred
by the literal value you're passing it
to if you decide to use the constructor
method in your class to pass a value to
your constructor method all you need to
simply do is use the new keyword and
then just put your value inside the
parentheses and if you have multiple
parameters simply separate each value
with a comma symbol to do this with file
paths you simply wait let me add the
value you simply pass the value into
your new method it's the same thing
again one line of code if you register
your class name to the editor and two
lines of code if you decide to use file
paths to create your instance object now
let's go ahead and look at some code so
I have here a class name and I've
declared a member variable with the data
type string so our variable only accepts
data types of string values now we have
our constructor method and we we can
pass a value and I've also defaulted the
value to the string literal value called
enemy most of the time when you take in
a value in your class constructor you
basically want to pass that value and
assign it to a member variable so we
take that value and we assign it to our
member variable name basically you as a
programmer if you want to use this
method you have a choice you can choose
to create an instance object with no
values passed in the constructor and our
name simply defaults to enemy or you can
create an instance object and pass in a
new name to car enemy now what we have
is a function and it just simply prints
to the console my name is followed by
the name you pass so it will either be
enemy by default or a custom name
you choose by itself this doesn't do
anything as you notice here we don't
have an extend because we're not going
to assign this to the scene tree in real
life production you most likely will but
in this example we aren't now let's go
ahead and take a look at our main script
which will be used in the same tree
notice the extends note key word as you
can see here we can create multiple
instance objects we have two variables
enemy one enemy two and we're going to
create an instance object of our enemy
class in this case the first line will
have the name default and in the second
line the name is jean grey now when we
press the play button notice how enemy
one's name
notice how the member variable can be
changed if we call it directly from the
object in this case we can access member
variables from our object or we can
access functions from our object simply
use the variable name followed by dot
notation followed by either the member
variable name or the function name now
in this case we have print enemy Oh No
let me change that in this case we're
printing to the screen no name now it's
going to print enemy if we don't change
this value here this is the example I
want it to show
now we're gonna print enemy because we
didn't change the member variable after
declaration if you initialize a value
that's the value it is a sign and then
after initialization you can go ahead
and feel free to change the initialized
value for your instance variable now in
the second line we have a again we're
doing a simple function but in this case
from enemy to object and that prints out
Jean Grae now this is an interesting
thing if you were to assign or
accidentally assign a value of a
different datatype keep in mind our
member variable name can only accept
datatype strings so if we try to assign
the value 100 to the value name we're
not going to throw an error what's going
to happen is we're going to or rather
the system is going to try it's best to
cast this as a string in this case when
we call the simple function out which
prints to the console what we're going
to print is
100 and I put double quotations because
what what the compiler ends up doing is
it takes your integer value and just
typecast as a string well that's all I
have for you in this episode I'm gonna
go ahead and upload this script or
rather both scripts to get up so feel
free to download that file or the
project and play around with instance
objects I hope you learned a lot about
class objects and constructor methods
and how to make your class objects
unique thank you for joining me in this
episode I'll see you in the next
Browse More Related Video
Class Inheritance | Godot GDScript Tutorial | Ep 17
PHP Variable Storage & Object Comparison - Zend Value (zval) - Full PHP 8 Tutorial
Python OOP Tutorial 1: Classes and Instances
Object Oriented Programming Features Part 4 | C ++ Tutorial | Mr. Kishore
La Lógica de la Programación Orientada a Objetos explicada con Minecraft
Classes | Godot GDScript Tutorial | Ep 15
5.0 / 5 (0 votes)