Godot 4 Crash Course #4 - Adding Meteors

Net Ninja
1 Feb 202422:17

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

00:00

🌌 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.

05:02

🚀 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.

10:03

🎲 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.

15:05

🖼️ 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.

20:07

🛡️ 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

Scene creation refers to the process of generating a specific environment or setting within a game or interactive application. In the context of the video, scene creation is central to the theme as the script discusses creating a 'meteor scene' programmatically. The video provides a step-by-step guide on how to script the creation of a meteor scene in Godot, which is essential for adding dynamic elements like meteors to a game level.

💡Randomness

Randomness is the concept of unpredictability or lack of pattern in events. In the video, randomness is a key concept used to make the game more dynamic and less predictable by introducing variability in the appearance and behavior of meteors. The script mentions adding randomness to the meteor's speed, direction, and graphic, which contributes to the video's theme of creating engaging and varied gameplay experiences.

💡Meteor Timer

A 'meteor timer' is a game mechanic used to trigger events, such as the spawning of a meteor, after a set period of time. The script explains how to set up a timer in Godot that, upon timing out, will execute a function to create a new meteor. This concept is integral to the video's theme of automating scene elements and adding an element of surprise to the game.

💡Packed Scene

A 'packed scene' in Godot is a reference to a pre-configured scene that can be saved and reused in different parts of a game. The video script describes using a packed scene to load the meteor scene, which is then instantiated multiple times with variations to add meteors to the game level. This concept is crucial for understanding how to efficiently manage and reuse game assets.

💡Instantiate

To 'instantiate' in the context of game development means to create a new instance of a pre-defined object or scene. The script details the process of instantiating the meteor scene to spawn new meteors in the game world. This action is a fundamental part of the video's theme, demonstrating how to dynamically generate game objects in response to in-game events.

💡Scene Tree

The 'scene tree' in Godot represents the hierarchy of nodes that make up a game's scene. The video script discusses attaching newly created meteor instances to the scene tree, which is essential for integrating them into the game world. Understanding the scene tree is key to the video's narrative of constructing and managing game scenes programmatically.

💡Collision

In game development, 'collision' refers to the interaction between two objects when they come into contact with each other. The script mentions handling collisions between the meteor and the game's walls, which is an important aspect of the video's theme of creating interactive and responsive game environments. The video also discusses modifying collision layers to prevent meteors from interacting with walls, which is crucial for fine-tuning gameplay.

💡Signal

A 'signal' in Godot is an event that a node can emit to communicate with other nodes. The video script explains how to use signals, specifically the 'timeout' signal from a timer, to trigger the creation of a meteor. This concept is central to the video's theme of creating interactive and event-driven game mechanics.

💡Random Number Generator

A 'random number generator' is a tool used to produce a sequence of numbers that cannot be reasonably predicted better than by a random chance. The script uses a random number generator to determine the starting position, speed, and direction of meteors, which is essential for adding unpredictability to the game's meteor behavior, aligning with the video's theme of creating randomized game elements.

💡Layer and Mask

In Godot, 'layers' and 'masks' are used to define which objects can interact with each other in a game. The video script discusses setting up collision layers and masks to control which objects can collide with meteors, such as ensuring that meteors do not collide with walls. This concept is vital for the video's theme of fine-tuning game interactions and collision mechanics.

💡Sprite2D

A 'Sprite2D' in Godot is a node used to display a 2D image or animation. The script mentions updating the texture of a Sprite2D to randomize the meteor graphics, which is a key part of the video's theme of adding visual variety to the game. This concept is important for understanding how to dynamically change the appearance of game objects.

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

play00:00

welcome back in this part we are going

play00:02

to create scenes via script and add

play00:05

Randomness to them basically the issue

play00:08

we have at the

play00:09

moment we want to create a meteor every

play00:12

time the timer times out or triggers but

play00:15

this we cannot do while we do have a

play00:18

meteor scene we can only manually attach

play00:21

it to the level scene and I think doing

play00:24

all of this in practice is much better

play00:27

here we are back in G do and at the

