After 50 clicks, the clicker in Godot starts to lag

Godot Version

4

Question

when the player reaches 50 clicks, the level becomes 2 and the picture changes, but the game starts to lag. The click texture works slowly, and it’s impossible to click quickly. It’s strange, but it’s not the texture; it’s a PNG format

extends Node2D
var clic = 0


func _on_touch_screen_button_pressed() -> void:
	clic += 1
	print(clic)
func _physics_process(delta):
	$Labelcchet.text = "СЧЁТ: " + str(clic)

	if clic > 20:
		$TouchScreenButton.texture_normal = load("res://newpict/normal1.png")
		$TouchScreenButton.texture_pressed = load("res://newpict/clic1.png")
		$Labellvl.text = "Уровень: " + str(1)
	if clic > 50:
		$TouchScreenButton.texture_normal = load("res://newpict/normal2.png")
		$TouchScreenButton.texture_pressed = load("res://newpict/clic2.png")
		$Labellvl.text = "Уровень: " + str(2)

Physics_process is called 60 times per seconds

What happens here is that as soon as you reach 50 clic, first it changes the texture to “normal1” and then to “normal2” 60 times per seconds
This is because you check “if clic > 20” and “if clic > 50”. When clic is superior to 50 it is also superior to 20 so both conditions are met.

You should try something like :
if clic >50:
$TouchScreenButton.texture_normal = load(“res://newpict/normal2.png”)
$TouchScreenButton.texture_pressed = load(“res://newpict/clic2.png”)
$Labellvl.text = "Уровень: " + str(2)
elif clic > 20:
$TouchScreenButton.texture_normal = load(“res://newpict/normal1.png”)
$TouchScreenButton.texture_pressed = load(“res://newpict/clic1.png”)
$Labellvl.text = "Уровень: " + str(1)

That way you can only have one of both.

1 Like

You don’t need this _physics_process() at all, that’s what causing you issues - you’re loading textures every physics frame. This should work the same and be much more efficient:

extends Node2D


var clic = 0


func _ready() -> void:
	$Labelcchet.text = "СЧЁТ: " + str(clic)


func _on_touch_screen_button_pressed() -> void:
	clic += 1
	print(clic)
	
	$Labelcchet.text = "СЧЁТ: " + str(clic)
	
	if clic == 21:
		$TouchScreenButton.texture_normal = load("res://newpict/normal1.png")
		$TouchScreenButton.texture_pressed = load("res://newpict/clic1.png")
		$Labellvl.text = "Уровень: " + str(1)
	elif clic == 51:
		$TouchScreenButton.texture_normal = load("res://newpict/normal2.png")
		$TouchScreenButton.texture_pressed = load("res://newpict/clic2.png")
		$Labellvl.text = "Уровень: " + str(2)
4 Likes

Thank you for helping the newbies

2 Likes