My game is 2D. I need a kill counter (I already have a death counter that works) for how many times one player has killed the other. Problem is, the two player nodes can't target each other, so I can't say...

is_dead
$Player.killCounter += 1
#Just an example

So, I tried doing it via the global level script.

	if $Player/AnimatedSprite.frames == $Player.PlayerDead:
		$Player_2.killCounter += 1
		print($Player.killCounter)
		$Player_2/Deaths/Kills.text = str("Kills:", $Player_2.killCounter)
	if $Player_2/AnimatedSprite2.frames == $Player_2.PlayerDead2:
		$Player.killCounter += 1
		print($Player.killCounter)
		$Player/Deaths/Kills.text = str("Kills:", $Player.killCounter)

That didn't work, because the kill counter ended up being 60, which is weird because my death timer is set to 1 minute (I think its a minute and there's 60 seconds in a minute)

This is my code for dying. I doesn't say anywhere in here that the kill counter is related to the death timer.


func dead():
	velocity = Vector2(0,0)
	$DeathTimer.start()
	is_dead = true

func respawn():
	$AnimatedSprite.hide()
	position = $Position2D.position
	$AnimatedSprite.frames = PlayerHappy
	$AnimatedSprite.show()
	is_dead = false
	exitCounter -= 6

func _on_DeathTimer_timeout():
	respawn()

I really need help with this.

Maybe you could have the player's call the global script to increase the kill counter? Something like:

# in the global script
func increase_kill_counter(player_index):
	if player_index == 0:
		$Player.killCounter += 1
	elif player_index == 1:
		$Player2.killCounter += 1

# then in the player script
is_dead = true
global.increase_kill_counter(other_player_index)

The hard part though is I guess you would still need to know which player killed the other player, so you could get it's index. Maybe in the script that triggers the player dying you could call something like increase_kill_counter?

@TwistedTwigleg said: Maybe you could have the player's call the global script to increase the kill counter? Something like:

# in the global script
func increase_kill_counter(player_index):
	if player_index == 0:
		$Player.killCounter += 1
	elif player_index == 1:
		$Player2.killCounter += 1

# then in the player script
is_dead = true
global.increase_kill_counter(other_player_index)

The hard part though is I guess you would still need to know which player killed the other player, so you could get it's index. Maybe in the script that triggers the player dying you could call something like increase_kill_counter?

I might need you to explain how you got a global script to work? Do you preload it in the project settings? I think I might've used "global" wrongly. I meant the script that is in the level that the players are in. It would work except if I preload the global script, it doesn't see the players...

If I try this

func increase_kill_counter(player_index):
	if player_index == 0:
		$Player.killCounter += 1
		$Player/Deaths/Kills.text = str("Kills: ", $Player.killCounter)
	 
	elif player_index == 1:
		$Player_2.killCounter += 1
		$Player_2/Deaths/Kills.text = str("Kills: ", $Player_2.killCounter)

func respawn():
	is_dead = true
	global.increase_kill_counter(1)
	velocity = Vector2(0,0)
	yield(get_tree().create_timer(1), "timeout")
	$AnimatedSprite.hide()
	position = $Position2D.position
	$DeathTimer.start()

Whenever I try to run this death script it says:

E 0:00:14.794 get_node: Node not found: Player. <C++ Error> Condition "!node" is true. Returned: __null <C++ Source> scene/main/node.cpp:1381 @ get_node() <Stack Trace> Level1Global.gd:5 @ increase_kill_counter() Player_2.gd:148 @ respawn() Player_2.gd:132 @ _on_Player2Head_area_entered()

@AesynthGrey said:

@TwistedTwigleg said: Maybe you could have the player's call the global script to increase the kill counter? Something like:

# in the global script
func increase_kill_counter(player_index):
	if player_index == 0:
		$Player.killCounter += 1
	elif player_index == 1:
		$Player2.killCounter += 1

# then in the player script
is_dead = true
global.increase_kill_counter(other_player_index)

The hard part though is I guess you would still need to know which player killed the other player, so you could get it's index. Maybe in the script that triggers the player dying you could call something like increase_kill_counter?

I might need you to explain how you got a global script to work? Do you preload it in the project settings? I think I might've used "global" wrongly. I meant the script that is in the level that the players are in. It would work except if I preload the global script, it doesn't see the players...

I was thinking a Singleton/Autoload is what you meant with global, so that's why I used it that way. However, fi It's just a script in the level and preloaded, then you'd need to get a reference to it to use it.

One thing you could try though is when you spawn the player, assigning them to a group like player_1 and player_2. Then you could use get_tree().get_nodes_in_group("player_1") to get the player and then update the kill counter that way.

# in the _ready function of the player
func _ready():
	add_to_group("player_1")

# in the global script:
func increase_kill_counter(player_index):
	if player_index == 0:
		var player_1_nodes = get_tree().get_nodes_in_group("player_1")
		if (player_1_nodes.size() > 0):
			player_1_nodes[0].killCounter += 1
			player_1_nodes[0].get_node("Deaths/Kills").text = str("Kills: ", player_1_nodes[0].killCounter);
	elif player_index == 1:
		var player_2_nodes = get_tree().get_nodes_in_group("player_2")
		if (player_2_nodes.size() > 0):
			player_2_nodes[0].killCounter += 1
			player_2_nodes[0].get_node("Deaths/Kills").text = str("Kills: ", player_2_nodes[0].killCounter);

That should, I think, work as long as the player's add themselves to the groups before you call increase_kill_counter.

a year later