Help making a distance tracker/ updater and saving high score

Godot Version

Godot V 4.3

Question

Im working on making a score system that involves distance, I think im on the right track but missing something. Im trying to get players position and store it in a variable, and every time the player travels "x" amount of distance to update and add score. Im also looking for a way to keep track of the high score even when game is reset/ restarted if possible? Here's what I have so far.

var distance: int = Vector2.AXIS_X
var High_score = 0 

#<-------- What needs worked on ------->#
func player_distance():
	var Ndistance
	distance = $Player.global_position.x
	if distance <= Ndistance + 500:
		print("distance: " + str(distance))
		return
	elif distance == Ndistance:
		Ndistance = distance
		distance_score()

func distance_score():
	score += 1
	update_score()
#<-------- What needs worked on ------->#

func update_score(): # Keeps track of score and displays it
	if score > High_score:
		High_score = score
	$tracker/Score.text = "Score:" + str(score)
	print(str(High_score))

Your player_distance function makes a variable Ndistance without a value, so it is null. You cannot make useful distance comparisons with null. I do not know what you intended to do with Ndistance so it’s hard to recommend any direct actions.

func player_distance():
	# Ndistance declared as NULL
	var Ndistance
	distance = $Player.global_position.x
	if distance <= Ndistance + 500: # comparing NULL + 500
		print("distance: " + str(distance))
		return
	elif distance == Ndistance: # distance will never equal NULL
		# Ndistance is set to a value, but will be deleted soon
		Ndistance = distance
		distance_score()

For saving data between games you will have to use a save file. In your case storing the high score as content.

oh forgot i was messing around with that a lot and kept it blank I did have the Ndistance set at 500. Thanks for the save file link I’ll read through that then. The main issue is this distance thing, am I on the right track with how I set up my player_distance function?

I have no idea what the player_distance function is supposed to do. Based on your Ndistance description as 500 it sounds like this is equivalent?

func player_distance():
    if $Player.global_position.x == 500 + 500:
        distance_score()

This would pretty much never work as floating point values are rarely ever exactly equal.

Do you want the player to score when they are over 1000 pixels?

if $Player.global_position.x > 1000:
    distance_score()

Start the scoring at 1000 pixel, and then every 500 pixels after that. So I know I need to set a variable to track the players position, and update it every 500. Something like that? This is how I originally called for the player function in my _physics_process. but I couldn’t figure out how to track it every 500 after that.

if $Player.global_position.x > 1000:
    player_distance()

Sounds like a short equation.

var bonus: int = floori(($Player.global_position.x - 1000) / 500)

floori removes decimal points, and converts to an int. Every 500 pixels after 1000 will result in 1 bonus

1 Like

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