How to make a grid based moving system

Godot ver 4.1

I’m trying to make a grid based moving system, where pressing up once moves you up one row, and down moves you down one row. I also want to have regular left-right platformer movement as well. I have no idea what nodes to use, or even how to do any grid stuff.

2 Likes

First, let’s assume that you use a Tilemap as a Grid and a Sprite as a controller. Now you only need to set each Cell of the Tilemap, calculate the center position of each Cell, and then write a controller script to move the position of this Sprite node’s x and y to the center point of the cell.

2 Likes

No hablo complex Godot :laughing:
I’m VERY new to Godot, and I’m still trying to figure things out, so while I kind of understand what you’re trying to say, I have no idea how to do it.

1 Like

I don’t understand what’s not clear. First, you can use Tilemap, and second, write a control script for the sprite. Then calculate the center position of the tilemap cell, change the sprite’s position, and move it to this center position.

2 Likes

I don’t know how to set up a tile map or anything like that.

1 Like

This looks like just what I need, thanks!

2 Likes

I did the tutorial on grid based movement, but when I tried to add regular left-right movement to it, it would just return to where the character was last when moving up or down. I know this has to do with the position stuff, but I don’t know how to fix it.

1 Like

Ok, I’ve almost got it working, just one question: how do you set a movement command to move only the amount set to? (To explain, left/right movement = speed until left/right buttons are released, but I want up and down to be, up/down movement = up/down speed ONCE, until p/down button is pressed again.)

1 Like

Let me explain it a little better: I want to make it so that once you press up/down you move exactly the up/down amount, and does not move any further until up/down is pressed again.

1 Like

So basically, how do you code up movement, but only move x amount once per button press?

1 Like

maybe you want to use Input.is_action_just_pressed():

func _input(event):
	if Input.is_action_just_pressed("left"):
		position.x -= 16

it detects only the beginning of the button press

1 Like

You can use a regular interval (grid Y size) then simply set the up and down keys to add or subtract that number from Y position. To avoid the character from leaving the screen you can simply set an if to check if the Y value is either the first or the last grid position value . Never tried this but it would be my approach.

The code would be the exact same as the comment before mine but using position.y with the if for checking.



func _input(event):

	if Input.is_action_just_pressed("ui_up") && position.y > 90:
		position.y -= 90
	if Input.is_action_just_pressed("ui_down") && position.y < 990:
		position.y += 90

I think this is the right way of writing this? Should check out. The idea of checking if the value is higher or lower is acting as a way to avoid the character from leaving the grid/screen(either upwards or downwards) The check value should be adjusted depending on the quantity and center y coordinate of the grid rows.
Using as an example a resolution of 1080p, i assumed 12 rows so each one should have 90 pixels, taking out the first and last as screen margins. There is no centering here, as that depends of where you want you character to stand on the grid (we are only using the horizontal rows anyways).
You can also do this using global values and making math operations like dividing the viewport height. ( i think you can get it using get_viewport_rect().size.y or something like that, sry i’m not much knowledgeable, pretty new to this too)

1 Like

Hey, this works! Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.