play00:29

moment we do have a meteor scene and

play00:32

this one works perfectly fine but inside

play00:35

of the level we can only ever attach a

play00:38

meteor to it and this happens manually

play00:42

now we could create multiple meteors by

play00:44

simply duplicating this node and then we

play00:47

would have a couple but that doesn't

play00:49

really solve the issue because we want

play00:51

to create a new meteor every time the

play00:53

timer times out this time I to be a bit

play00:56

more specific and to be clear about this

play00:59

one let's name it to metor

play01:02

timer and with that on the right when we

play01:05

are calling the signal I want to

play01:08

disconnect all of the signals and click

play01:10

on okay and then reconnect them because

play01:13

with that we get a proper name for the

play01:15

method we want to call you don't have to

play01:17

do that but I think it makes a bit more

play01:20

sense also we want to get rid of the

play01:22

previous signal

play01:24

function cool so with that we have a

play01:27

meteor timer timeout function and in

play01:30

there we want to spawn a whole new

play01:32

meteor and for that we will need a

play01:35

couple of steps but now though let me

play01:37

simply keep pass in here step number

play01:40

one we have to load the scene and this

play01:45

we can store in a separate variable

play01:47

let's call it meteor uncore scene and

play01:50

for the data type we need a packed scene

play01:54

to assign this one a value we will need

play01:57

a function that is called load and if if

play02:00

we are calling that Gau gives us a whole

play02:02

bunch of things we could be loading now

play02:04

in our case we want to have the meteor

play02:07

TSN make sure to get the right ending

play02:10

here we do not want to import the script

play02:12

we want to import the entire scene if I

play02:14

now press enter good do should be happy

play02:17

and let me expand the code window to see

play02:20

everything a bit better next up then we

play02:22

want to create an instance of this

play02:24

meteor scene every time this method

play02:27

triggers and for that I want to create

play02:30

another variable let's simply call this

play02:32

one the

play02:33

meteor and now to create an instance of

play02:36

the scene we want to have the meteor

play02:38

scene the one we just created and then

play02:41

called the instantiate method on it the

play02:45

way you want to think about all of this

play02:46

the meteor scene we importing in the

play02:48

beginning is the blueprint while the

play02:50

meteor we are creating via instantiate

play02:53

is going to be the actual building or

play02:55

car we are creating from that blueprint

play02:58

so that would be part number two create

play03:01

and instance that's one more thing that

play03:04

we need because at the moment we do have

play03:07

a note that we could work with inside of

play03:09

the level scene but that isn't enough we

play03:13

also have to attach the Noe to the level

play03:17

or rather let's call it the scene

play03:20

tree and this you can do via ADD child

play03:24

and then add the meteor and that is all

play03:27

we need if I now run the code

play03:31

code you can see in the top left we have

play03:34

a meteor that is

play03:37

entering also what you can do if I go

play03:40

back to gdo while the game is still

play03:42

running if I bring it up again you can

play03:44

see we have the game still running

play03:47

perfectly fine and I can move around

play03:50

what you can do while the game is

play03:51

running you can look back at the editor

play03:53

and then in the scene Tre you can see

play03:56

remote and local local is what you have

play03:59

in the editor but if you click on remote

play04:02

you can see what is actually going on in

play04:04

real time in the game and there you can

play04:06

see we adding a whole bunch of area 2D

play04:09

notes all of those are meteors that are

play04:12

created via the

play04:13

timer which is a really good sign so we

play04:16

know this is working cool although you

play04:19

can see all of this is a bit badly

play04:21

organized and to keep all of this a bit

play04:23

more manageable I want to create another

play04:26

note

play04:27

2D which I am going to call all

play04:31

meteors then I can also get rid of the

play04:33

meteor we have created

play04:36

earlier and then when I am adding the

play04:38

child I do not want to add the child to

play04:41

the parent note so the note with the

play04:42

script at the moment I want to add it to

play04:45

the meteors and for that I can simply

play04:47

drag and drop the meteors note in there

play04:49

and then add a DOT and a child if I now

