最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

2d - Why I cannot handle my signal inside my child class while In the parent I can? - Stack Overflow

programmeradmin0浏览0评论

I have this class which is responsable to send me a signal each time my character detect's a parry:

class_name CharacterAttack

signal attack_parried_detected

func _init(body: CharacterBody2D):
    character = body
    hitbox = body.get_node("HitBox");
    hitbox.connect("body_entered", Callable(self, "_on_hit_detected"))
    hitbox.connect("body_exited", Callable(self, "_on_body_outs_hitbox"))
    self.collisionArea = hitbox.get_node("Area")
    #timer
    self.timer = Timer.new()
    self.timer.one_shot = true
    self.timer.wait_time = attack_duration
    self.character.add_child(timer)
    self.timer.connect("timeout", Callable(self, "_commit_attack"))

func _commit_attack():
    for chr in characters_in_attack_area:
        if chr.state.current_state != CharacterState.States.PARRYING:
            chr.receive_damage.receive(character.attributes.attack_damage)
        else:
            attack_parried_detected.emit()
    characters_in_attack_area.clear()
    collisionArea.set_deferred("disabled", true)
    character.state.change_state(CharacterState.States.IDLE)

It's a property of my "BaseCharacter" class

# Player.gd
extends CharacterBody2D

class_name BaseCharacter

#core
var state: CharacterState
var animator: CharacterAnimator
var movement: CharacterMovement
var attributes: CharacterAttributes

#att
var _jump_force
var _speed
var _attack_damage
var _health

#Actions
var attack: CharacterAttack
var receive_damage: CharacterReceiveDamage
var die: CharacterDie
var parry: CharacterParry

func _init(jump_force, speed, attack_damage, health):
    self._jump_force = jump_force
    self._speed = speed
    self._attack_damage = attack_damage
    self._health = health

func _ready():
    #core
    state = CharacterState.new(self)
    animator = CharacterAnimator.new(self)
    attributes = CharacterAttributes.new(self, _speed, _jump_force, _attack_damage, _health)
    movement = CharacterMovement.new(self)
    
    #actions
    receive_damage = CharacterReceiveDamage.new(self)
    attack = CharacterAttack.new(self)
    die = CharacterDie.new(self)
    parry = CharacterParry.new(self)
    self.attack.attack_parried_detected.connect(_handle_parry_attack)
    
func _handle_parry_attack():
    print("ok")

and here I can handle the signal properly... but in my class that extends BaseCharacter class down here, the log "ok1" is never printed, even that in the parent class "ok" is aways printed when a parry is detected

extends BaseCharacter

class_name Player

var jump_force = -250
var speed = 200
var attack_damage = 10
var health = 1000

@onready var camera: Camera2D = $Camera
var playerCameraShaker: PlayerCameraShaker
var self_attack: CharacterAttack

func _init():
    super(jump_force, speed, attack_damage, health)

func _handle_parry_attack1():
    print("ok 1")
    
func _ready():
    super._ready()
    self.attack.attack_parried_detected.connect(_handle_parry_attack1)
    self.playerCameraShaker = PlayerCameraShaker.new(self, camera)
    
func _physics_process(delta: float) -> void:
    var direction := Input.get_axis("move_left", "move_right")
    
    movement.add_gravity(delta);
    movement.move_x(direction)
    
    if Input.is_action_just_pressed("jump") and self.is_on_floor():
        movement.jump(delta)

func _process(delta):
    if Input.is_action_just_pressed("attack"):
        attack.attack()
    if Input.is_action_just_pressed("parry"):
        parry.parry()

what am i doing wrong ?

I have this class which is responsable to send me a signal each time my character detect's a parry:

class_name CharacterAttack

signal attack_parried_detected

func _init(body: CharacterBody2D):
    character = body
    hitbox = body.get_node("HitBox");
    hitbox.connect("body_entered", Callable(self, "_on_hit_detected"))
    hitbox.connect("body_exited", Callable(self, "_on_body_outs_hitbox"))
    self.collisionArea = hitbox.get_node("Area")
    #timer
    self.timer = Timer.new()
    self.timer.one_shot = true
    self.timer.wait_time = attack_duration
    self.character.add_child(timer)
    self.timer.connect("timeout", Callable(self, "_commit_attack"))

func _commit_attack():
    for chr in characters_in_attack_area:
        if chr.state.current_state != CharacterState.States.PARRYING:
            chr.receive_damage.receive(character.attributes.attack_damage)
        else:
            attack_parried_detected.emit()
    characters_in_attack_area.clear()
    collisionArea.set_deferred("disabled", true)
    character.state.change_state(CharacterState.States.IDLE)

