My game adds 16 points after finish line and I don't know why

Godot 4

Hi! I am making a game to gain expierence for my actual dream project. I am aming for some sort of mobile game (but I develop it with mouse control since I don’t plan to publish it anyway). Here is the issue: When I cross the finish line then my character gets aditional 16 points and I don’t know why!!

Additionally, if you have any coding pro tips let me know!

here is code:

Main code of the scene:

extends Node2D

var rng = RandomNumberGenerator.new()

var distance0 = 0
var distance1 = 0
var distance2 = 0


@onready var leaderboard = $CharacterMain/Camera2D/UI/Leader
@onready var pointsboard = $CharacterMain/Camera2D/UI/Points

@onready var uifinish = $CharacterMain/Camera2D/UI/Info/UiFinish
@onready var uistart = $CharacterMain/Camera2D/UI/Info/UiStart

@onready var item1 =$CharacterMain/Camera2D/ITEMS/Items1
@onready var item2 = $CharacterMain/Camera2D/ITEMS/Items2


@onready var finishline = $UiFinishline


signal gamestop


func _ready() -> void:
	#finishline
	finishline.position.x = Global.RaceLenght + 100
	#items
	_itemsgenerator()
	#UI
	uifinish.position = Vector2(75,850)
	uistart.position = Vector2(75,850)
	uifinish.hide()
	uistart.hide()
	leaderboard.text = str(Global.leader)
	pointsboard.text = str(0)


func _process(delta: float) -> void:
	_distance()
	leaderboard.text = str(Global.leader)
	pointsboard.text = str(Global.points)


	
func _distance():
	distance0 = Global.GB_distance
	distance1 = Global.A_distance
	if distance0 > distance1:
		Global.leader = "GB"
	elif distance1 > distance0:
		Global.leader = "Angry"


func _itemsgenerator():
	var maybeitems = [Global.ItemsList[0],Global.ItemsList[1]]
	item1.type =maybeitems[0]
	item2.type =maybeitems[1]



func _on_area_2d_area_entered(area: Area2D) -> void:
	#_save()
	var t_finish = create_tween()
	uifinish.show()
	t_finish.tween_property(uifinish,"position",Vector2(75,950),0.5)
	emit_signal("gamestop")

#func _save2():
	#var data = SceneData.new()
	#data.HighScore += Global.points
	#
	#ResourceSaver.save(data,"user://scene_data.res")
	#print("saved!")

func _save():
	var file = FileAccess.open("res://savegame.data", FileAccess.WRITE)
	file.store_var(Global.HighScore)

Coins:

extends Node2D
var rng = RandomNumberGenerator.new()
@onready var a1 = $Coin1/area1
@onready var a2 = $Coin2/area2
@onready var a3 = $Coin3/area3

@onready var coin1a =$Coin1/C1a
@onready var coin2a =$Coin1/C2a
@onready var coin3a =$Coin1/C4a
@onready var coin4a =$Coin1/C3a
@onready var coin1b =$Coin2/C1b
@onready var coin2b =$Coin2/C2b
@onready var coin3b =$Coin2/C3b
@onready var coin4b =$Coin2/C4b
@onready var coin1c =$Coin3/C1c
@onready var coin2c =$Coin3/C2c
@onready var coin3c =$Coin3/C3c
@onready var coin4c =$Coin3/C4c

@onready var allcoins = [coin1a,coin2a,coin3a,coin4a,coin1b,coin2b,coin3b,coin4b,coin1c,coin2c,coin3c,coin4c]
@onready var allarea = [a1,a2,a3]

@onready var coins

var coinpasses = randi_range(1,3)
var coinx
var coininstance
var column = 0

var col1
var col2
var col3

@onready var coinscolumns = [$Coin1,$Coin2,$Coin3]

func _ready() -> void:
	Global.points = 0
	for i in coinpasses:
		coinx = randi_range(300,Global.RaceLenght-200)
		coinscolumns[column].position.x = coinx
		column += 1

func _process(delta: float) -> void:
	pass


func _on_character_main_area_entered(area: Area2D) -> void:
	Global.points += 16
	Global.HighScore += 16



func _on_area_1_area_entered(area: Area2D) -> void:
	_animation_exit(0)
	allarea[0].queue_free()
	

func _on_area_2_area_entered(area: Area2D) -> void:
	_animation_exit(4)
	allarea[1].queue_free()


func _on_area_3_area_entered(area: Area2D) -> void:
	_animation_exit(8)
	allarea[2].queue_free()
	

func _animation_exit(coins):
	var cointween = create_tween()
	cointween.set_parallel(true)
	cointween.tween_property(allcoins[0+coins],"scale",Vector2(0,0),0.5)
	cointween.tween_property(allcoins[1+coins],"scale",Vector2(0,0),0.5)
	cointween.tween_property(allcoins[2+coins],"scale",Vector2(0,0),0.5)
	cointween.tween_property(allcoins[3+coins],"scale",Vector2(0,0),0.5)
	await cointween

