Trouble with connecting a signal from a node

Godot Version

4.2.2

Question

The problem is pretty straightforward - I need for my enemy node to get the direction signal from the projectile node that just hit him so I could make a kickback in the right direction (need an enemy to be pushed back by projectiles).

For that in the projectile node (that I call prAjectile cause it’s a letter A) I emit a signal:

var speed : int = 400
var doirection : int
signal got_your_ass(doirection)

func _physics_process(delta):
  move_local_x (doirection * speed * delta)

func _on_timer_timeout():
  queue_free()

func _on_hitbox_body_entered(body):
  emit_signal("got_your_ass", doirection)

and in the enemy node I try to catch it:

func _on_hurtbox_area_entered(area):
  $prAjectile.got_your_ass.connect(enemy_hurt)


func enemy_hurt(doirection):

  velocity.x = move_toward(velocity.x, 10*doirection, speed)

the results are captured on video:

(I can’t upload a video cause I’m a new user ffs, anyway it doesn’t work because Invalid get index ‘got_your_ass’ on base: ‘null instance’ in the enemy scrips when it tries to connect a signal)

Hello, @lexajzps! How did you get $prAjectile in a enemy node by code?

I didn’t lol, maybe that’s the issue, that’s the first ever instance of this $prAjectile in my code

I think, you can do without using signals in this case. In the function func _on_hitbox_body_entered(body): you can get enemy node with your class from body parameter and call he’s enemy_hurt(doirection): function.

Oh I did try that but it gave me the same error as with signals, I believe you can only call the child node’s attributes with this command, but my nodes are not related (well they exist in the same level but are not connected to each other)

So, in any case, you will need to get a projectile using the code. Use signals is not good way at this case. What nodes do you use as a hitbox of projectile and hurtbox_area of enemy?
Still, try to do without signals. Output body to the console (use print()) to see which node got into the hitbox and try to get the enemy out of it. I think body is another Rigidbody, so try to get he’s parent node with your enemy class.

блин чёт ничего не понял, понял что сигналы не канают, но как без сигнала передать хз, нода prAjectile у меня Animated Sprite 2d с хитбоксом

Скажи, пожалуйста, какие у тебя ноды стоят у хитбокса и хартбокса? Я имею ввиду, что при коллизии ты всегда можешь получить объект, который тригернул сигнал body_entered, например, у ноды Area. Только само тело передаваемое в параметр body у сигнала будет RigidBody или другой PhysicsBody. По этому, если скрипт enemy не привязан к ним, тебе нужно получить родительскую ноду, в которой он находится, чтобы получить доступ к функции enemy_hurt(doirection) и вызвать её.

Если вкратце как именно сделать (случайно отправил недописанное):

func _on_hitbox_body_entered(body):
    # Я так понимаю, скрипт противника у тебя в родительской ноде
    var enemy_node: EnemyClassName = body.get_parent()# Получаем родительскую ноду
    enemy_node.enemy_hurt(doirection) # Вызываем функцию из скрипта противника в ней

я разобрался в итоге, да, отказался от сигнала и такое вот сделал:

var knockback_strength = 300.0
var knockback_time = 0.1
var fist_vector: Vector2 = Vector2()

func _on_hurtbox_area_entered(area):
	if "hitbox" in area.name:
		fist_vector = global_position.direction_to(area.global_position)
		knockback = true
		
func fist_knockback(delta : float):
	if knockback == true:
		print("Ouwee")
		#velocity.x = lerp(0.0, -fist_vector.x * knockback_strength, 0.8) 
		velocity.x = -fist_vector.x * knockback_strength 
		move_and_slide()
		await get_tree().create_timer(knockback_time).timeout
		print("done")
		knockback = false
		velocity.x = 0.0

спасибо за подсказку!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.