It's a property of my "BaseCharacter" class

# Player.gd
extends CharacterBody2D

class_name BaseCharacter

#core
var state: CharacterState
var animator: CharacterAnimator
var movement: CharacterMovement
var attributes: CharacterAttributes

#att
var _jump_force
var _speed
var _attack_damage
var _health

#Actions
var attack: CharacterAttack
var receive_damage: CharacterReceiveDamage
var die: CharacterDie
var parry: CharacterParry

func _init(jump_force, speed, attack_damage, health):
    self._jump_force = jump_force
    self._speed = speed
    self._attack_damage = attack_damage
    self._health = health

func _ready():
    #core
    state = CharacterState.new(self)
    animator = CharacterAnimator.new(self)
    attributes = CharacterAttributes.new(self, _speed, _jump_force, _attack_damage, _health)
    movement = CharacterMovement.new(self)
    
    #actions
    receive_damage = CharacterReceiveDamage.new(self)
    attack = CharacterAttack.new(self)
    die = CharacterDie.new(self)
    parry = CharacterParry.new(self)
    self.attack.attack_parried_detected.connect(_handle_parry_attack)
    
func _handle_parry_attack():
    print("ok")

and here I can handle the signal properly... but in my class that extends BaseCharacter class down here, the log "ok1" is never printed, even that in the parent class "ok" is aways printed when a parry is detected

extends BaseCharacter

class_name Player

var jump_force = -250
var speed = 200
var attack_damage = 10
var health = 1000

@onready var camera: Camera2D = $Camera
var playerCameraShaker: PlayerCameraShaker
var self_attack: CharacterAttack

func _init():
    super(jump_force, speed, attack_damage, health)

func _handle_parry_attack1():
    print("ok 1")
    
func _ready():
    super._ready()
    self.attack.attack_parried_detected.connect(_handle_parry_attack1)
    self.playerCameraShaker = PlayerCameraShaker.new(self, camera)
    
func _physics_process(delta: float) -> void:
    var direction := Input.get_axis("move_left", "move_right")
    
    movement.add_gravity(delta);
    movement.move_x(direction)
    
    if Input.is_action_just_pressed("jump") and self.is_on_floor():
        movement.jump(delta)

func _process(delta):
    if Input.is_action_just_pressed("attack"):
        attack.attack()
    if Input.is_action_just_pressed("parry"):
        parry.parry()

what am i doing wrong ?

Share Improve this question asked Mar 15 at 11:01 Hiran JúniorHiran Júnior 3653 silver badges11 bronze badges 10
  • As a starting point, I'd check the signal connections. Use print(attack.attack_parried_detected.get_connections()) after Signal.connect() in both the BaseCharacter and Player classes. What's the output? (I expect the inheriting class to print one more item than its parent.) – liggiio Commented Mar 15 at 12:48
  • Here's my output BaseCharacter: BaseCharacter[{ "signal": Node(attack.gd)::[signal]attack_parried_detected, "callable": CharacterBody2D(Player)::_handle_parry_attack, "flags": 0 }] Player: Player[{ "signal": Node(attack.gd)::[signal]attack_parried_detected, "callable": CharacterBody2D(Player)::_handle_parry_attack, "flags": 0 }, { "signal": Node(attack.gd)::[signal]attack_parried_detected, "callable": CharacterBody2D(Player)::_handle_parry_attack1, "flags": 0 }] – Hiran Júnior Commented Mar 15 at 13:01
  • I could format this properly, but I got an array with 1 connection in my basecharacter class and an array with 2 connections in my player class ... but I keep receiving only "ok" print when my character parries an enemy attack – Hiran Júnior Commented Mar 15 at 13:02
  • here is the full code if you would like to see it: github/maximosdrr/samurai_edge – Hiran Júnior Commented Mar 15 at 13:04
  • 1 I just find out my problem here, it was between my monitor and my chair ... The problem here is that the parry action only happens when I'm attacking, but I have an action for my parry, the parry signal should be emited for the body which is about to receive damage, not for the one who is about to deal the damage :/ , I'll share the solution down bellow – Hiran Júnior Commented Mar 15 at 14:21
 |  Show 5 more comments

1 Answer 1

Reset to default 0

I've found the problem, it was on my code itself, I was calling "take_damage" into my object which is acctually attacking instead of in one of is elegible to take damage

发布评论

评论列表(0)

  1. 暂无评论