0 votes

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 Engine by (197 points)
edited by

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

2 Answers

+1 vote
Best answer

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')
by (197 points)

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

0 votes

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?

by (3,394 points)

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)

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.

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

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.

Okay, how would I do that?

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

Did this didn't work?

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.}

Good to hear it works!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.