How can I run a function when a certain number is reached in my 'score counter'?

Godot Version

Replace this line with your Godot version

Question

How can I make something happen when the player scores a certain number

Hi, I’m working on a game where you livestream a kaiju and you get ‘points’ by viewers being added. The longer the kaiju is in the camera view, the higher the ‘viewer count’ goes. I was thinking you can win the game by getting to 100 viewers for example. The stream always starts with the character’s three friends.

Screenshot of the game, Viewer count is in the top right and the number goes up whenever the crocodile/kaiju is in the camera view :

Player script :

extends CharacterBody3D

@export_group("Camera")
@export_range(0.0, 1.0) var mouse_sensitivity := 0.25

@export_group("Movement")
@export var move_speed := 50
@export var acceleration := 20.0

var _viewer_count = 3
var _camera_input_direction := Vector2.ZERO
var _last_movement_direction := Vector3.BACK

@onready var _viewer_count_label: Label = %viewer_count_label
@onready var _camera_pivot: Node3D = %CameraPivot
@onready var _camera: Camera3D = %Camera3D
@onready var _Skin: Node3D = %YorkSkin
@onready var _RowBoat: AudioStreamPlayer3D = %RowBoat


func _ready():
	pass

func _input(event):
	#Misc. Controls ---------------------------------------------
	#Input = "Left Click" to Capture Mouse Movement i.e. Control the camera.
	if event.is_action_pressed("left_click"):
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	
	#Input = "Alt" to Navigate other Windows. 
	if event.is_action_pressed("Alt"):
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	
	
	#Scene Navigation -----------------------------
	#Input = "R"
	if Input.is_action_just_pressed("Restart"):
		get_tree().reload_current_scene()
	
	#Input = "Esc"
	if Input.is_action_just_pressed("Exit"):
		get_tree().quit()
	
		
#Movement & Animations --------------------------------------------------
	#Variables
	var ground_speed := velocity.length()
	var AP = $YorkSkin/AnimationPlayer
	
	
		
	if ground_speed > 0.0:
		AP.play("Rowing")
		if %RowTimer.time_left <= 0:
			%RowBoat.play()
			%RowTimer.start(3.8)
	else:
		AP.play("Idle")
	

func _unhandled_input(event: InputEvent) -> void:
	var is_camera_motion :=(
		event is InputEventMouseMotion and 
		Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
	)
	if is_camera_motion:
		_camera_input_direction = event.screen_relative * mouse_sensitivity

func _physics_process(delta: float) -> void:
	
	
	
#Camera Controls 

	_camera_pivot.rotation.y -= _camera_input_direction.x * delta
	
	_camera_input_direction = Vector2.ZERO
	
	#Movement Controls #--------------------------------------------------
	var raw_input := Input.get_vector("Forward","Backward", "Left", "Right")
	var forward := _camera.global_basis.x
	var right := _camera.global_basis.z
	
	var move_direction := forward * raw_input.y + right * raw_input.x
	move_direction.y = 0.0
	move_direction = move_direction.normalized()
	
	velocity = velocity.move_toward(move_direction * move_speed, acceleration * delta)
	move_and_slide()
	
	if move_direction.length() > 0.2:
		_last_movement_direction = move_direction
	var target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
	_Skin.global_rotation.y = target_angle

#---------------------------------------
#Viewer Count
func _add_viewers(new_viewer_count: int) -> void:
	_viewer_count = new_viewer_count
	_viewer_count_label.text = "Viewers: " + str(_viewer_count)
	


#-------------------------------
#LiveStream Interactions

func happy_chat():
	print("This stream is great!")
	
	var _new_viewers_array = [1, 2, 5, 10, 20, 3, 8, 6, 7, 13]
	
	#Viewer Count Handling
	_add_viewers(_viewer_count + _new_viewers_array.pick_random())
	print(_viewer_count)
	
	
	#Zach handling
	var happyzacharray = ["omG", "he's real!!!", "SO COOL"]
	$SheepyZach/AnimationPlayer.play("PopUp")
	$SheepyZach/Icon/Message.text = happyzacharray.pick_random()
	
	#Mars handling
	var happymarsarray = ["HOLY SHIT", "He's actually real", "This is insane"]
	$xXplanet_marsXx/AnimationPlayer.play("PopUp")
	$xXplanet_marsXx/Icon/Message.text = happymarsarray.pick_random()
	
	#Jamie handling
	var happyjamiearray = ["Whoa!", "Huh!?", "I gotta show my friends!"]
	$jamie666bends/AnimationPlayer.play("PopUp")
	$jamie666bends/Icon/Message.text = happyjamiearray.pick_random()
	

func upset_chat():
	print("This stream SUCKS.")
	
	
	#Zach handling
	var upsetzacharray = ["Better luck next time...", "At least it's nice out..."]
	$SheepyZach/AnimationPlayer.play("PopUp")
	$SheepyZach/Icon/Message.text = upsetzacharray.pick_random()
	
	#Mars handling
	var upsetmarsarray = ["Where'd he go???", "Maybe it's a filter...", "idk about this.."]
	$xXplanet_marsXx/AnimationPlayer.play("PopUp")
	$xXplanet_marsXx/Icon/Message.text = upsetmarsarray.pick_random()
	
	#Jamie handling
	var upsetjamiearray = ["gonna lurk...", "brb."]
	$jamie666bends/AnimationPlayer.play("PopUp")
	$jamie666bends/Icon/Message.text = upsetjamiearray.pick_random()


Maybe you could check if new_viewer_count is greater than 100 in the _add_viewers function?

1 Like

Thank you so much that works perfectly!
Just for reference I changed it to this :

#---------------------------------------
#Viewer Count
func _add_viewers(new_viewer_count: int) -> void:
	_viewer_count = new_viewer_count
	_viewer_count_label.text = "Viewers: " + str(_viewer_count)
	
	if new_viewer_count >100: 
		get_tree().change_scene_to_file("res://Win/win.tscn")