I have created a script for a camera that has two states: CAM_CONTROLL
and CANT_CONTROLL
.
CAM_CONTROLL
allows the player to control the camera.- If the player clicks on one of the characters, like “Meh,” they lose control, and the camera begins to follow the selected entity, switching the state to
CANT_CONTROLL
.
Here’s the full script:
extends Camera2D
var direction_speed : float = 155
enum PLAYER_STATE {CAM_CONTROLL, CANT_CONTROLL}
var PLAYER_ = PLAYER_STATE.CAM_CONTROLL
var GetPositionMeh : Node2D
var CreateOnceOne : bool = true
var NewCam : Camera2D
@export var Canvas : CanvasLayer
@onready var GetAllNodesMeh : Array[Node2D] = [$"../../UggaUgga/Meh",
$"../../UggaUgga/Meh2",$"../../UggaUgga/Meh3",$"../../UggaUgga/Meh4",$"../../UggaUgga/Meh5"]
func _ready() -> void:
pass
func _process(delta):
match PLAYER_:
PLAYER_STATE.CAM_CONTROLL:
if NewCam == null and PLAYER_ == PLAYER_STATE.CANT_CONTROLL:
NewCam.queue_free()
if(Input.is_action_pressed("W")):
transform.origin += Vector2.UP * direction_speed * delta
pass
if(Input.is_action_pressed("S")):
transform.origin += Vector2.DOWN * direction_speed * delta
pass
if(Input.is_action_pressed("A")):
transform.origin += Vector2.LEFT * direction_speed * delta
pass
if(Input.is_action_pressed("D")):
transform.origin += Vector2.RIGHT * direction_speed * delta
pass
pass
PLAYER_STATE.CANT_CONTROLL:
_follow_meh()
_Create_Exit_button()
pass
pass
func _Create_Exit_button() -> Button:
var ExitButton : Button = Button.new()
ExitButton.position = Vector2(0,0)
ExitButton.text = "Exit View"
ExitButton.name = "Exit View"
Canvas.add_child(ExitButton)
ExitButton.pressed.connect(_Destroy_camera_and_add_new_camera)
return ExitButton
pass
func _follow_meh() -> void:
for i in GetAllNodesMeh.size():
if GetPositionMeh.name == GetAllNodesMeh[i].name:
NewCam = Camera2D.new()
NewCam.zoom = self.zoom
NewCam.offset = self.offset
NewCam.limit_bottom = self.limit_bottom
NewCam.limit_left = self.limit_left
NewCam.limit_right= self.limit_right
NewCam.limit_top = self.limit_top
NewCam.set_script("res://data/assets/scripts/Camera.gd")
NewCam.position_smoothing_enabled = true
if CreateOnceOne:
self.queue_free()
GetPositionMeh.add_child(NewCam)
NewCam.make_current()
CreateOnceOne = false
pass
pass
pass
func _Destroy_camera_and_add_new_camera() -> void:
print(NewCam)
if NewCam != null:
var CameraMain : Camera2D = Camera2D.new()
CameraMain.set_script("res://data/assets/scripts/Camera.gd")
PLAYER_ = PLAYER_STATE.CAM_CONTROLL
NewCam.queue_free()
pass
func _on_area_click_self_node_2d(Nod: Node2D) -> void:
PLAYER_ = PLAYER_STATE.CANT_CONTROLL
GetPositionMeh = Nod
pass
The problem arises when the “Exit View” button is created. When the player presses the button, nothing happens, even though I’ve connected the signal using ExitButton.pressed.connect(_Destroy_camera_and_add_new_camera)
.
Before this, I also tried creating a button node with the same name and behavior, adding the signal in the Camera2D
script, but it still didn’t work.
Here is an image of the main scene tree for reference:
Could you please explain why this is happening? As you can see, the “Play” button works, but the “Exit View” button does not. Why might this be?