play04:53

run all of this again we cannot see the

play04:55

meteor anymore because I deleted this

play04:57

one but we can still see the meteos in

play04:58

the top and if I go to remote and expand

play05:02

level we can see meteors and this one

play05:05

keeps on growing so that is pretty good

play05:08

perfect if you want to create an

play05:10

instance of a scene via code you need

play05:12

these three steps you have to load a

play05:14

scene you have to create an instance and

play05:17

finally you have to attach the note to

play05:18

the scene tree or to some other note

play05:20

inside of the scene tree although this

play05:22

at the moment still isn't amazing

play05:25

because all of the meteors are always in

play05:26

the top left to fix that I want to work

play05:30

inside of the meteor script at the

play05:33

moment we only have a basic function for

play05:36

when a body enters this one we can just

play05:39

ignore it's not too important instead I

play05:41

want to run some code when we are adding

play05:44

this node to the scene tree which I can

play05:46

do via the ready function and just to

play05:48

make sure that this is working let's

play05:50

print meteor was added to the scene if I

play05:55

now run all of this again we can see

play05:57

that we have meteor was added to the

play05:59

scene

play06:00

along with a collision with the wall and

play06:03

by the way in case you are wondering the

play06:06

Collision we're getting right now is a

play06:08

collision between the meteor and the

play06:09

walls so this is what triggers the

play06:12

signal we are going to fix that in just

play06:15

a second actually but for now don't

play06:17

worry too much about

play06:18

it so back inside of the meteor I want

play06:22

to get a random position for that meteor

play06:25

when it is being spawned and for that we

play06:28

have to create a random number generator

play06:31

and stored inside of a variable let's

play06:33

call it RNG for random number generator

play06:36

and the data type for this one is going

play06:39

to be a random number

play06:42

generator so I am going to create this

play06:44

one via the semicolon equal sign and

play06:47

then the method to actually create a

play06:50

random number generator you want to add

play06:52

new with that we have a random number

play06:56

generator and this we want to use to set

play06:59

this start position for that first of

play07:02

all we will need the width of the

play07:04

display so bar width now you could

play07:07

simply add 1280 in here because that is

play07:10

the number we have said earlier so if

play07:12

you go to project project settings and

play07:14

window this is the number we are working

play07:16

with right now but this isn't

play07:18

particularly elegant because the player

play07:20

might change the window size we might go

play07:22

to full screen or you might have chosen

play07:24

a different number instead we want to

play07:27

get this number dynamically and to get

play07:29

that you need get viewport then you want

play07:33

to get visible

play07:36

rectangle and on that we want to have

play07:39

the size also let me expand the code

play07:42

windows so we can see what's going on

play07:44

size is an attribute so this one doesn't

play07:46

need to be called now with all of that

play07:49

let me print what we're getting via

play07:51

whift it's not exactly what we need if I

play07:54

now run this again we can see we get

play07:56

1280 by 720 now on this I I want to use

play07:59

indexing because I only really care

play08:01

about the first value which would be the

play08:03

width if I now run this

play08:06

again we are getting 1280 or the width

play08:09

of the window from that we can create

play08:11

another variable let's call this one

play08:14

random X and to create a random number

play08:18

you want to first of all get the random

play08:19

number generator and then call the

play08:21

method from it the one that I'm going to

play08:23

use for this one is Rand I range or

play08:26

short for random integer range which is

play08:28

asking has to go from a number to a

play08:30

number and then the return value is

play08:32

going to be an integer I want to go from

play08:35

zero to my whift and then we can print

play08:39

the outcome So Random X run the code

play08:43

again and we should be getting a number

play08:45

between 0 and 1280 and that looks about

play08:48

right we now have a random X position

play08:51

but on top of that I also want to have a

play08:53

random I position although this one is

play08:56

quite a bit easier once again we can

play08:58

reuse the the random number generator

play09:00

and I want to get Rand I

play09:02

range I want to go

play09:05

from1 100 to let's say 50 actually

play09:10

should

play09:11

be50 once we have those two numbers we

play09:14

can update the position of the meteor

play09:16

right away which we can set via a vector

play09:19

2 that has a random X and a random y

play09:23

value if I now run all of this again we

play09:26

well cannot see anything okay my mistake

play09:29

basically what is happening is we are

play09:31

creating meteors on top of the window

play09:34

and since those aren't moving we cannot

play09:36

see anything but that we can fix quite

play09:38

easily all we have to do is add a

play09:42

funcore process with Delta we can simply

play09:45

update the position once again an area

play09:48

todde unfortunately doesn't have a move

play09:50

and slide method so I want to get a

play09:53

vector 2 with a Direction but now this

play09:55

can simply be zero and 1.0

play09:59

this then I want to multiply with some

play10:01

kind of speed so the meteor isn't too

play10:03

slow let's say for now 400 and finally

play10:06

all of this needs to be multiplied with

play10:08

Delta and now if we run this

play10:11

again we should be getting meteors

play10:14

coming from the top and going down and

play10:16

this is looking really good that is

play10:20

going to cover the basics of a meteor

play10:22

but we can add a bit more Randomness

play10:24

which is going to be your

play10:26

exercise I want you guys to randomize

play10:29

the meteor graphic speed and direction

play10:32

what I mean by meteor graphic is that

play10:34

inside of the meteor Graphics folder we

play10:36

have five images in total and when we

play10:38

are creating the meteor pick one of

play10:40

those at random after that also rotate

play10:43

the meteor by a random speed should be

play10:47

quite a bit to work on so pause the

play10:48

video now and see how far you

play10:53

get all right back inside of

play10:57

Gau I first off I want to add a few more

play11:00

comments because after the start

play11:02

position I also want to have speed

play11:06

rotation and the direction could be in

play11:08

here as well you do have to be careful

play11:10

with these numbers because the position

play11:13

we could set inside of the ready

play11:15

function but for Speed rotation and

play11:18

direction we have to reuse all of those

play11:20

variables inside of the process function

play11:23

because and there for example the speed

play11:25

would be necessary as a consequence we

play11:28

couldn't create a VAR speed for example

play11:31

inside of the ready function because it

play11:33

wouldn't be available inside of the

play11:35

process function so instead what we are

play11:37

going to do is create a VAR speed all

play11:40

the way at the top as a global variable

play11:42

which has to be an integer and by

play11:44

default we actually don't have to set

play11:46

anything if you keep it like this it's

play11:49

totally fine and also while we are here

play11:52

I want to set VAR rotation speed which

play11:56

is an integer and finally I want to have

play11:58

VAR bar let's call it a Direction undor

play12:02

X and this one should be a floating

play12:04

point value because for direction. X we

play12:07

want to have this value for which we do

play12:10

want to have floating Point values next

play12:13

up we have to create actual random

play12:14

values for speech I want to have the

play12:17

random number generator and then give it

play12:19

a Rand I range the numbers here are

play12:23

fairly subjective I find that 200 to 500

play12:27

are reasonably okay and after we have

play12:30

that we can replace the 400 with this

play12:32

speed so now if we are running all of

play12:36

this nothing seems to be

play12:38

happening and I can tell you why I made

play12:41

the exact mistake I told you not to do

play12:43

this should not be a VAR because that

play12:45

way we are creating a local variable

play12:47

inside of this function we instead want

play12:50

to

play12:51

overwrite this speed Global variable

play12:54

inside of the function that way we can

play12:57

reuse it inside of process so now if I

play12:59

run this

play13:00

again there we go this is working much

play13:03

better and now the meteors move at a

play13:04

randomized speed bit hard to see but

play13:08

this is much better cool this works well

play13:11

and just to be precise here when you're

play13:13

creating VAR and a keyword then you are

play13:17

creating a local variable inside of a

play13:19

function however if you don't leave the

play13:23

keyword and you create a variable before

play13:25

that in the global scope then you're

play13:27

overwriting that variable that way you

play13:30

can overwrite the variable in one

play13:31

function and use it in another function

play13:34

silly mistake sorry about that so next

play13:36

up we can work on direction. X and we

play13:40

only care about the X Direction because

play13:42

Y is always going to be one now the

play13:45

value for this one is going to be RNG

play13:48

and importantly we want to have Rand F

play13:51

range because we want to have floating

play13:54

Point values that go from -1 all the way

play13:57

to one these are the values we then want

play13:59

to use for the vector 2 for the

play14:01

direction so this one can be Direction

play14:03

dox and now if I run this again we

play14:07

should be having meteors that move a bit

play14:09

more randomly and that feels

play14:12

significantly

play14:13

better perfect finally we want to have a

play14:16

rotation speed and for that we have to

play14:19

update our rotation speed first of all

play14:21

so this happens in the same way rotation

play14:23

speed is going to be rng. Rand

play14:27

I range for the values I went with 40 to

play14:34

100 once again entirely subjective just

play14:37

play around with the numbers and to

play14:40

actually Implement these values I want

play14:42

to get rotation or rotation

play14:46

degrees the difference between the two

play14:49

is that Rotation by itself Works in

play14:50

radians while rotation degree Works in

play14:53

well degrees I tend to prefer degrees

play14:56

but that's entirely subjective

play14:59

all we have to do to this one is add the

play15:02

rotation speed and don't forget to

play15:05

multiply it with Delta

play15:07

time and now I can run all of this again

play15:10

and we are getting random movement well

play15:14

rotation so that's looking pretty good

play15:17

very happy and now finally to make all

play15:20

of this look a bit better when we are

play15:22

going to the level and the meteor timer

play15:25

we can reduce the weight time quite a

play15:27

bit I find 0 .25 is much better and with

play15:32

that we are getting considerably more

play15:36

meteors you could probably even lower

play15:39

the number a bit more let's go with

play15:43

0.2 but once again this is a number you

play15:46

can just play around

play15:50

with and well this is not looking too

play15:55

bad the last thing we have to work on is

play15:58

that when when we have a meteor we also

play16:00

want to have a random graphic if you

play16:02

look at the graphics folder there on the

play16:05

meteors we have six different meteors in

play16:08

total and I want to pick a random

play16:10

graphic from that this probably was the

play16:13

most difficult part of the exercise so I

play16:15

hope it worked out I want to create a

play16:18

random

play16:19

texture and for that first of all we

play16:22

will need a randomized path which has to

play16:25

be a string and just to get one path

play16:29

from these folders you can simply drag

play16:30

in one of the files and then we get let

play16:33

me expand all of this then we get the

play16:36

path and ultimately all I want to do is

play16:39

to randomize this one to be between 1

play16:41

and six which is actually fairly easy so

play16:45

first of all I'm going to split these

play16:46

two strings so we can add something in

play16:49

between and to combine different strings

play16:52

all you have to do is to add a value in

play16:54

between via plus the value that I want

play16:57

to add is is going to be random number

play16:59

generator with Rand I range the value

play17:04

here should be between 1 and six finally

play17:08

gdau is a bit picky about combining

play17:10

different data types right now we are

play17:12

trying to combine a string with an

play17:14

integer which gdau doesn't like but we

play17:17

can account for that by converting the

play17:19

string to an integer via the string

play17:22

function and with that we are going to

play17:25

get a random path all we have to do now

play17:28

is to load the graphic for this

play17:31

particular path for that if I go back to

play17:36

the normal view I want to first of all

play17:39

get this Sprite 2D which I can do by a

play17:42

dollar sign and Sprite 2D and on there I

play17:46

want to update the texture so how can I

play17:49

get this attribute and well the easiest

play17:51

way is to hover over the attribute in

play17:53

the inspector and there you can see we

play17:55

have the texture and the value we have

play17:57

to assign is a texture to the object but

play18:00

step by step first of all we want to

play18:02

have a texture and assign a new value

play18:05

now to create a texture to object all we

play18:08

need is load and then load the path we

play18:11

have just created this is going to

play18:13

return the object we need and with that

play18:16

we can actually get rid of the value we

