0 votes

Hello, i'm new into godot and i would like to ask some question.
So i've been following a grid based movement tutorial by GDQuest (https://www.youtube.com/watch?v=A7xcmDeIoLY). It's outdated for Godot 3.1

I tried to make it work on Godot 3.1. Well, it's work on first and second tutorial (https://www.youtube.com/watch?v=BWBD3i00AfM&t=130s and https://www.youtube.com/watch?v=Yus8zAculWA ). But, after that, the player doesn't wanna move.

I still finding about the error, but can't find any.

Here is my code :
grid.gd (parent)

extends TileMap

var tile_size = get_cell_size()
var half_tile_size = tile_size /2

var grid_size = Vector2(16, 16)
var grid = []

onready var Obstacle = preload("res://Obstacle.tscn")

enum {PLAYER, OBSTACLE, COLLECTIBLE}

func _ready():
    #Create 16x16 grid size
    for x in range(grid_size.x):
        grid.append([])
        for y in range(grid_size.y):
            grid[x].append(null)

    var Player = get_node("Player")
    var start_pos = update_child_pos(Player)
    Player.position = start_pos

    #Create position for obstacle
    var positions = []
    for n in range(5): 
        var grid_pos = Vector2(randi() % int(grid_size.x), randi() % int(grid_size.y))
        if not grid_pos in positions:
            positions.append(grid_pos)

    for pos in positions:       
        var new_obstacle = Obstacle.instance()
        new_obstacle.position = map_to_world(pos)+half_tile_size
        grid[pos.x][pos.y] = OBSTACLE
        add_child(new_obstacle)

#Create a function to prevent player off the grid
func is_cell_vacant(pos, direction):
    var grid_pos = world_to_map(pos) + direction
    if grid_pos.x < grid_size.x and grid_pos.x >= 0:
        if grid_pos.y < grid_size.y and grid_pos.y >= 0:
            return grid[grid_pos.x][grid_pos.y] == null
    return false

#Create a function to update player position
func update_child_pos(child_node):
    var grid_pos = world_to_map(child_node.position)
    print (grid_pos)
    grid[grid_pos.x][grid_pos.y] = null

    var new_grid_pos = grid_pos + child_node.direction
    grid[new_grid_pos.x][new_grid_pos.y] = child_node.type

    var target_pos = map_to_world(new_grid_pos) + half_tile_size
    return target_pos

and this is the player script
player (child node)

extends KinematicBody2D

var type
var grid

var velocity = Vector2()
var direction = Vector2()

var speed = 0
const MAX_SPEED = 400

var is_moving = false
var target_pos = Vector2()
var target_direction = Vector2()

# Called when the node enters the scene tree for the first time.
func _ready():
    grid = get_parent()
    type = grid.PLAYER  
    set_physics_process(true)

func _physics_process(delta):
    direction = Vector2()

    if Input.is_action_pressed("move_up"):
        direction = Vector2(0, -1)
    elif Input.is_action_pressed("move_down"):
        direction = Vector2(0, 1)
    if Input.is_action_pressed("move_right"):
        direction = Vector2(1, 0)
    elif Input.is_action_pressed("move_left"):
        direction = Vector2(-1, 0)

    if direction != Vector2():
        speed = MAX_SPEED
    else:
        speed = 0

    if not is_moving and direction != Vector2():
        target_direction = direction
        if grid.is_cell_vacant(position, target_direction):
            target_pos = grid.update_child_pos(self)
            is_moving = true
    elif is_moving:
        speed = MAX_SPEED
        velocity = speed*target_direction
        var distance_to_target = Vector2(abs(target_pos.x - position.x), abs(target_pos.y -  position.y))

        if abs(velocity.x) > distance_to_target.x:
            velocity.x = distance_to_target.x * target_direction.x
            is_moving = false
        if abs(velocity.y) > distance_to_target.y:
            velocity.y = distance_to_target.y * target_direction.y
            is_moving = false
        move_and_slide(velocity)

Sorry, if codes too long.

in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.