Godot 101 - Part 4: Instancing Scenes

KidsCanCode · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Instancing scenes in Godot game engine

Full Transcript

welcome to part four of good 101 in this video we will be talking about taking a scene that you have created and showing you how to instance it or create multiple instances of it as always if you haven't watched the previous videos please go back and do that before watching this one because we are building on the things we've made before all right let's get started okay here's our bouncing Sprite and it's all working well but now the question is what if we want to make a bunch of these what if I want to spawn multiple of these Sprites and have them all bouncing around the screen well you might think you could go over here and just duplicate this node right so if I right click on this and say duplicate oh now I'm going to get this error and I'll zoom in a bit so you can read it the problem is that a scene is a tree right and there can only be one node that's the root of the tree or the one at the top this this node can have children things that are underneath it that are attached to it but it can't have something at the same level as it and we don't want to make another Sprite that's a child of this Sprite because then they would be joined together right the the child of this Sprite would use its position and all that kind of stuff I want another unique Sprite that is equivalent to this one but I don't want to go and make a bunch more Sprite scenes and and duplicate my code and all that kind of stuff well one of the powerful things about scenes and remember a scene is just a collection of nodes our scene here just has this one node but you could have a scene that could have you know multiple nodes in it and those those nodes together with any scripts that are attached to them can be thought of and actually are an object and if you've done objectoriented programming before you know that that allows you to create multiple of the same object to use that object as a definition for creating more and more objects of the same type and that's what we're going to do with this Sprite scene I'm going to write some code to have gdau just spawn a bunch of these Sprite scenes okay so to do that we're going to start by making another new scene I just click on scene new scene and to this scene I'm going to add a node and I'm just going to add the simplest node possible just a node node that's all I care about I just want this this is just a generic node has almost no properties at all but it's just there to serve as the root of my scene and I'm going to click on the name here and I'm going to call this main this is going to be my main scene so if I save it I'm going to save it with the name Main and then I'm going to hit not the play scene button but I'm going to hit this button this is to play the whole project and when you click on this it's going to say you haven't de defined a main scene which scene is the one that starts when you start the project so I'm going to say I want to do that right now so I'm going to click select and I'm going to select the main scene for that so now when I hit Play and play the project my main scene will open of course there's nothing in here just yet so now let's add a script to this right so we're going to add a script we're going to call it main. GD that's fine okay as usual let's remove our comments and we're ready to start coding okay and in this script we can have just like in our Sprite script we could have things that we can have variables we can have uh things that happen when the scene comes into existence in the ready and we could have the underscore process function do things every frame any of that stuff what we want to do is we want to have we want to have a reference to that Sprite scene let's save this sprite. tscn okay now I could do this I'm going to show you what we're going to do let's call this we'll just call this Sprite we want to load this Sprite scene that's saved this one over here as the file we're going to use the preload function okay and what the preload function does is it lets you name some resource and this is the one we want and so I could rightclick on this and choose copy path and then if I go over here and hit paste this is the path to that Sprite scene file and all the resources that you have in your in your gido project are going to be listed like this starts out with Rees which stands for resource uh and just like a URL on the web colon SL slash and then the path to the object in a more complicated project we might have lots and lots of files so we're going to have we would organize them into folders and you would just right if you had a a scenes folder you know you would put it like that but this will be the the this will load our scene and put it in this variable now the problem too that happens is that what order things happen in is important and you want to make sure that if you do something in the ready function that it is actually that it's actually been loaded and is ready to go and so we're going to put on ready here and what on ready does that's going to ensure that this variable is ready and has been loaded off the dis when we're in the ready function and it's not going to give us an error because it hasn't gotten around to doing it yet so in our ready function we just want to spawn a bunch of these and so to spawn one of these Sprites we would just say instance okay that's going to take this Sprite scene and create an instance of it if I hit play oh we don't see anything why is that well that's because we have created this Sprite instance so we've made one of these but we need to add it to our tree so we're going to say add child and that because we're running this on Main that's going to add it as a child of the main node and when we hit play now we have one Sprite and if you want to look we can go over here to the debugger you can actually see if you look at the remote inspector the remote inspector lets you look at the scenes and the nodes that are currently live that are running right now so if we look under here we have there's our main node and underneath of it is a Sprite if I click on Sprite you can see look at that as the position is changing and the rotation is changing as it runs around screen so now if we want to spawn more of these we can just put this in a loop so if I just say for I in range let's just count to 10 and I'm going to put this in a loop so we're just going to count to 10 and add a bunch of sprite nodes to the scene and there you go it just spawned 10 of them and they're all going to do what they're supposed to do and we can look real quick here just to show show you what happens in the tree now if we look at our tree we can see we've got make this a little bigger we've got 10 Sprite children and gdau went ahead and auton named them because they all have to have unique names but they're all and we can click on them and inspect them and see what they're all doing and what's cool is we can actually do that with quite a lot of them right it'll work totally fine and that's it and as we get further into good development that's how you're going to see we're going to do things like bullets and mobs and things like that where you're just going to set up a scene that describes all the functionality of how any particular object in your game works and then in whatever scene you want them to appear you're just going to instance them as you need them do various things with them whatever you want to do that'll do it for this video in the next one we will start looking at some of the other node types and some of the things you can do with them so I'll see you next time thanks for watching

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. Code for this series can be found here: https://github.com/kidscancode/godot_tutorials/tree/master/Godot101 Text version of this tutorial: http://kidscancode.org/blog/2017/02/godot_101_04/ 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 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

📰
Use a model route manifest before Dify, Cursor, and Node.js share Vector Engine
Learn to reduce inconsistencies in shared API gateways by using a model route manifest with Node.js, Dify, and Cursor
Dev.to AI
📰
What 90 Days of Comments on AI Side Panels Taught Me About Distribution
Analyzing 90 days of comments on AI side panels reveals key insights on distribution and user engagement
Dev.to · AI Buddy
📰
Claude vs ChatGPT for Small Business (2026): The Real Numbers Behind the Quiet Takeover
Compare Claude and ChatGPT for small business productivity, with a focus on AI workflows that save time
Medium · AI
📰
AI Builds the Website in Minutes. So What Are You Actually Charging For?
Learn how AI is revolutionizing website production and challenging traditional hourly pricing models, and why you need to adapt your pricing strategy to remain competitive
Medium · AI
Up next
ElevenLabs Review and Tutorial: Free AI Content Tools
LoverFighterWriter
Watch →