play18:18

have set by default because we're going

play18:20

to add something random anyway and now

play18:22

if I run all of this we should be seeing

play18:25

different kinds of meteors and that is

play18:27

looking really good

play18:29

Perfect all right finally there's one

play18:31

more thing that I do want to do if I go

play18:34

back to the level scene at the moment We

play18:37

have basically walls around the entire

play18:39

level and the player can collide with

play18:42

them but also the meteors are colliding

play18:44

with them which is not ideal and because

play18:46

of that if I run the code again we get

play18:48

all of these messages that a static body

play18:51

2D has entered the area 2D of the meteor

play18:54

which I really don't want so ideally I

play18:57

want the meteor to ignore these wordss

play19:00

entirely and that you can do fairly

play19:02

easily so if I go to the meteor there we

play19:05

have collision and in there we have a

play19:07

layer and we have a mask the way this

play19:10

system works is that every physics

play19:12

object is on a layer and it can see

play19:15

whatever is on a mask for example right

play19:18

now the meteor is on layer one and it

play19:20

can see all of the other physics objects

play19:22

which are also on this layer at the

play19:24

moment every single physics object is on

play19:27

layer one and can only see mask one so

play19:30

everything can see everything else but

play19:32

now watch what happens if I set this to

play19:34

a two then if I run the code again the

play19:38

meteors are not going to collide with

play19:40

anything simply because none of the

play19:42

physics object is on the second

play19:44

layer however if I set the player on

play19:49

this layer so under Collision I want the

play19:52

player to be on Layer Two if I now run

play19:55

this again the meteors can can see the

play19:58

player again so we get the function back

play20:01

and this is much

play20:03

better although this system can get very

play20:06

complex so simply working with numbers

play20:08

isn't great instead what you usually do

play20:11

is you go to project project settings

play20:13

and in there we have under layer names

play20:17

2D physics and there can give each layer

play20:19

a name in my case for the names usually

play20:22

layer one is player for me Layer Two in

play20:25

this case I want to be meteor layer

play20:27

three is going to be walls and layer

play20:30

four is going to be

play20:32

laser and with that we have to make

play20:34

quite a few updates so let's go through

play20:37

it step by step first of all we have the

play20:39

player itself and the player should only

play20:42

be on layer one which is for the player

play20:45

the player then should not be able to

play20:47

see itself however it should be able to

play20:49

see the meteors and the wals but it

play20:52

cannot see the lasers those we haven't

play20:54

worked on yet but that's going to come

play20:56

later next up the meteor metors the

play20:59

meteors should not be on the player

play21:01

layer instead they should be on layer

play21:03

number two and they can only see the

play21:06

player and they should also be able to

play21:09

see the lasers after that we have the

play21:11

lasers and the lasers under the

play21:14

Collision should be on layer three four

play21:18

actually and they should be able to see

play21:21

not the player but they should be able

play21:22

to see the

play21:24

meteors finally we have the level

play21:27

because in the level

play21:28

we have the four static bodies those

play21:30

also need to be updated because at the

play21:33

moment they can only see layout one

play21:37

which is the player that's a good start

play21:39

but they shouldn't be the player

play21:40

themselves they should instead be on

play21:42

layer three and this by the way you have

play21:45

to do with all of them so this is a

play21:47

system you want to set up early

play21:49

otherwise it can get a bit

play21:53

tedious so with that we should have all

play21:56

of the collisions set up properly

play21:58

which means now the player can still

play22:00

collide with the meteors that is looking

play22:02

good but the meteors cannot collide with

play22:05

the walls and later on once we get

play22:07

lasers we can also make sure that the

play22:09

lasers collide with the meteors for now

play22:12

I'm pretty confident that this is going

play22:14

to work next up we'll work on the lasers

Rate This

5.0 / 5 (0 votes)

Связанные теги
Godot EngineGame DevelopmentScripting TutorialRandomizationScene CreationPhysics EngineMeteor MechanicsCollision DetectionGame DesignLevel Design
Вам нужно краткое изложение на английском?