Godot Engine - Know Your Nodes: Tilemap (part 2)
Key Takeaways
Interacts with TileMap node via code in Godot Engine
Full Transcript
welcome back to another know your nodes Godot game engine video in this lesson we're going to be looking at tile Maps again this time we're going to be talking about how to interact with the tile map from your code so that you can query and modify the tile map during the game in the previous video we showed how to use the tile map node to set up our map for the simple 2d platformer example we also showed how to do a top-down map and in this video we're going to look at how to use GD script to interact with this map during the game here's the tile map node documentation and you can see there's lots of methods for interacting with the tile map and setting and getting various settings for the individual tiles as well as the tile map as a whole one of the ones that you're going to use the most or the most often is going to be this world 2 map method this lets you convert from screen coordinates and pixels to map coordinates on the tile map grid so here's our script and what this script is going to do is detect amount left mouse click and display the global mouse position it's going to look like this so whenever I click appear it's showing me the pixel coordinates where on the screen I clicked and what I want to know is what are the tile coordinates on the tile map of that click location so now to get that tile coordinate we are just going to use the map world to map method and we're going to pass it the mouse pause and that's it so now when we run it we will see when I click up here in the corner that's tile zero zero and anywhere I click on the screen I'm now finding out what my tile position is on the top map now that we know what tile we've clicked on let's say we want to change the tile that's there so we can use map set cell and then you give it an X and a Y and what tile you want to do so let's say I do tile number five and what that means is anywhere I click I'm going to place a tile number five they're still there if you want a tile to be removed negative one means no tile at that location so that's how you can remove tiles from the map when you click as well now it's also useful there is a set cell there's also a set cell V so tell V lets you pass a vector instead of individual X and Y's which means we could just say that which is a little bit more convenient now let's say instead of the mouse I want to detect which tile the player collides with well on the player script if we go over to the player script and we put something like if is colliding print and we're just going to print out get Collider get name so you can see what we're colliding with and if we do that you'll see it's printing up map 1 so what happens is the collision detection doesn't see the individual tiles because they're part of the top map and because it's more efficient this way you just collide with the map object wherever you collide so let's look at how we would do that on the player I'm just going to create a signal called hit it's going to emit a position and then if we do collide we're just going to admit that hit signal with the collision position and then over on our main we're going to connect that signal from the player to a function called collided and that collided function is just going to print out the world to map which means it should print out the tile coordinate of our collision so let's see how that looks there we go as I'm moving around you can see down there at the bottom it's printing out different tile positions so now instead of printing out let's do a set Selvi and take that position that cell and set it to negative one which means now whenever we hit a tile we're going to delete it now if you wanted to do something like this where every time you collide with the tile you change it to a different tile then you're going to get something like this which is fine except you want to be a little careful about this because you're going to run into some issues for example when I jump off this way I'm fine but if I go this direction I'm spawning another tile and that's because the collision position is rounding to the next tile because I'm technically hitting the tile on the side and that's going to lead to all sorts of problems especially if you start trying to jump and you're hitting the tile from below but your collision position is still in the current tile so you need to add some logic there to make sure you don't get stuck in some weird positions like that so a quick way we could solve that would be just to check what is in the cell before we replace it so we'll just use map get cell V and we're going to get this location where we are looking and if that tile is not is not equal to negative one then we'll set the cell to five and that way we will still change them when we land on them but we can't change any of the empty tiles into solid ones all right I hope this gives you some ideas how you could use tile maps in your projects please post your questions in the comments below there's anything you didn't understand I'll be happy to help you out in the next video we're going to do one more look into tile maps and this time to look at a couple of requested topics isometric tom maps and animated tiles thanks as always for watching and I'll see you in the next video you
Original Description
Continuing to explore the TileMap node - this time how to interact with your map via code, so you can get and even change your map's tiles during gameplay.
Sample project(s) from this video can be found here:
https://github.com/kidscancode/godot_tutorials/releases/tag/Know_Your_Nodes
Support me on Patreon: http://patreon.com/kidscancode
Thanks to https://github.com/djrm/godot-design for title image.
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
Related Reads
🎓
Tutor Explanation
DeepCamp AI