Skip to content
The Chat Archive
gpt-4o-mini-2024-07-18· OpenAI WildChat-4.8M (AI2)

@icon("res://addons/cogito/Assets/Graphics/Editor/IconCogitoObject.svg") extends Node3D…

  1. User

    @icon("res://addons/cogito/Assets/Graphics/Editor/Icon_CogitoObject.svg") extends Node3D class_name CogitoSecurityCamera

    signal started_detecting signal object_detected signal object_no_longer_detected signal turned_off

    @onready var audio_stream_player_3d: AudioStreamPlayer3D = $AudioStreamPlayer3D @onready var lightmeter:CogitoAttribute @onready var detected_light_level: float

    @export var detection_ray_cast_3d: RayCast3D @export var indicator_light: OmniLight3D @export var detection_area : Area3D @export var start_state : DetectorState

    @export_category("Detection Settings") @export var only_detect_player : bool = true ##Threshold at which Player is detected @export var detection_threshold : float = 4.0

    enum DetectionMethod{ TIME, ##Uses Time directly to until detection is trigger LIGHTMETER ##Uses Time * Lightmeter until detection is triggered. Scaled by light_level_detection_multipler. }

    ##How should the player be detected? @export var detection_method: DetectionMethod = DetectionMethod.TIME ##Determines impact player light level has on detection for Lightmeter method @export var light_level_detection_multipler : float = 0.01 ##Names of non player objects that should trigger detection @export var non_player_detection_list: Array[String] = []

    Sound played on detection triggered

    @export var alarm_sound : AudioStream

    @export_group("Detection indicator settings") @export var indicator_mesh : MeshInstance3D @export var color_offline : Color = Color.DARK_GRAY @export var color_searching : Color = Color.GREEN @export var color_detecting : Color = Color.YELLOW @export var color_detected : Color = Color.RED

    enum DetectorState{ SEARCHING, DETECTING, DETECTED, OFFLINE }

    var current_state : DetectorState var objects_in_detection_area : Array[Node3D] var detected_objects : Array[Node3D] var detection_time : float = 0

  2. Assistant

    Here's a continuation and potential implementation of your CogitoSecurityCamera class that logically follows the initialization and property definitions you provided. This implementation will include functions to handle detection logic, state management, and triggering signals for detected objects.

    func _ready():
        current_state = start_state
        objects_in_detection_area = []
        detected_objects = []
        detection_time = 0.0
        update_indicator_color()
    
    func _process(delta):
        update_detector_state(delta)
        if current_state == DetectorState.DETECTING:
            check_detection()
    
    func update_detector_state(delta: float) -> void:
        match current_state:
            DetectorState.SEARCHING:
                # If player is detected we move to detecting state
                if is_player_within_detection_area():
                    current_state = DetectorState.DETECTING
                    emit_signal("started_detecting")
            
            DetectorState.DETECTING:
                # Handle detection time and state transitions
                detection_time += delta
                if detection_time >= get_detection_time_threshold():
                    current_state = DetectorState.DETECTED
                    emit_signal("object_detected")
                    play_alarm_sound()
                elif not is_player_within_detection_area():
                    reset_detection()
    
            DetectorState.DETECTED:
                # If player leaves detection area, move to searching
                if not is_player_within_detection_area():
                    reset_detection()
    
    func check_detection() -> void:
        # Check for other objects for detection purposes
        for obj in detected_objects:
            if obj and (obj.name in non_player_detection_list):
                current_state = DetectorState.DETECTED
                emit_signal("object_detected")
                play_alarm_sound()
                
                # Stop checking if we have detected the object
                break
    
    func is_player_within_detection_area() -> bool:
        # This method determines if the player is within the detection area
        for body in detection_area.get_overlapping_bodies():
            if only_detect_player and body.is_in_group("Player"):
                return true
    
        return false
    
    fun reset_detection() -> void:
        detection_time = 0.0
        current_state = DetectorState.SEARCHING
        update_indicator_color()
        emit_signal("object_no_longer_detected")
        audio_stream_player_3d.stop()
    
    func update_indicator_color() -> void:
        match current_state:
            DetectorState.SEARCHING:
                indicator_mesh.material_override.albedo_color = color_searching
            DetectorState.DETECTING:
                indicator_mesh.material_override.albedo_color = color_detecting
            DetectorState.DETECTED:
                indicator_mesh.material_override.albedo_color = color_detected
            DetectorState.OFFLINE:
                indicator_mesh.material_override.albedo_color = color_offline
    
    func get_detection_time_threshold() -> float:
        match detection_method:
            DetectionMethod.TIME:
                return detection_threshold
            DetectionMethod.LIGHTMETER:
                if lightmeter:
                    return detection_threshold * (lightmeter.light_level * light_level_detection_multipler)
        return detection_threshold
    
    func play_alarm_sound() -> void:
        if alarm_sound:
            audio_stream_player_3d.stream = alarm_sound
            audio_stream_player_3d.play()
    

    Explanation:

    1. Detection States: The detection logic inherently moves between defined states (SEARCHING, DETECTING, DETECTED, OFFLINE) based on whether the player or certain non-player objects are detected in the defined area.

    2. Signal Emission: Signals are emitted to inform other parts of your game when an object starts detecting, is detected, and when no longer detected.

    3. Indicator Control: The visual indicator's color changes based on the state, providing feedback in the editor or during gameplay.

    4. Detection Check: The check_detection method checks if an object is in the detection area and, based on properties defined (like only_detect_player), updates state accordingly.

    5. Alarm Sound: Plays an audio cue when an object has been detected.

    You should integrate and extend it as per your game's specific requirements, but this structure gives you a robust starting point for your security camera logic.

  3. User

    Called when the node enters the scene tree for the first time.

    func _ready() -> void: detection_area.body_entered.connect(on_body_entered_detection) detection_area.body_exited.connect(on_body_left_detection)

    audio_stream_player_3d.stream = alarm_sound
    
    current_state = start_state
    update_indicator_mesh()
    

    func _physics_process(delta: float) -> void: match current_state: DetectorState.OFFLINE: return DetectorState.SEARCHING: searching() DetectorState.DETECTING: detecting(delta) DetectorState.DETECTED: return

    func searching(): if objects_in_detection_area.size() > 0: if find_visible_targets_within_detection_area().size() > 0: start_detecting()

    func start_detecting(): current_state = DetectorState.DETECTING started_detecting.emit() update_indicator_mesh()

    func detecting(delta: float): detected_objects = find_visible_targets_within_detection_area() if detected_objects.size() <= 0: CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "DETECTING: No visible targets in detection area. Stopping detection.") stop_detecting()

    if detection_method == DetectionMethod.LIGHTMETER and lightmeter != null: 
    	detected_light_level = lightmeter.value_current
    	detection_time += delta * (detected_light_level*light_level_detection_multipler)
    else:
    	detection_time += delta
    	
    if detection_time >= detection_threshold:
    	# === THIS IS WHERE THE FULL DETECTION HAPPENS ===
    	CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Detected!")
    	current_state = DetectorState.DETECTED
    	start_alarm_light()
    	if !audio_stream_player_3d.playing:
    		audio_stream_player_3d.play()
    	object_detected.emit()
    	update_indicator_mesh()
    
  4. Assistant

    The additional code you've provided further fleshes out the functionality of the CogitoSecurityCamera, specifically handling detection behaviors within the _ready, _physics_process, and detection methods. I’ll build upon that by integrating your existing logic and adding the remaining parts necessary for a complete implementation. Below, I've included structured explanations and additional functions that handle stopping detection, updating the indicator, and managing various states.

    Completed CogitoSecurityCamera Class

    extends Node3D
    @icon("res://addons/cogito/Assets/Graphics/Editor/Icon_CogitoObject.svg")
    class_name CogitoSecurityCamera
    
    signal started_detecting
    signal object_detected
    signal object_no_longer_detected
    signal turned_off
    
    @onready var audio_stream_player_3d: AudioStreamPlayer3D = $AudioStreamPlayer3D
    @onready var lightmeter: CogitoAttribute
    @onready var detected_light_level: float
    
    @export var detection_ray_cast_3d: RayCast3D
    @export var indicator_light: OmniLight3D
    @export var detection_area: Area3D 
    @export var start_state: DetectorState
    
    @export_category("Detection Settings")
    @export var only_detect_player: bool = true
    @export var detection_threshold: float = 4.0
    
    enum DetectionMethod {
        TIME, ## Uses time directly until detection is triggered
        LIGHTMETER ## Uses time * light meter until detection is triggered, scaled by light_level_detection_multiplier
    }
    
    @export var detection_method: DetectionMethod = DetectionMethod.TIME
    @export var light_level_detection_multipler: float = 0.01
    @export var non_player_detection_list: Array[String] = []
    @export var alarm_sound: AudioStream
    
    @export_group("Detection indicator settings")
    @export var indicator_mesh: MeshInstance3D
    @export var color_offline: Color = Color.DARK_GRAY
    @export var color_searching: Color = Color.GREEN
    @export var color_detecting: Color = Color.YELLOW
    @export var color_detected: Color = Color.RED
    
    enum DetectorState {
        SEARCHING,
        DETECTING,
        DETECTED,
        OFFLINE
    }
    
    var current_state: DetectorState
    var objects_in_detection_area: Array[Node3D]
    var detected_objects: Array[Node3D]
    var detection_time: float = 0.0
    
    # Called when the node enters the scene tree for the first time.
    func _ready() -> void:
        detection_area.body_entered.connect(on_body_entered_detection)
        detection_area.body_exited.connect(on_body_left_detection)
        
        audio_stream_player_3d.stream = alarm_sound
        
        current_state = start_state
        update_indicator_mesh()
    
    func _physics_process(delta: float) -> void:
        match current_state:
            DetectorState.OFFLINE:
                return
            DetectorState.SEARCHING:
                searching()
            DetectorState.DETECTING:
                detecting(delta)
            DetectorState.DETECTED:
                # This state is managed in detecting method
                return
    
    func searching() -> void:
        if objects_in_detection_area.size() > 0:
            if find_visible_targets_within_detection_area().size() > 0:
                start_detecting()
    
    func start_detecting() -> void:
        current_state = DetectorState.DETECTING
        started_detecting.emit()
        update_indicator_mesh()
    
    func detecting(delta: float) -> void:
        detected_objects = find_visible_targets_within_detection_area()
        if detected_objects.size() <= 0:
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "DETECTING: No visible targets in detection area. Stopping detection.")
            stop_detecting()
            return
        
        if detection_method == DetectionMethod.LIGHTMETER and lightmeter != null:
            detected_light_level = lightmeter.value_current
            detection_time += delta * (detected_light_level * light_level_detection_multipler)
        else:
            detection_time += delta
    
        if detection_time >= detection_threshold:
            # Detection successful
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Detected!")
            current_state = DetectorState.DETECTED
            start_alarm_light()
            if !audio_stream_player_3d.playing:
                audio_stream_player_3d.play()
            object_detected.emit()
            update_indicator_mesh()
    
    func stop_detecting() -> void:
        detection_time = 0.0
        current_state = DetectorState.SEARCHING
        update_indicator_mesh()
        emit_signal("object_no_longer_detected")
        audio_stream_player_3d.stop()
    
    func on_body_entered_detection(body: Node) -> void:
        # Add the body to the detection area
        if only_detect_player and body.is_in_group("Player":
            objects_in_detection_area.append(body)
        elif !only_detect_player and (body.is_in_group("Player") or body.name in non_player_detection_list):
            objects_in_detection_area.append(body)
    
    func on_body_left_detection(body: Node) -> void:
        # Remove the body from the detection area
        objects_in_detection_area.erase(body)
    
    func update_indicator_mesh() -> void:
        match current_state:
            DetectorState.SEARCHING:
                indicator_mesh.material_override.albedo_color = color_searching
            DetectorState.DETECTING:
                indicator_mesh.material_override.albedo_color = color_detecting
            DetectorState.DETECTED:
                indicator_mesh.material_override.albedo_color = color_detected
            DetectorState.OFFLINE:
                indicator_mesh.material_override.albedo_color = color_offline
    
    func find_visible_targets_within_detection_area() -> Array[Node3D]:
        var visible_targets: Array[Node3D] = []
        for body in detection_area.get_overlapping_bodies():
            # Alternatively, you can implement more thorough visibility checks.
            if body.is_in_group("Player") or body.name in non_player_detection_list:
                visible_targets.append(body)
        return visible_targets
    
    func start_alarm_light() -> void:
        # Placeholder for the indicator light's reaction to an alarm state
        indicator_light.light_color = Color(1, 0, 0)  # Red for detecting
    

    Explanation of the Functions:

    1. _ready(): Connects the signals for entering/existing the detection area and initializes the audio player and current state.

    2. _physics_process(delta: float): Checks the current state and dispatches the behavior for searching or detecting.

    3. searching(): Evaluates the detection conditions and transitions to the detecting state if targets are visible.

    4. start_detecting(): Transitions to the DETECTING state and emits the relevant signal.

    5. detecting(delta: float): Main logic that handles detection timing and state transitions, playing audio alarms when detection occurs.

    6. stop_detecting(): Resets detection state and emits a signal when no longer detecting.

    7. Detection Signals (on_body_entered_detection and on_body_left_detection): Handles entering/exiting bodies from the defined detection area, adding/removing them as necessary.

    8. update_indicator_mesh(): Adjusts the indicator’s visual feedback based on the camera's current state.

    9. find_visible_targets_within_detection_area(): Determines the visible targets currently within the detection area, based on group membership.

    10. start_alarm_light(): Placeholder to demonstrate how to trigger an alarm light when detection occurs.

    This class structure can be further refined or expanded based on additional game mechanics or performance optimizations you may want to implement.

  5. User

    func stop_detecting(): CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Stopping detection.") detection_time = 0 current_state = DetectorState.SEARCHING object_no_longer_detected.emit() update_indicator_mesh()

    func switch_to_searching(): detection_time = 0 current_state = DetectorState.SEARCHING update_indicator_mesh()

    func turn_off(): if audio_stream_player_3d.playing: audio_stream_player_3d.stop() current_state = DetectorState.OFFLINE turned_off.emit() update_indicator_mesh() indicator_light.light_energy = 0

    func find_visible_targets_within_detection_area() -> Array[Node3D]: var visible_targets : Array[Node3D] visible_targets.clear() for target in objects_in_detection_area: if object_visibile_for_detector(target): visible_targets.append(target)

    return visible_targets
    

    func object_visibile_for_detector(target: Node3D) -> bool: detection_ray_cast_3d.set_target_position(to_local(target.global_position)) CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Checking if detector can see " + target.name + " at position " + str(target.global_position) ) var detected_object = detection_ray_cast_3d.get_collider()

    if detected_object == target:
    	return true
    else:
    	return false
    

    func on_body_entered_detection(body: Node3D): if only_detect_player and body.is_in_group("Player"): CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Player entered detection area: " + body.name) objects_in_detection_area.append(body)

    # For non-player objects, check they are in the non_player_detection_list, and not a player
    elif !only_detect_player and !body.is_in_group("Player"):
    	if is_relevant_non_player(body):
    		CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Relevant object entered detection area: " + body.name)
    		objects_in_detection_area.append(body)
    if detection_method == DetectionMethod.LIGHTMETER:
    	find_lightmeter(body) 	# Check for Lightmeter on any entered body, if using Lightmeter detection method
    

    func find_lightmeter(body): var found_lightmeter: CogitoLightmeter = null for attribute in body.find_children("", "CogitoLightmeter", false): if attribute is CogitoLightmeter: found_lightmeter = attribute break

    if found_lightmeter:
    	lightmeter = found_lightmeter
    	CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "Lightmeter found on entity:" + body.name)
    else:
    	CogitoGlobals.debug_log(true,"cogito_security_camera.gd", "No lightmeter found on entity:"  + body.name)
    
  6. Assistant

    Your additional code segments enhance the functionality of the CogitoSecurityCamera class, particularly in the areas of stopping detection, turning the camera off, and checking for visibility of objects using raycasting. Below is a comprehensive view of the integration of this functionality, along with detailed explanations.

    Complete CogitoSecurityCamera Class with Enhanced Functionality

    Here's how your CogitoSecurityCamera implementation can look with the newly provided functions:

    extends Node3D
    @icon("res://addons/cogito/Assets/Graphics/Editor/Icon_CogitoObject.svg")
    class_name CogitoSecurityCamera
    
    signal started_detecting
    signal object_detected
    signal object_no_longer_detected
    signal turned_off
    
    @onready var audio_stream_player_3d: AudioStreamPlayer3D = $AudioStreamPlayer3D
    @onready var indicator_light: OmniLight3D
    @onready var detected_light_level: float
    @onready var detection_ray_cast_3d: RayCast3D = $DetectionRayCast3D
    
    @export var detection_area: Area3D 
    @export var start_state: DetectorState
    
    @export_category("Detection Settings")
    @export var only_detect_player: bool = true
    @export var detection_threshold: float = 4.0
    
    # Enum definitions
    enum DetectionMethod {
        TIME,
        LIGHTMETER
    }
    
    @export var detection_method: DetectionMethod = DetectionMethod.TIME
    @export var light_level_detection_multipler: float = 0.01
    @export var non_player_detection_list: Array[String] = []
    @export var alarm_sound: AudioStream
    
    @export_group("Detection indicator settings")
    @export var indicator_mesh: MeshInstance3D
    @export var color_offline: Color = Color.DARK_GRAY
    @export var color_searching: Color = Color.GREEN
    @export var color_detecting: Color = Color.YELLOW
    @export var color_detected: Color = Color.RED
    
    enum DetectorState {
        SEARCHING,
        DETECTING,
        DETECTED,
        OFFLINE
    }
    
    var current_state: DetectorState
    var objects_in_detection_area: Array[Node3D]
    var detected_objects: Array[Node3D]
    var detection_time: float = 0.0
    var lightmeter: CogitoAttribute
    
    func _ready() -> void:
        detection_area.body_entered.connect(on_body_entered_detection)
        detection_area.body_exited.connect(on_body_left_detection)
        
        audio_stream_player_3d.stream = alarm_sound
        current_state = start_state
        update_indicator_mesh()
    
    func _physics_process(delta: float) -> void:
        match current_state:
            DetectorState.OFFLINE:
                return
            DetectorState.SEARCHING:
                searching()
            DetectorState.DETECTING:
                detecting(delta)
            DetectorState.DETECTED:
                return
    
    func searching() -> void:
        if objects_in_detection_area.size() > 0:
            if find_visible_targets_within_detection_area().size() > 0:
                start_detecting()
    
    func start_detecting() -> void:
        current_state = DetectorState.DETECTING
        started_detecting.emit()
        update_indicator_mesh()
    
    func detecting(delta: float) -> void:
        detected_objects = find_visible_targets_within_detection_area()
        if detected_objects.size() <= 0:
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "DETECTING: No visible targets in detection area. Stopping detection.")
            stop_detecting()
            return
        
        if detection_method == DetectionMethod.LIGHTMETER and lightmeter != null:
            detected_light_level = lightmeter.value_current
            detection_time += delta * (detected_light_level * light_level_detection_multipler)
        else:
            detection_time += delta
    
        if detection_time >= detection_threshold:
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Detected!")
            current_state = DetectorState.DETECTED
            start_alarm_light()
            if not audio_stream_player_3d.playing:
                audio_stream_player_3d.play()
            object_detected.emit()
            update_indicator_mesh()
    
    func stop_detecting() -> void:
        CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Stopping detection.")
        detection_time = 0
        current_state = DetectorState.SEARCHING
        object_no_longer_detected.emit()
        update_indicator_mesh()
    
    func switch_to_searching() -> void:
        detection_time = 0
        current_state = DetectorState.SEARCHING
        update_indicator_mesh()
    
    func turn_off() -> void:
        if audio_stream_player_3d.playing:
            audio_stream_player_3d.stop()
        current_state = DetectorState.OFFLINE
        turned_off.emit()
        update_indicator_mesh()
        indicator_light.light_energy = 0
    
    func find_visible_targets_within_detection_area() -> Array[Node3D]:
        var visible_targets: Array[Node3D] = []
        for target in objects_in_detection_area:
            if object_visible_for_detector(target):
                visible_targets.append(target)
        return visible_targets
    
    func object_visible_for_detector(target: Node3D) -> bool:
        detection_ray_cast_3d.set_target_position(to_local(target.global_position))
        CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Checking if detector can see " + target.name + " at position " + str(target.global_position))
        var detected_object = detection_ray_cast_3d.get_collider()
    
        if detected_object == target:
            return true
        return false
    
    func on_body_entered_detection(body: Node3D) -> void:
        if only_detect_player and body.is_in_group("Player"):
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Player entered detection area: " + body.name)
            objects_in_detection_area.append(body)
        elif not only_detect_player and not body.is_in_group("Player"):
            if is_relevant_non_player(body):
                CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Relevant object entered detection area: " + body.name)
                objects_in_detection_area.append(body)
        if detection_method == DetectionMethod.LIGHTMETER:
            find_lightmeter(body)  # Check for Lightmeter on any entered body, if using Lightmeter detection method
    
    func find_lightmeter(body: Node) -> void:
        var found_lightmeter: CogitoLightmeter = null
        for attribute in body.find_children("", "CogitoLightmeter", false):
            if attribute is CogitoLightmeter:
                found_lightmeter = attribute
                break
    
        if found_lightmeter:
            lightmeter = found_lightmeter
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "Lightmeter found on entity: " + body.name)
        else:
            CogitoGlobals.debug_log(true, "cogito_security_camera.gd", "No lightmeter found on entity: " + body.name)
    
    func update_indicator_mesh() -> void:
        match current_state:
            DetectorState.SEARCHING:
                indicator_mesh.material_override.albedo_color = color_searching
            DetectorState.DETECTING:
                indicator_mesh.material_override.albedo_color = color_detecting
            DetectorState.DETECTED:
                indicator_mesh.material_override.albedo_color = color_detected
            DetectorState.OFFLINE:
                indicator_mesh.material_override.albedo_color = color_offline
    
    func start_alarm_light() -> void:
        # Change the indicator light to red or blinking
        indicator_light.light_color = Color(1, 0, 0)  # Red for detection.
    

    Key Functionalities Added:

    1. Stopping Detection: The stop_detecting() function stops the detection process and resets necessary parameters, such as detection_time. It also emits the object_no_longer_detected signal.

    2. Switch to Searching and Turn Off: The switch_to_searching() and turn_off() functions provide controls to gracefully handle state transitions and turn off the camera.

    3. Visibility Checks: find_visible_targets_within_detection_area() now checks for visibility based on raycasting, which determines if a detected object is truly visible.

    4. Object Visibility Check: The object_visible_for_detector() function uses a raycast to determine if the target object is visible to the detector.

    5. Signal Handling on Object Entry: The on_body_entered_detection() function manages entries into the detection area more thoroughly, including checking for light meters on relevant objects.

    6. Light Meter Handling: The find_lightmeter() function allows checking for the presence of light meters on relevant detected objects.

    Conclusion

    This implementation combines effective management of detection logic, customizable behavior based on game objects, and state management for a security camera system in Godot. With proper logging and state transitions, this system can form a solid basis for more complex interactions in your game environment. Adjustments can be made based on specific game requirements and performance considerations.