# If-else statement not working as intended

Attention Topic was automatically imported from the old Question2Answer platform.
Old Version Published before Godot 3 was released.

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 = []

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()

var new_player = Player.instance()
new_player.set_pos(map_to_world(Vector2(6,6)) + half_tile_size)
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)
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 = []

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()

var new_player = Player.instance()
new_player.set_pos(map_to_world(Vector2(6,6)) + half_tile_size)
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)
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.

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

cooldrcool | 2017-08-17 02:57

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.

cooldrcool | 2017-08-18 02:32