Increment "Lives" variable every 200 points

Godot Version

4.2.2

Question

Hello all,
I would appreciate your input on resolving this little pickle I’m in, detailed below.

The requirement is to increment the Player “Lives” variable by 1 for every 200 points the player accumulates.

The following is what I have implemented so far. (Only the coding parts relevant to the issue are mentioned here.)

Signal Manager (Autoload singleton) - Manages all the signals in the game

extends Node

signal add_one_life

Score Manager (Autoload singleton) - Manages updating score, highscore, etc.

extends Node

var current_score: int = 0

func update_score(points: int)-> void:
	current_score += points
	if high_score < current_score:
		high_score = current_score
	if current_score % 200 == 0:
		Signalmanager.add_one_life.emit()
		
	Signalmanager.current_score_update.emit()

Player script

extends CharacterBody2D

var current_score: int  #Starts at 0 on load.

func _ready():
	Signalmanager.add_one_life.connect(add_lives)

func add_lives():
	current_lives += 1
	Signalmanager.current_lives.emit(current_lives)

The above configuration works if the current score ends up as precisely multiples of 200. (Ex: 200, 400, etc.)

Ex:
If the current score is 50 and the player collects 150, the new score of 200 would add one life.

But if the current score is 100 and the player collects 150 points, the new score of 250 would NOT increment the life variable by 1, despite the current score passing the 200-point threshold.

Your help is much appreciated.
Thank you in advance!

You could use modulus along with the difference in score to detect crossing 200 points. If the new score is over 200 then it’s mod will be lower than the current score’s mod.

Take your example: current_score = 100, points = 150

thus new_points = 250

100 % 200 == 100
250 % 200 == 50

Since the new_points % 200 is less than the current_score % 200 it will trigger a single extra life.

func update_score(points: int)-> void:
	var new_score: int = points + current_score

	if high_score < new_score:
		high_score = new_score
	if current_score % 200 > new_score % 200:
		Signalmanager.add_one_life.emit()
		
	current_score = new_score
	Signalmanager.current_score_update.emit()
1 Like

This worked like a charm.
Thank you so much!

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