Godot 4 Crash Course #4 - Adding Meteors
Summary
TLDRThis video tutorial guides viewers through creating dynamic meteor scenes in a game using Godot Engine. It covers scripting to spawn meteors at random intervals, loading and instantiating scenes, and attaching them to the scene tree. The script is enhanced to add randomness in meteor graphics, speed, direction, and rotation. The tutorial also addresses collision layers to prevent meteors from interacting with walls, ensuring a more realistic gameplay experience.
Takeaways
- π The script is a tutorial on creating and managing dynamic game objects, specifically meteors, in a game environment using Godot Engine.
- π» The issue addressed is the manual attachment of meteors to a level scene, and the goal is to automate this process by creating a meteor every time a timer triggers.
- π A 'meteor timer' is introduced to handle the spawning of new meteors, which requires disconnecting and reconnecting signals for proper naming and functionality.
- π The process of creating a meteor involves loading a scene, instantiating it, and then attaching it to the scene tree, which is essential for dynamic object creation in Godot.
- π The script covers the steps to instantiate a meteor, including loading the meteor scene as a PackedScene, creating an instance of it, and adding it as a child to the level scene.
- π² The tutorial introduces randomness to the meteor's behavior, including random positions, speeds, directions, and graphics, to enhance gameplay variety.
- π A random number generator is utilized to determine the starting position, speed, and direction of the meteors, contributing to their unpredictable nature in the game.
- π§ The script includes a 'ready' function to set up initial conditions for the meteor, such as position, and a 'process' function to update the meteor's movement continuously.
- πΌοΈ Randomization of the meteor's graphic is discussed, where a random texture is selected from a set of available images to give each meteor a unique appearance.
- π‘οΈ Collision layers and masks are explained to manage which objects can interact with each other, ensuring that meteors only collide with the player and not with the game walls.
- π οΈ The tutorial concludes with setting up collision layers for different game objects, such as players, meteors, lasers, and walls, to fine-tune the game's physics interactions.
Q & A
What is the main issue the speaker is trying to solve in the script?
-The main issue is that the speaker wants to create a meteor every time a timer times out, but currently, they can only manually attach a meteor to the level scene.
What is the purpose of creating a 'meteor timer' in the script?
-The 'meteor timer' is used to trigger the spawning of a new meteor every time it times out, adding randomness to the game scene.
Why does the speaker suggest disconnecting and reconnecting signals in the script?
-The speaker suggests this to ensure a proper name for the method they want to call and to get rid of the previous signal function, making the code more organized and clear.
What is the significance of using a 'Packed Scene' in the script?
-A 'Packed Scene' is used to store the meteor scene as a blueprint, which can then be instantiated multiple times to create actual game objects.
How does the speaker plan to manage multiple meteors in the game scene?
-The speaker plans to manage multiple meteors by creating a new node called 'all meteors' and adding each new meteor as a child to this node, keeping the scene organized.
What is the purpose of the 'ready' function in the context of the meteor script?
-The 'ready' function is used to run code when the meteor node is added to the scene tree, such as setting a random start position for the meteor.
Why is it necessary to create a random number generator (RNG) for the meteor's position?
-The RNG is necessary to set a random start position for the meteor, ensuring that each meteor appears at a different location on the screen.
How does the speaker address the issue of meteors always spawning at the top left of the screen?
-The speaker addresses this by using a random number generator to set a random X and Y position for the meteors when they are spawned.
What is the 'process' function used for in the meteor script?
-The 'process' function is used to update the meteor's position continuously, giving it movement by applying a direction vector multiplied by speed and delta time.
How does the speaker intend to add more randomness to the meteor behavior?
-The speaker intends to add randomness by randomizing the meteor's graphic, speed, direction, and rotation speed, making each meteor's behavior unique.
What is the final step the speaker takes to prevent meteors from colliding with the walls?
-The final step is to adjust the collision layers and masks in the project settings, ensuring that meteors are on a different layer and can only collide with the player, not the walls.
Outlines
π Creating Meteor Scenes with Timers
The script discusses the process of creating meteor scenes in a game via scripting and adding randomness to their appearance. The current issue is that a meteor can only be manually attached to the level scene, which is not ideal for automatic generation upon a timer's trigger. The solution involves creating a 'meteor timer' function that, when called, spawns a new meteor. The steps include loading the meteor scene into a variable, instantiating it to create a new meteor object, and then attaching this object to the level scene. The process is demonstrated in the Godot engine, where the game is still running while the editor is being used to make changes.
π Randomizing Meteor Spawn and Movement
This paragraph delves into randomizing the meteor spawn positions and their movement within the game. It starts by explaining how to create a random number generator to determine the initial position of the meteor on the X and Y axes. The script then discusses adding a 'ready' function to the meteor to set a random starting position when it's added to the scene tree. The movement of the meteors is randomized by setting a 'process' function that updates the meteor's position with a direction vector and speed, multiplied by the frame's delta time for smooth motion. The paragraph also covers the mistake of using a local variable instead of a global one for speed, direction, and rotation, emphasizing the importance of variable scope in scripting.
π² Adding Randomization to Meteor Properties
The focus of this paragraph is on further randomizing the meteor's properties, including its graphic, speed, direction, and rotation. It explains how to set global variables for speed, rotation speed, and direction at the top of the script for use in both the 'ready' and 'process' functions. The script uses the random number generator to assign randomized values to these properties. For the meteor's graphic, it suggests picking one of the five available images at random. The rotation speed is also randomized, and the paragraph provides a clear explanation of the difference between 'rotation' and 'rotation degrees', opting for the latter for this implementation.
πΌοΈ Implementing Random Meteor Graphics
This paragraph covers the implementation of random graphics for the meteors by loading different textures based on a randomized path. It explains how to create a random path by combining strings and integers, and then loading the corresponding texture for the meteor's sprite. The process involves using the 'load' function to get the texture from the randomized path and then assigning it to the meteor's sprite. The paragraph also addresses the issue of meteors colliding with the game's walls, which is undesirable, and suggests a solution by adjusting the collision layers and masks to prevent these collisions.
π‘οΈ Setting Up Collision Layers for Game Objects
The final paragraph discusses setting up collision layers for different game objects to manage interactions effectively. It explains the importance of assigning each game object to a specific layer and configuring the layer's visibility and collision properties. The script outlines the process of renaming the layers in the project settings for better organization and then updating the layer and mask settings for the player, meteors, lasers, and level static bodies. This setup ensures that the meteors only collide with the player and not with the walls, and that lasers can collide with meteors but not with the player. The paragraph concludes with the intention to work on lasers in the subsequent part of the tutorial.
Mindmap
Keywords
π‘Scene Creation
π‘Randomness
π‘Meteor Timer
π‘Packed Scene
π‘Instantiate
π‘Scene Tree
π‘Collision
π‘Signal
π‘Random Number Generator
π‘Layer and Mask
π‘Sprite2D
Highlights
Creating scenes via script and adding randomness to them for dynamic gameplay.
Issue of manually attaching meteors to the level scene and the need for an automated solution.
Introduction of the meteor timer to automate meteor creation.
Steps to load a scene using a packed scene and the load function.
Creating an instance of the meteor scene using the instantiate method.
Attaching the meteor node to the scene tree using add child.
Organizing meteors under a parent node for better scene management.
Adding randomness to meteor spawn positions using a random number generator.
Calculating the display width dynamically to accommodate different window sizes.
Updating meteor positions with random X and Y coordinates for varied spawn locations.
Adding movement to meteors with a process function and delta time for smooth motion.
Randomizing meteor speed, rotation, and direction for unpredictable behavior.
Using global variables for speed, rotation speed, and direction to maintain consistency.
Implementing collision layers and masks to control interactions between objects.
Assigning specific layers and masks to differentiate between player, meteors, walls, and lasers.
Ensuring meteors collide with the player but not with walls for realistic gameplay.
Randomizing meteor graphics by selecting a random texture from a set of images.
Transcripts
welcome back in this part we are going
to create scenes via script and add
Randomness to them basically the issue
we have at the
moment we want to create a meteor every
time the timer times out or triggers but
this we cannot do while we do have a
meteor scene we can only manually attach
it to the level scene and I think doing
all of this in practice is much better
here we are back in G do and at the
moment we do have a meteor scene and
this one works perfectly fine but inside
of the level we can only ever attach a
meteor to it and this happens manually
now we could create multiple meteors by
simply duplicating this node and then we
would have a couple but that doesn't
really solve the issue because we want
to create a new meteor every time the
timer times out this time I to be a bit
more specific and to be clear about this
one let's name it to metor
timer and with that on the right when we
are calling the signal I want to
disconnect all of the signals and click
on okay and then reconnect them because
with that we get a proper name for the
method we want to call you don't have to
do that but I think it makes a bit more
sense also we want to get rid of the
previous signal
function cool so with that we have a
meteor timer timeout function and in
there we want to spawn a whole new
meteor and for that we will need a
couple of steps but now though let me
simply keep pass in here step number
one we have to load the scene and this
we can store in a separate variable
let's call it meteor uncore scene and
for the data type we need a packed scene
to assign this one a value we will need
a function that is called load and if if
we are calling that Gau gives us a whole
bunch of things we could be loading now
in our case we want to have the meteor
TSN make sure to get the right ending
here we do not want to import the script
we want to import the entire scene if I
now press enter good do should be happy
and let me expand the code window to see
everything a bit better next up then we
want to create an instance of this
meteor scene every time this method
triggers and for that I want to create
another variable let's simply call this
one the
meteor and now to create an instance of
the scene we want to have the meteor
scene the one we just created and then
called the instantiate method on it the
way you want to think about all of this
the meteor scene we importing in the
beginning is the blueprint while the
meteor we are creating via instantiate
is going to be the actual building or
car we are creating from that blueprint
so that would be part number two create
and instance that's one more thing that
we need because at the moment we do have
a note that we could work with inside of
the level scene but that isn't enough we
also have to attach the Noe to the level
or rather let's call it the scene
tree and this you can do via ADD child
and then add the meteor and that is all
we need if I now run the code
code you can see in the top left we have
a meteor that is
entering also what you can do if I go
back to gdo while the game is still
running if I bring it up again you can
see we have the game still running
perfectly fine and I can move around
what you can do while the game is
running you can look back at the editor
and then in the scene Tre you can see
remote and local local is what you have
in the editor but if you click on remote
you can see what is actually going on in
real time in the game and there you can
see we adding a whole bunch of area 2D
notes all of those are meteors that are
created via the
timer which is a really good sign so we
know this is working cool although you
can see all of this is a bit badly
organized and to keep all of this a bit
more manageable I want to create another
note
2D which I am going to call all
meteors then I can also get rid of the
meteor we have created
earlier and then when I am adding the
child I do not want to add the child to
the parent note so the note with the
script at the moment I want to add it to
the meteors and for that I can simply
drag and drop the meteors note in there
and then add a DOT and a child if I now
run all of this again we cannot see the
meteor anymore because I deleted this
one but we can still see the meteos in
the top and if I go to remote and expand
level we can see meteors and this one
keeps on growing so that is pretty good
perfect if you want to create an
instance of a scene via code you need
these three steps you have to load a
scene you have to create an instance and
finally you have to attach the note to
the scene tree or to some other note
inside of the scene tree although this
at the moment still isn't amazing
because all of the meteors are always in
the top left to fix that I want to work
inside of the meteor script at the
moment we only have a basic function for
when a body enters this one we can just
ignore it's not too important instead I
want to run some code when we are adding
this node to the scene tree which I can
do via the ready function and just to
make sure that this is working let's
print meteor was added to the scene if I
now run all of this again we can see
that we have meteor was added to the
scene
along with a collision with the wall and
by the way in case you are wondering the
Collision we're getting right now is a
collision between the meteor and the
walls so this is what triggers the
signal we are going to fix that in just
a second actually but for now don't
worry too much about
it so back inside of the meteor I want
to get a random position for that meteor
when it is being spawned and for that we
have to create a random number generator
and stored inside of a variable let's
call it RNG for random number generator
and the data type for this one is going
to be a random number
generator so I am going to create this
one via the semicolon equal sign and
then the method to actually create a
random number generator you want to add
new with that we have a random number
generator and this we want to use to set
this start position for that first of
all we will need the width of the
display so bar width now you could
simply add 1280 in here because that is
the number we have said earlier so if
you go to project project settings and
window this is the number we are working
with right now but this isn't
particularly elegant because the player
might change the window size we might go
to full screen or you might have chosen
a different number instead we want to
get this number dynamically and to get
that you need get viewport then you want
to get visible
rectangle and on that we want to have
the size also let me expand the code
windows so we can see what's going on
size is an attribute so this one doesn't
need to be called now with all of that
let me print what we're getting via
whift it's not exactly what we need if I
now run this again we can see we get
1280 by 720 now on this I I want to use
indexing because I only really care
about the first value which would be the
width if I now run this
again we are getting 1280 or the width
of the window from that we can create
another variable let's call this one
random X and to create a random number
you want to first of all get the random
number generator and then call the
method from it the one that I'm going to
use for this one is Rand I range or
short for random integer range which is
asking has to go from a number to a
number and then the return value is
going to be an integer I want to go from
zero to my whift and then we can print
the outcome So Random X run the code
again and we should be getting a number
between 0 and 1280 and that looks about
right we now have a random X position
but on top of that I also want to have a
random I position although this one is
quite a bit easier once again we can
reuse the the random number generator
and I want to get Rand I
range I want to go
from1 100 to let's say 50 actually
should
be50 once we have those two numbers we
can update the position of the meteor
right away which we can set via a vector
2 that has a random X and a random y
value if I now run all of this again we
well cannot see anything okay my mistake
basically what is happening is we are
creating meteors on top of the window
and since those aren't moving we cannot
see anything but that we can fix quite
easily all we have to do is add a
funcore process with Delta we can simply
update the position once again an area
todde unfortunately doesn't have a move
and slide method so I want to get a
vector 2 with a Direction but now this
can simply be zero and 1.0
this then I want to multiply with some
kind of speed so the meteor isn't too
slow let's say for now 400 and finally
all of this needs to be multiplied with
Delta and now if we run this
again we should be getting meteors
coming from the top and going down and
this is looking really good that is
going to cover the basics of a meteor
but we can add a bit more Randomness
which is going to be your
exercise I want you guys to randomize
the meteor graphic speed and direction
what I mean by meteor graphic is that
inside of the meteor Graphics folder we
have five images in total and when we
are creating the meteor pick one of
those at random after that also rotate
the meteor by a random speed should be
quite a bit to work on so pause the
video now and see how far you
get all right back inside of
Gau I first off I want to add a few more
comments because after the start
position I also want to have speed
rotation and the direction could be in
here as well you do have to be careful
with these numbers because the position
we could set inside of the ready
function but for Speed rotation and
direction we have to reuse all of those
variables inside of the process function
because and there for example the speed
would be necessary as a consequence we
couldn't create a VAR speed for example
inside of the ready function because it
wouldn't be available inside of the
process function so instead what we are
going to do is create a VAR speed all
the way at the top as a global variable
which has to be an integer and by
default we actually don't have to set
anything if you keep it like this it's
totally fine and also while we are here
I want to set VAR rotation speed which
is an integer and finally I want to have
VAR bar let's call it a Direction undor
X and this one should be a floating
point value because for direction. X we
want to have this value for which we do
want to have floating Point values next
up we have to create actual random
values for speech I want to have the
random number generator and then give it
a Rand I range the numbers here are
fairly subjective I find that 200 to 500
are reasonably okay and after we have
that we can replace the 400 with this
speed so now if we are running all of
this nothing seems to be
happening and I can tell you why I made
the exact mistake I told you not to do
this should not be a VAR because that
way we are creating a local variable
inside of this function we instead want
to
overwrite this speed Global variable
inside of the function that way we can
reuse it inside of process so now if I
run this
again there we go this is working much
better and now the meteors move at a
randomized speed bit hard to see but
this is much better cool this works well
and just to be precise here when you're
creating VAR and a keyword then you are
creating a local variable inside of a
function however if you don't leave the
keyword and you create a variable before
that in the global scope then you're
overwriting that variable that way you
can overwrite the variable in one
function and use it in another function
silly mistake sorry about that so next
up we can work on direction. X and we
only care about the X Direction because
Y is always going to be one now the
value for this one is going to be RNG
and importantly we want to have Rand F
range because we want to have floating
Point values that go from -1 all the way
to one these are the values we then want
to use for the vector 2 for the
direction so this one can be Direction
dox and now if I run this again we
should be having meteors that move a bit
more randomly and that feels
significantly
better perfect finally we want to have a
rotation speed and for that we have to
update our rotation speed first of all
so this happens in the same way rotation
speed is going to be rng. Rand
I range for the values I went with 40 to
100 once again entirely subjective just
play around with the numbers and to
actually Implement these values I want
to get rotation or rotation
degrees the difference between the two
is that Rotation by itself Works in
radians while rotation degree Works in
well degrees I tend to prefer degrees
but that's entirely subjective
all we have to do to this one is add the
rotation speed and don't forget to
multiply it with Delta
time and now I can run all of this again
and we are getting random movement well
rotation so that's looking pretty good
very happy and now finally to make all
of this look a bit better when we are
going to the level and the meteor timer
we can reduce the weight time quite a
bit I find 0 .25 is much better and with
that we are getting considerably more
meteors you could probably even lower
the number a bit more let's go with
0.2 but once again this is a number you
can just play around
with and well this is not looking too
bad the last thing we have to work on is
that when when we have a meteor we also
want to have a random graphic if you
look at the graphics folder there on the
meteors we have six different meteors in
total and I want to pick a random
graphic from that this probably was the
most difficult part of the exercise so I
hope it worked out I want to create a
random
texture and for that first of all we
will need a randomized path which has to
be a string and just to get one path
from these folders you can simply drag
in one of the files and then we get let
me expand all of this then we get the
path and ultimately all I want to do is
to randomize this one to be between 1
and six which is actually fairly easy so
first of all I'm going to split these
two strings so we can add something in
between and to combine different strings
all you have to do is to add a value in
between via plus the value that I want
to add is is going to be random number
generator with Rand I range the value
here should be between 1 and six finally
gdau is a bit picky about combining
different data types right now we are
trying to combine a string with an
integer which gdau doesn't like but we
can account for that by converting the
string to an integer via the string
function and with that we are going to
get a random path all we have to do now
is to load the graphic for this
particular path for that if I go back to
the normal view I want to first of all
get this Sprite 2D which I can do by a
dollar sign and Sprite 2D and on there I
want to update the texture so how can I
get this attribute and well the easiest
way is to hover over the attribute in
the inspector and there you can see we
have the texture and the value we have
to assign is a texture to the object but
step by step first of all we want to
have a texture and assign a new value
now to create a texture to object all we
need is load and then load the path we
have just created this is going to
return the object we need and with that
we can actually get rid of the value we
have set by default because we're going
to add something random anyway and now
if I run all of this we should be seeing
different kinds of meteors and that is
looking really good
Perfect all right finally there's one
more thing that I do want to do if I go
back to the level scene at the moment We
have basically walls around the entire
level and the player can collide with
them but also the meteors are colliding
with them which is not ideal and because
of that if I run the code again we get
all of these messages that a static body
2D has entered the area 2D of the meteor
which I really don't want so ideally I
want the meteor to ignore these wordss
entirely and that you can do fairly
easily so if I go to the meteor there we
have collision and in there we have a
layer and we have a mask the way this
system works is that every physics
object is on a layer and it can see
whatever is on a mask for example right
now the meteor is on layer one and it
can see all of the other physics objects
which are also on this layer at the
moment every single physics object is on
layer one and can only see mask one so
everything can see everything else but
now watch what happens if I set this to
a two then if I run the code again the
meteors are not going to collide with
anything simply because none of the
physics object is on the second
layer however if I set the player on
this layer so under Collision I want the
player to be on Layer Two if I now run
this again the meteors can can see the
player again so we get the function back
and this is much
better although this system can get very
complex so simply working with numbers
isn't great instead what you usually do
is you go to project project settings
and in there we have under layer names
2D physics and there can give each layer
a name in my case for the names usually
layer one is player for me Layer Two in
this case I want to be meteor layer
three is going to be walls and layer
four is going to be
laser and with that we have to make
quite a few updates so let's go through
it step by step first of all we have the
player itself and the player should only
be on layer one which is for the player
the player then should not be able to
see itself however it should be able to
see the meteors and the wals but it
cannot see the lasers those we haven't
worked on yet but that's going to come
later next up the meteor metors the
meteors should not be on the player
layer instead they should be on layer
number two and they can only see the
player and they should also be able to
see the lasers after that we have the
lasers and the lasers under the
Collision should be on layer three four
actually and they should be able to see
not the player but they should be able
to see the
meteors finally we have the level
because in the level
we have the four static bodies those
also need to be updated because at the
moment they can only see layout one
which is the player that's a good start
but they shouldn't be the player
themselves they should instead be on
layer three and this by the way you have
to do with all of them so this is a
system you want to set up early
otherwise it can get a bit
tedious so with that we should have all
of the collisions set up properly
which means now the player can still
collide with the meteors that is looking
good but the meteors cannot collide with
the walls and later on once we get
lasers we can also make sure that the
lasers collide with the meteors for now
I'm pretty confident that this is going
to work next up we'll work on the lasers
Browse More Related Video
How To Make a Topdown RPG in Godot 4: Adding The Player (Part 1)
Learn C# Scripting for Unity in 15 Minutes (2024 Working)
Learn C# Scripting For Unity in 15 Minutes - Part 2
Space Shooter in Godot 4 - part 1 (introduction)
Start Your Game Creation Journey Today! (Godot beginner tutorial)
Unity 2D CHECKPOINTS Tutorial (Simple and Easy!) | Unity 2D Platformer Tutorial #9
5.0 / 5 (0 votes)