Godot 101 - Part 2: Scripting
Skills:
AI Pair Programming60%
Key Takeaways
Scripts in Godot game engine using GDScript
Full Transcript
welcome back good Mego 101 this is part 2 or we'll start talking about scripting if you haven't watched the previous video please do so every video is going to build on the previous one so I don't want you to fall behind alright let's get started ok so here we have our sprite scene that we created in the last video and when you run it doesn't do very much it just pops up and shows what's our sprite in our game window and so in this video we want to make the spread-eagle move and to do that we need to add a script to it you can add a script to any node in Godot and the code and scripts will act on that node for example you can access and change any of these properties over here in the inspector that the node has like for example if you want to change its position you want to change whether it is visible or not any of these any of these properties can be accessed that you can access in that in the inspector it can also be accessed from code ok so to move the sprite around we want to focus on this property right here pause let's see position so this is the position of the sprite in the scene so when I grab it and move it around that position and value will change and if I go in here and I type that I want it at 300 300 be able to move there but in our script if we write code to change the value of this then we'll see the sprite both so we need to add a script to this node and there's a couple different ways to do that like I said any node can have a script attached to it so if you scroll down to the bottom of the inspector right here there's a script property it's under node because every node can have one and if you pull down around here you can create a new CD script slate or load one that you've already made if you written the script previously do you want to use and so that's one way the other way is you just go up to the node here in the scene and you right click on it you can choose add script and that's what we're going to do that's quicker so we click there and then we get this little box that pops up and asked us about the script that we want to create so the inherits is going to put in here what type of node it is this is a sprite node so it's going to inherit from sprite you don't need to change that from language QT script is the only option right now in the future there will be some other languages that you can use in Godot but let me use GU script and then the path is just going to be where in the game folder that it will be we're just going to leave that as it is right now - it'll create and now here we are in the script editor so this is our default script you see the first line says extend write if you're familiar with object-oriented programming this script is a class every script you make is going to be a class and it's inheriting from the sprite class because that's the type of node that we're wanting to control so by inheriting from the sprite class we will get access to all of the properties that a sprite has so then it has some comments that are automatically inserted you know when they start this is where member variables will go we'll talk about what that means in a minute I'm just going to delete that and then we have a function called underscore ready and like the comments up here this function runs every time the node is added to a seems sort of like your initialization code when your first hit play the sprite is added to the scene and whatever coded in this ready function is going to execute now if you're familiar with Python Gidi script is going to look very similar it has very similar syntax indentation is important and some of the names will be different like we use func to declare a function not death but aside from that it should look pretty familiar to you there's not a lot of resources out there for learning Gd script there's lots out there for so if you want to study Python it'll help you with your GD script so let's just demonstrate how this works what we're going to do is we're just going to fill in I'm going to race the comets here and I'm just going to say print hello right so I want it to print when it gets added to the scene right so that's what the ready will do and I can remove the TAS as well that was just a placeholder so when I hit run [Music] there's my sprite and right here in the output tab I see hello sprite so my code is working so what do we want to do well to start with why don't we do this what we're going to do is we're going to print get pause well let's get Todd let's look at what it does prints out 300 300 because that is the sprites position that's the value right now I don't know I could use this fit Clause function well one thing you can do is if you click on the classes tab here you can see a reference to the documentation so if I go to sprite that's the note we're using these are all the properties that a sprite has right set set texture is centered is slipped you see these are all various properties but you don't see get pause there do you because if we look down here the paws property is part of the node to the class and a sprite is a node to be but all node 2ds have this so this is under the node TD class so if we click here you can see which sprite inherits from node 2 T so if I click on that these are all the functions or methods that a node TD nodes can use and if we go along here we can see there's get pause you're going to hear it tells us it returns the position of the 2d node and it gives us a vector to the vector 2 is a two-dimensional vector with an x and a y component and that is what we're going to that's what we're printing out so now if I want to move this right I mean I want to change its position over time and just like any other game engine because oil engine is running a loop it's running a certain number of frames per second typically 60 and so every 60th of a second I want to move that sprite a little bit so what we need to do is we're going to declare a variable U bar to do that we call it the velocity okay and that's going to be a vector two and I'm just going to put one comma one in there okay I wanted to move one pixel next one one six one Y and now you want this sprite to be something every frame and that means we need to say set process true and what this does is it tells it it's sprite to do something every frame and what it will do every frame is what you put in the process function so anything in this process function will happen every frame so what we're going to do is we're going to tell you to set position to whatever the position is now plus blocking okay and then if we hit play on this now that's what we're going to see is every frame it's adding one pixel in x + 1 pixel in Y and that's fine but we could definitely do better and one of the things we could do better is how to do is this Delta variable ratio because different things could be going on on your computer at different times every frame isn't going to be exactly one sixtieth of a second it might be a little off and what Delta measures is how much time has passed since the last frame it's going to be around 160th but sometimes a little bit less so and we want to use that so that we can make sure there are animation anything moving anything like that is independent of that delay and another word for that is framerate independence so for example if I have a sprite and I wanted to move across the screen at 100 pixels per second I always wanted to move 100 pixels in a second of time regardless of what the framerate is doing whether it's going up or down and that's what Delta lets us do so instead of having our velocity vector just be in you know the units of this would be pixels per frame right we're saying move this mucus with every frame what we want them to we want the unit to be is pixels per second so I'm going to increase these numbers here so I want to move it a hundred pixels per second which is convinced going to be fairly slow because that means it'll take ten seconds to cross the window that's a thousand pixels wide and I'm going to make this one even smaller I'm except 50 and we use that just by going and taking the velocity and multiplying by the Delta so now it'll move whatever fraction of that second that fraction of that speed okay and that's going to look like this so pretty similar still very smooth now we are set up to be independent of the framerate if anything ever happens to slow the framerate down for one or two frames we won't have a you won't see any difference in the movement speed so there's lots of other stuff you can do obviously anything about any other property that you want to change over time you can change them here in the process for example we have this lot property that's the rotation of the sprite so let's change that to be fun so I set the rotation and you can see here it says it's looking for radians so we want to do get whatever the current rotation is plus I'm going to take PI times Delta that means I want to move high in radians represents 180 degrees so when I lose 180 degrees versus rotate 180 degrees for a second let's lie on that you can see it's also going to stand spotless okay so the next thing we need to do is deal with it going off the edge of the screen there but we will save that for the next video we've almost reached 15 that so I don't want to go too much longer with each video thanks for watching please like and subscribe if you enjoyed the video and I will see you next time
Original Description
Godot 101 is an introduction to the Godot game engine and how it works. If you've never used a game engine before, or if you're just new to Godot, this is the place to start.
Text version of this tutorial: http://kidscancode.org/blog/2017/02/godot_101_02/
Get Godot: http://godotengine.org/
Support me on Patreon: http://patreon.com/kidscancode
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Show DEV: MoilStack .md — A fast, private Markdown editor with inline AI
Dev.to · mohammad shafiullah
Bifrost AI Gateway Would Have Saved My App
Dev.to · Lorenzo Zarantonello
50 Best AI Tools in 2026 (Free & Paid) to Boost Productivity
Medium · AI
50 Best AI Tools in 2026 (Free & Paid) to Boost Productivity
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI