Points dont add up to other points/another type of points add up to other points but revert when player collects something that has another type of points

Godot Version

4.1

Question

hi, im having an issue with the score system in my game
the problem is that when i first play the level, when i collect something that has another type of points, the type of points replace the other type of points (like when i collect something that has a different type of points, it replaces with 1000 instead of adding up with the other type of points)

however, in the 2nd time (when the player dies), the same type of points and another one add up but when i collect something that has another type, it replaces the other types (for example: if its 500, the other points type replaces it with 200 when i collect something that follows that type of points)

so is there a way to solve this? here are the scripts incase your interested

the 1st (and 2nd) type of points:

extends Node

class_name PelletsManager

var total_pellets_count
var pellets_eaten = 0



@onready var chomp_1_player = $"../soundPlayers/chomp1_player"
@onready var chomp_2_player = $"../soundPlayers/chomp2_player"
@onready var points_manager = $"../pointsManager"
@onready var ui = $"../UI" as UI

const BASE_POINTS_FOR_PELLET_VALUE = 10
const BASE_POINTS_FOR_POWERPELLET_VALUE = 50
var pellet_points = BASE_POINTS_FOR_PELLET_VALUE
var powerpellet_points = BASE_POINTS_FOR_POWERPELLET_VALUE
var points = 0


@onready var powerpellet_player = $"../soundPlayers/powerpellet_player"

@export var ghost_array: Array[Ghost]

var eaten_ghost_counter = 0

func _ready():
	var pellets = self.get_children() as Array[Pellet]
	total_pellets_count = pellets.size()
	for pellet in pellets:
		pellet.pellet_eaten.connect(on_pellet_eaten)
	
	for ghost in ghost_array:
		ghost.run_away_timeout.connect(on_ghost_run_away_timeout)

func on_pellet_eaten(should_allow_eating_ghosts: bool):
	print("wakawaka")
	points += pellet_points
	if !chomp_1_player.playing:
		chomp_1_player.play()
	else:
		chomp_2_player.play()
	ui.set_score(points)
	if should_allow_eating_ghosts:
		powerpellet_player.play()
		points += powerpellet_points
		ui.set_score(points)
		for ghost in ghost_array:
			if ghost.current_state != ghost.GhostState.STARTING_AT_HOME:
				ghost.run_away_from_pacman()

	
	if pellets_eaten == total_pellets_count:
		get_tree().change_scene_to_file("res://SCENES/gameover.tscn")

# The 2nd type of points
func on_ghost_run_away_timeout():
	eaten_ghost_counter += 1
	if eaten_ghost_counter == ghost_array.size():
		points_manager.reset_points_for_ghosts()
		eaten_ghost_counter = 0
		powerpellet_player.stop()

the 3rd type of points:

extends Node

class_name PointsManager

@onready var ui = $"../UI" as UI


const BASE_POINTS_FOR_GHOST_VALUE = 200
const BASE_POINTS_FOR_PELLET_VALUE = 10
const BASE_POINTS_FOR_POWERPELLET_VALUE = 50

var ghost_points = BASE_POINTS_FOR_GHOST_VALUE
var pellet_points = BASE_POINTS_FOR_PELLET_VALUE
var powerpellet_points = BASE_POINTS_FOR_POWERPELLET_VALUE
var points = 0


func pause_ghost():
	points += ghost_points
	get_tree().paused = true
	await get_tree().create_timer(1.13).timeout
	get_tree().paused = false
	ghost_points += BASE_POINTS_FOR_GHOST_VALUE
	ui.set_score(points)

func reset_points_for_ghosts():
	ghost_points = BASE_POINTS_FOR_GHOST_VALUE

Seems like you are using two different points values and setting UI to either of them. One points value only tracks ghost_points, the other tracks pellets. So your UI show the total ghosts if you ate a ghost last, or the total pellets if you ate a pellet.

Keep in mind just because these two scripts have a variable named points does not make it the same variable, they are not communicating between each other.

oh
so is there a way to make them track both and add them up when i collect either the ghost points or pellet points?

oh
so how do i fix that? like make it so it adds up these types of points

Since they both access “UI” you should have “UI” in charge of points tracking.

how exactly tho?

Move your code from these two classes and merge it into the UI class.

ill try to do that

i tried to put the point types into “UI” but it didnt work

what didn’t work? You would have to also explain what you changed as I cannot know what you did.

i cant collect the points when i collect something that has the said points, it also gives me an error on another script when i collect another thing that has another type of points

btw what i changed is i putted this:

func pause_ghost():
	points += ghost_points
	get_tree().paused = true
	await get_tree().create_timer(1.13).timeout
	get_tree().paused = false
	ghost_points += BASE_POINTS_FOR_GHOST_VALUE
	ui.set_score(points)

and this:

func pellet_points_eaten():
	points += pellet_points
	pellet_points += BASE_POINTS_FOR_PELLET_VALUE
	ui.set_score(points)

And these functions are part of UI? it seems strange for them to use ui.set_score instead of set_score. What is ui inside of the UI script?

So you collect something and get an error, what is the error?

ui is just a user interface/canvas as the class “UI”
btw i forgot what the error was but i do remember it was in the eaten ghosts points type

waiting for a reply

Will need the error to help; not much I can do without that information otherwise.

ima try to do the error again

the error probably is “Invalid call. Nonexistent function ‘pause_ghost’ in base ‘Node (PointsManager)’.”

i removed the function pause_ghost from another script and putted it in the UI script

That would make sense, you need to call your function on UI since it has moved from PointsManager to UI.

alright