main character:

extends Area2D

#Character looks
@onready var body = $AnimatedSprite2D

var slimetypes = {
	1: "slimey"
}

var slimecolors = {
	1: "blue",
	2: "green",
	3: "pink",
	4: "red",
	5: "yellow",
}


#Character variables
var is_alive
var stamina_level
var dex_level
var stamina_regain

signal moved

@onready var StaminaTimer =$Camera2D/UI/TIMERS/GB_stamina
@onready var TurboTimer = $Camera2D/UI/TIMERS/GB_turbo
@onready var TapSpace = $Camera2D/UI/TapSpace

@onready var PB = $Camera2D/UI/TextureProgressBar

@onready var turbobutton = $Camera2D/UI/Turbo

func _process(delta: float) -> void:
	_deathloop()
	PB.value = stamina_level
	Global.GB_distance = $".".position.x
	if stamina_level <= Global.GB_turbo_cost + 5:
		turbobutton.texture_disabled
	else:
		turbobutton.texture_normal
	

#BEHAVIOUR
#variables
func _ready() -> void:
	#LOOKS
	#_load()
	#GAMEPLAY
	TapSpace.disabled = false
	#$".".animation = "idle"
	body.frame = 0
	stamina_level = Global.GB_stamina
	dex_level = Global.GB_stamina_dex
	stamina_regain = Global.GB_stamina_regain
	is_alive = true

#death loop
func _deathloop():
	if stamina_level < 5:
		is_alive = false
		_anim_death()
	elif stamina_level >= 25:
		if is_alive == false:
			is_alive = true
			#$".".animation = "idle"
			body.frame = 0
	

#stamina
func _on_gb_stamina_timeout() -> void:
	if stamina_level <= Global.GB_stamina:
		stamina_level += stamina_regain


#turbo
func _on_turbo_button_down():
	if stamina_level >= Global.GB_turbo_cost + 5:
		stamina_level -= Global.GB_turbo_cost
		_anim_turbo()
		TurboTimer.start()
		
		
func _on_gb_turbo_timeout():
	#$".".animation = "idle"
	body.frame = 0
	
#ANIMACJE
func _anim_move():
	$".".position.x += Global.GB_speed
	#$".".play("move")
	body.frame = 1

func _anim_death():
	#$".".play("dead")
	body.frame = 2

func _anim_turbo():
	#$".".play("turbo")
	body.frame = 3
	var actualpositionx = $".".position.x
	var actualpositiony = $".".position.y
	var turbotween = create_tween()
	turbotween.tween_property($".", "position", Vector2(actualpositionx + Global.GB_turbo_power, actualpositiony),1)
	StaminaTimer.start()

func _on_tap_space_button_down() -> void:
	StaminaTimer.start()
	if is_alive == true:
		_anim_move()
		stamina_level -= dex_level
		emit_signal("moved")
		

func _on_tap_space_button_up() -> void:
	#$".".animation = "idle"
	body.frame = 0


func _on_node_2d_gamestop() -> void:
	TapSpace.disabled = true

#func _load():
	##var data = ResourceLoader.load("user://scene_data.res") as SceneData
	##body.animation = str(data.GBLooks)
#
	##print("loaded!")
	#pass

Finish pop up:

extends TextureButton

@onready var points = $points
var amount

signal hide

func _ready() -> void:
	pass


func _process(delta: float) -> void:
	pass


func _on_button_down() -> void:
	get_tree().change_scene_to_file("res://scenes/play.tscn")

#func _load():
	#var data = ResourceLoader.load("user://scene_data.res") as SceneData
	#amount = data.HighScore
	#points.text = str(amount)
	#print("loaded!")


func _on_node_2d_gamestop() -> void:
	points.text = str(Global.points)

I can’t find this bug so I will apreciate any help!

If I had to guess, and it would only be a guess, but you are probably getting multiple collision events.

Add a GD Print to the above and see if you get more than one. If thats the case then you need some sort of double check to ensure after the first collision you dont get another one.

As I said though thats just a guess.

1 Like

It runs once. This area 2D is the finish line

I’d expect it’s some interaction between Global.HighScore and Global.points; you’re maintaining a relationship between those somewhere that isn’t in the code above. It could be several things:

  • in _on_character_main_area_entered() you’re directly adding 16 to both Global.points and Global.HighScore, which seems wrong
  • somewhere you’re presumably testing whether Global.points > Global.HighScore to update the high score if necessary; does that code add or assign?
  • as @OriginalBadBoy says, check for multiple collisions, and expanding on that, I’d put a unique print statement next to everything that changes Global.points or Global.HighScore, like print("End of race +16") or the like – that’ll give you a log of what happened
2 Likes

thank you! I tried to find bugs but I still can’t find any. I will try changing collisions masks, maybe that will help!