0 votes

Player script

extends TileMap
#experimental variables
var count = 0

var players = []
#end experimental variables
enum ENTITY_TYPES {PLAYER, OBSTACLE, COLLECTIBLE}

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")
onready var Player = preload("res://Player.tscn")


func _ready():

for x in range(grid_size.x):
    grid.append([])
    for y in range(grid_size.y):
        grid[x].append(null)

# Player






# Obstacles
var positions = []
for x in range(5):
    var placed = false
    while not placed:
        var grid_pos = Vector2(randi() % int(grid_size.x), randi() % int(grid_size.y))
        if not grid[grid_pos.x][grid_pos.y]:
            if not grid_pos in positions:
                positions.append(grid_pos)
                placed = true

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


var new_player = Player.instance()
new_player.set_pos(map_to_world(Vector2(6,6)) + half_tile_size)
add_child(new_player)
new_player.player_number = 0
new_player.player_turn = false
players.append(new_player)

var new_player2 = Player.instance()
new_player2.set_pos(map_to_world(Vector2(6,5)) + half_tile_size)
add_child(new_player2)  
new_player2.player_number = 1
new_player.player_turn = true
players.append(new_player2)

func return_next_player(player_number):

if player_number < players.size() -1: 
    players[player_number].player_turn = false
    player_number = player_number + 1
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    players[player_number].player_turn = true
    print ("player number is: ", player_number)

elif player_number == players.size() -1:
    players[player_number].player_turn = false
    player_number = 0
    players[player_number].player_turn = true
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    print ("player number is: ", player_number)



func get_cell_content(pos=Vector2()):
    return grid[pos.x][pos.y]


func is_cell_vacant(pos=Vector2(), direction=Vector2()):
    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 true if grid[grid_pos.x][grid_pos.y] == null else false
return false


func update_child_pos(new_pos, direction, type):
    # Remove node from current cell, add it to the new cell, returns the new target 
move_to position
    var grid_pos = world_to_map(new_pos)

grid[grid_pos.x][grid_pos.y] = null

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

var target_pos = map_to_world(new_grid_pos) + half_tile_size
return target_pos

Grid Script

# Collection of functions to work with a Grid. Stores all its children in the grid array

extends TileMap

var count = 0

var players = []

enum ENTITY_TYPES {PLAYER, OBSTACLE, COLLECTIBLE}

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")
onready var Player = preload("res://Player.tscn")


func _ready():

for x in range(grid_size.x):
    grid.append([])
    for y in range(grid_size.y):
        grid[x].append(null)

# Player






# Obstacles
var positions = []
for x in range(5):
    var placed = false
    while not placed:
        var grid_pos = Vector2(randi() % int(grid_size.x), randi() % int(grid_size.y))
        if not grid[grid_pos.x][grid_pos.y]:
            if not grid_pos in positions:
                positions.append(grid_pos)
                placed = true

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


var new_player = Player.instance()
new_player.set_pos(map_to_world(Vector2(6,6)) + half_tile_size)
add_child(new_player)
new_player.player_number = 0
new_player.player_turn = false
players.append(new_player)

var new_player2 = Player.instance()
new_player2.set_pos(map_to_world(Vector2(6,5)) + half_tile_size)
add_child(new_player2)  
new_player2.player_number = 1
new_player.player_turn = true
players.append(new_player2)

func return_next_player(player_number):

  if player_number < players.size() -1: 
        players[player_number].player_turn = false
    player_number = player_number + 1
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    players[player_number].player_turn = true
    print ("player number is: ", player_number)

elif player_number == players.size() -1:
    players[player_number].player_turn = false
    player_number = 0
    players[player_number].player_turn = true
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    print ("player number is: ", player_number)

func get_cell_content(pos=Vector2()):
    return grid[pos.x][pos.y]


func is_cell_vacant(pos=Vector2(), direction=Vector2()):
     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 true if grid[grid_pos.x][grid_pos.y] == null else false
return false


func update_child_pos(new_pos, direction, type):
    # Remove node from current cell, add it to the new cell, returns the new target 
        # move_to position
var grid_pos = world_to_map(new_pos)

grid[grid_pos.x][grid_pos.y] = null

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

var target_pos = map_to_world(new_grid_pos) + half_tile_size
return target_pos

So that is my code above, I got the majority of it from here
I just tried to add in the ability to switch between different player nodes. Its this part here

func return_next_player(player_number):

if player_number < players.size() -1: 
    players[player_number].player_turn = false
    player_number = player_number + 1
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    players[player_number].player_turn = true
    print ("player number is: ", player_number)

elif player_number == players.size() -1:
    players[player_number].player_turn = false
    player_number = 0
    players[player_number].player_turn = true
    players[player_number].max_movement = 8
    players[player_number].distance_moved = 0
    print ("player number is: ", player_number)

there's an array that holds the player nodes, and idealy is supposed to switch out the boolean variable "player turn" on each call. It works as planned when called from here

        if move_distance > distance_to_target:
        distance_moved = distance_moved + 10
        velocity = target_direction * distance_to_target
        #print(velocity)
        is_moving = false
        if max_movement_quant <= 1:
            print ("Cant move anymore")
            grid.return_next_player(player_number)

basically what is happening is that the "players" are running out a "movement points" and the function to change players is called. I also want to option to just press a button and the player turns are changed. Such as in this code:

func _input(event):
if event.is_action_pressed("end_turn") and player_turn == true:
    grid.return_next_player(player_number)
    print("end turn")

The idea is that only player nodes with "player_turn" booleans set to true can access this. But when I run it, the first player acts as expected, but when the second player is set to true and you and try to pass control to the next player it skips over them and goies back to the second one.

in Engine by (19 points)

The weird thing is that it works in the first example(movement points) but not the second(button pressed)

I've been experimenting today and I tried this code

func return_next_player_end_turn(player_number):
if players[player_number].player_number == 0:
    print (count)
    print ("if ran")
    count = count + 1
elif players[player_number].player_number == 1:
    print (count)
    print ("elif ran")
    count = 0

called at the input section like so:

func _input(event):
if event.is_action_released("end_turn") and player_number == grid.count:
    #print (player_number)
    grid.return_next_player_end_turn(player_number)

I added a variable called count becouse I that that maybe both of them were set as "true" somehow in the previos post. So after hitting the input once I get this output

0
if ran

which is what I expect, but when I hit it again I get this

1
elif ran
0
if ran

I still cant understand why the if is being ran after the elif. Again, my goal is to just get the elif to run on the second press, then loop back to the if on the next etc.

1 Answer

0 votes
Best answer

So after what felt like forever working on this, I finally figured it out. I just moved the input function that was in the player scene over to the grid scene. I dont know why it didn't work excatly, my guess it's because there were always two player objects calling the same function. It also makes more sense for it to be in the Grid scene since it is the parent scene.

by (19 points)
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.