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?
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()