How can I add what I want to this code block?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By System_Error

Hi all.

There’s something I want to do, but I don’t know how to do it. I have a function that counts how many kills the player gets in the game, and a function that counts, in seconds, how long the player survived. Currently, when the game ends, the survival time is displayed if it is greater than the current score.

func player_loss():
show_message("Game Over")
yield($MessageDuration, "timeout")
$Interactive/Button.show()
$SystemMessage.text = "New Game?"
$SystemMessage.show()
$Score.show()
if $Score.text < $TimeSurvived.text:
	$Score.text = $TimeSurvived.text

What I want to add is the kill count, multiplied by five, to the survival time before comparing the final score to the displayed score ($Score).

In short, my goal is to have Time Survived + (Kill Count * 5) be compared to the Score.

System_Error | 2019-01-08 03:38

:bust_in_silhouette: Reply From: p7f

Hi, I don’t know if i understood the question, but you want to compare to Time Survived + (Kill Count * 5) instead of comparing to TimeSurvived ?

First of all, the way you are comparing text is not quite ok in my understanding. Because comparing strings will compare them alphabetically instead of numerically and (i asume) you need to compare numbers.

So, first get the numbers (idk if they are int or float in your case, but its similar. Also i asume you have already kill count number stored as an integer in var killcount or something like that).

var score = int($Score.text)
var time = int($TimeSurvived.text)

then you get the number you want:

var num = time + killcount*5

Then you compare:

if score < num:
    score = num
    $Score.text = str(score)

Is that what you wanted?

Yes, this is what I was looking for. Thank you.

And, could you help me make the code better?

extends CanvasLayer

signal play_game

var score = int($Score.text)
var time = int($TimeSurvived.text)
var num = time + kill_count*5

func show_message(text):
	$SystemMessage.text = text
	$SystemMessage.show()
	$MessageDuration.start()

func player_loss():
	show_message("Game Over")
	yield($MessageDuration, "timeout")
	$Interactive/Button.show()
	$SystemMessage.text = "New Game?"
	$SystemMessage.show()
	$Score.show()
	if score < num:
		score = num
		$Score.text = str(score)


func score_count(score):
	$TimeSurvived.text = str(score)

func _on_MessageDuration_timeout():
	$SystemMessage.hide()


func _on_Button_pressed():
	$Interactive/Button.hide()
	$TimeSurvived.text = str("0")
	$Score.hide()
	emit_signal('play_game')

func kill_count(count):
	$Kills.text = str(count)

System_Error | 2019-01-08 20:53

You are welcome!
What do you mean with “make the code better”? It looks ok to me…
Also, i think you should update num when you call player_loss, or in both functions where you update time and kills.

p7f | 2019-01-08 21:35

When I run my code, the debugger is complaining that an identifier isn’t found (kill_count).

System_Error | 2019-01-08 21:55

Because you dont have a variable called kill_count defined in your code. In the lin that you have this:

var num = time + kill_count*5

Change kill_count by int($Kills.text). I still think you should update num inside player_loss before the comparation also.

p7f | 2019-01-08 22:59

Okay, how would I do that?

System_Error | 2019-01-08 23:52

try with this and tell me if it does not work:

extends CanvasLayer

signal play_game

var score = int($Score.text)
var time = int($TimeSurvived.text)
var kills= int($Kills.text)
var num = time + kills*5

func show_message(text):
    $SystemMessage.text = text
    $SystemMessage.show()
    $MessageDuration.start()

func player_loss():
    show_message("Game Over")
    yield($MessageDuration, "timeout")
    $Interactive/Button.show()
    $SystemMessage.text = "New Game?"
    $SystemMessage.show()
    $Score.show()
    num = time + kills*5
    if score < num:
        score = num
        $Score.text = str(score)


func score_count(score):
    $TimeSurvived.text = str(score)
    time = score

func _on_MessageDuration_timeout():
    $SystemMessage.hide()


func _on_Button_pressed():
    $Interactive/Button.hide()
    $TimeSurvived.text = str("0")
    $Score.hide()
    emit_signal('play_game')

func kill_count(count):
    $Kills.text = str(count)
    kills = count

p7f | 2019-01-09 00:34

Did this didn’t work?

p7f | 2019-01-09 11:25

Well, it was a bit messy. A friend helped get it done, though, and in a slightly cleaner way. We moved the majority of the stuff into Level.gd, so the HUD isn’t doing the calculations.

{HUDs are for showing data, and aren’t meant to do anything else.}

System_Error | 2019-01-10 03:57

Good to hear it works!

p7f | 2019-01-10 10:02

:bust_in_silhouette: Reply From: System_Error

Figured I’d put this here so people don’t think I’m still looking for an answer.

So I cleaned up my HUD.gd, moving the calculations over to Level.gd.

What I added to Level.gd:

...
var timescore
var kills
var finalscore
var highscore = 0.0
...
func _on_BaseArea_area_entered(area):
...
finalscore = timescore + kills*5
if highscore < finalscore:
	highscore = finalscore
$HUD.player_loss(finalscore, highscore)
...

And here’s how HUD.gd looks after cleanup;

extends CanvasLayer

signal play_game

func show_message(text):
	$SystemMessage.text = text
	$SystemMessage.show()
	$MessageDuration.start()

func _on_MessageDuration_timeout():
	$SystemMessage.hide()

func player_loss(finalscore, highscore):
	show_message("Game Over")
	yield($MessageDuration, "timeout")
	$Interactive/PlayButton.show()
	$SystemMessage.text = "Play Again?"
	$SystemMessage.show()
	$HighScore.show()
	$CurrentScore.show()
	$HighScore.text = str(highscore)
	$CurrentScore.text = str(finalscore)
	$Interactive/HelpButton.show()

func score_count(score):
	$TimeSurvived.text = str(score)

func kill_count(count):
	$Kills.text = str(count)

func _on_Button_pressed():
	$Interactive/PlayButton.hide()
	$Interactive/HelpButton.hide()
	$CurrentScore.hide()
	$TimeSurvived.text = str("0")
	$Kills.text = str("0")
	emit_signal('play_game')

+1 for posting the answer and not leaving the question oppened forever!

p7f | 2019-01-10 10:04