How to get trigger information from a specific part of the area signal function?

Godot Version

Godot 4.2

Question

I want to get information from part of the signal function being triggered after some logic gates and put it into a function for any outside scripts to use.

# Signal Function
func _on_collect_area_entered(area):
	if IsDashing():
		return
	
	if not area is Bullet and not area is Enemy:
		return
	
	if not area.is_in_group("EnemyBullet") and not area.is_in_group("Enemy"):
		return
	
	if _already_grazed(area):
		print("Already Grazed")
		return
	
	graze += 1
	grazedObjects.append(area)
	print("Grazed")
	# Trigger Information Here

# The function that I want to put the trigger information in
func is_grazed() -> bool:
	return false

I tried doing where I have a Boolean variable in the bottom of the signal function that turns true after passing through some logic gates, and added that said variable into the function that when used outside the script, check if the variable is true, then variable turns false and returns true, if not, returns false.

I felt that the solution I described above, felt like a band-aid solution because the function can only be used once at a time and I don’t think it would make for good code.

# Old Solution

var isGrazed : bool

func _on_collect_area_entered(area):
	if IsDashing():
		return
	
	if not area is Bullet and not area is Enemy:
		return
	
	if not area.is_in_group("EnemyBullet") and not area.is_in_group("Enemy"):
		return
	
	if _already_grazed(area):
		print("Already Grazed")
		return
	
	graze += 1
	grazedObjects.append(area)
	print("Grazed")
	isGrazed = true

func is_grazed() -> bool:
	if isGrazed:
		isGrazed = false
		return false
	return false

If anybody has a solution, please have a code example, thank you.

Could you provide more detail as to what behavior you’re trying to program? What object is this code from? What is actually happening here? etc. Also what does “graze” mean in this case - eating grass (cows), or skirting/touching an object?

How is _already_grazed() different from is_grazed()?

PS: don’t need two returns in that last method, this works just the same:

func is_grazed() -> bool:
	if isGrazed:
		isGrazed = false
	
	return isGrazed

PPS: Unrelated, but I would recommend against checking both class name (area is Bullet) and group (is_in_group(“EnemyBullet”)). Pick one and stick with it; otherwise it can lead to confusion/bugs down the road. Exception being is if you’re inheriting an old code base, of course, and just don’t wanna change anything there.

1 Like

Thank you for your feedback on the logic gates, I might change that soon, but just to explain a few things:

The signal and bool function is part of the player script and what I tried to do with the is_grazed() function is for other scripts to use such as the PlayerCustoms script because I want to make the player’s hud bop/bounce/pulse if the player grazes a projectile/bullet.

Graze in this context refers to when a object, such as projectiles, comes pretty close to the player via colliding with the player’s area2D node.

The _already_grazed() function checks for if the projectile already collided with the player’s Area2D Node.

var grazedObjects : Array[Node2D]

func _already_grazed(area : Area2D) -> bool:
	var grazeArray : Array[Node2D] = []
	
	for i in grazedObjects.size():
		if grazedObjects[i] != null:
			grazeArray.append(grazedObjects[i])
	
	grazedObjects = grazeArray
	
	for i in grazedObjects.size():
		if grazedObjects[i] == area:
			return true
	return false

I’m simply asking if there’s any alternatives to getting information from the signal function outside of the script?

PS, the example you gave me with the is_grazed() function does not make it work the same, what you basically doing is making the function always returns false instead of returning true when the variable is true and then turning the variable false, this old solution works fine as it is, but not what I’m looking for.

func is_grazed() -> bool:
	if isGrazed:
		isGrazed = false
		return true
	return false