Godot Version
4.3
Question
Hi all,
I am having a problem with a 2D game I am making. The base game was part of a course where you make bare-bones games which I wanted to modify, so, the game could be reset or play over without resorting back to the main menu. The movement is also something that is really annoying but that is a bridge too far. The TLDR is that everything works with one enemy, everything seems to be functioning correctly the Reset and play buttons all work the shooting everything. However when the enemy is increased from 1 to 2 a problem immediately appears (there is supposed to be 3). If an enemy kills themselves or shot by the player - the player and the remaining enemy can no longer shoot - I can’t figure out why. I’ve gone through numerous permutations and tried to solve that simple problem but to no avail. There is an autoload signal used to destroy bullets because, of another problem I couldn’t overcome like if a bullet was headed toward the player and hit after the gameover had occurred it could say that the player had one and then change it to a loss - not something I wanted to occur - in any case after one enemy is down the bullet autoload seems to be operating on the player why, don’t know. So, I have a problem!
So, here is the rather long game_manager.gd script that is supposed to handle the game:
extends Node2D
var game_started = false;
var blue_tanks : int = 1;
var red_tanks : int = 2;
#var game_oni : bool = true;
@onready var start_screen: CanvasLayer = $StartScreen
var gameboard_redo:bool = false;
@onready var title: Label = $StartScreen/Title
@onready var map: Node2D = $Map
@onready var tile_map_layer: TileMapLayer = $Map/TileMapLayer
@onready var tile_map_layer_2: TileMapLayer = $Map/TileMapLayer2
#-------------------------------------------------------------------------------
#-- Inital locations
# hero
var player_initial_position:Vector2;
var player_initial_rotation: float;
#enemy
var opponent_1_initial_position:Vector2;
var opponent_1_initial_rotation: float;
var opponent_2_initial_position:Vector2;
var opponent_2_initial_rotation: float;
#-----------------------------------------------------------------------------
signal reset;
@onready var player: CharacterBody2D = $player
@onready var opponent_1: CharacterBody2D = $opponent_1
@onready var opponent_2: CharacterBody2D = $opponent_2
#@onready var opponent_3: CharacterBody2D = $opponent_3
var num = 1;
func _ready() -> void:
game_started = false;
start_screen.visible=true;
print("Blue Tanks: "+ str(blue_tanks) + " red tanks: " + str(red_tanks) )
reset.connect(make_reset); #no brackets required
#make tank visible now
player.player_visible = true;
#set the initial positions for all characters
#player
player_initial_position = player.position;
#enemy
opponent_1_initial_position = opponent_1.position;
opponent_2_initial_position = opponent_2.position;
if(gameboard_redo == false):
set_positions_for_later();
#end
#Update
func _process(_delta: float) -> void:
if(red_tanks <=0):
print("red tanks - dead");
game_over();
if(gameboard_redo == true):
redo_tank_positions();
if(player.player_destroyed): #Why?!?
player.visible = false;
if(opponent_1.enemy_destroyed):
opponent_1.visible = false;
Autoload.stop_bullets.emit();
if(opponent_2.enemy_destroyed):
opponent_2.visible = false;
Autoload.stop_bullets.emit();
#end func
func redo_tank_positions():
print("redo tank positions");
print("Redo pos x: " +str(player_initial_position.x) + " redo pos y: " +
str(player_initial_position.y));
player.position.x = player_initial_position.x;
player.position.y = player_initial_position.y
player.rotation = player_initial_rotation;
player.tank_visible();
opponent_1.position.x = opponent_1_initial_position.x;
opponent_1.position.y = opponent_1_initial_position.y
opponent_1.rotation = opponent_1_initial_rotation;
print("Redo oppenent 1 position: " + str(opponent_1_initial_position) );
opponent_1.tank_visible();
opponent_2.position.x = opponent_2_initial_position.x;
opponent_2.position.y = opponent_2_initial_position.y
opponent_2.rotation = opponent_2_initial_rotation;
print("Redo opponent 2 position: " + str(opponent_2_initial_position) );
opponent_2.tank_visible();
#stop the redo_tank_positions continually to that position
gameboard_redo = false;
#End
func set_positions_for_later():
#set for later
print("set for later");
player_initial_position = player.position;
player_initial_rotation = player.rotation;
opponent_1_initial_position = opponent_1.position;
print("Enemy_1 pos x: " +str(opponent_1_initial_position.x) +
" Enemy1 pos y: " +str(opponent_1_initial_position.y));
opponent_1_initial_rotation = opponent_1.rotation;
opponent_2_initial_position = opponent_2.position;
print("Enemy_2 pos x: " +str(opponent_2_initial_position.x) +
" Enemy2 pos y: " +str(opponent_2_initial_position.y));
opponent_2_initial_rotation = opponent_2.rotation;
#End
#Not used anymore
func make_reset():
print("This is reset time");
start_screen.visible = true;
game_started = true;
#end
#function called by player - when blue_tank is destroyed
#probably when the reset button is hit as well
func game_over()->void:
#game_oni= false;
game_started = false;
print("\nGame Over code....");
start_screen.visible=true;
#reset.emit();
if(blue_tanks <= 0 ):
title.text = "You Lose :(";
print("Red Wins");
#if player is destroyed stop shooting
#of enemies by saying they are destroyed
Autoload.stop_bullets.emit();
blue_tanks = 1;
gameboard_redo = true;
#end if
if(red_tanks <= 0 ):
print("\nYou Win!");
title.text = "You Win!";
Autoload.stop_bullets.emit();
red_tanks = 2;
gameboard_redo = true;
#end if
start_screen.visible=true;
print("\n");
#end func
func new_game():
print("\nNew game: ");
game_started = true;
#make player visible
#end
#-----------------------------------------------------------------
#-- buttons ----------------------------------------------------
func _on_reset_button_pressed() -> void:
print("\n............ Reset ATG ....................");
game_started = false;
start_screen.visible = true;
Autoload.stop_bullets.emit();
if(blue_tanks > 0 && red_tanks >0):
title.text = "It's a Draw!";
player.commence_fire();
opponent_1.commence_fire();
#end
func _on_play_button_pressed() -> void:
print("Reset - Play Button");
start_screen.visible = false;
game_started = true;
#game_oni = true;
#-- objects in scene -------------------------------
player.visible = true;
player.player_destroyed = false;
player.commence_fire();
if(opponent_1.enemy_destroyed):
opponent_1.visible = true;
opponent_1.enemy_destroyed = false;
if(opponent_2.enemy_destroyed):
opponent_2.visible = true;
opponent_2.enemy_destroyed = false;
#-----------------------------------------------------
#end
func _on_quit_button_pressed() -> void:
print("quit");
#code to drop back to menu
get_tree().reload_current_scene();
#end
If you can solve the problem here is a big thankyou in advance. The only thing I can offer in return is that if we ever meet IRL I will shout you to a cold frosty providing you are over 18.
Thankyou and Regards.