words in button constintly change

Godot Version

4.3

Question

im trying to make the code pick a random number wich it is doing and print the word that it also randomly selects into a button the problem im having though is that it is constintly changing the number and word every frame

this is my code:

extends Control

@onready var return_button = $return_button
@onready var gui = $pop_ups
@onready var error = $pop_ups/PanelContainer2/error
@onready var timer = $Timer
@onready var button_1 = $pop_ups/PanelContainer2/Panel/HBoxContainer/Button
@onready var button_2 = $pop_ups/PanelContainer2/Panel/HBoxContainer/Button2
@onready var button_3 = $pop_ups/PanelContainer2/Panel/HBoxContainer/Button3

var words: Array = ["across", "beginning", "copyright", "disappear", "forward", "guard"]
var rng = RandomNumberGenerator.new()


func _process(delta):
	pause()
	unpause()
	if gui.visible == true:
		button_text()

func pause():
	if ScreenGui.gui.visible == true:
		get_tree().paused = true
	
func unpause():
	if ScreenGui.gui.visible == false:
		get_tree().paused = false

func random_word_start():
	var random_word = str(words[rng.randi_range(0, 5)])
	if gui.visibility_changed:
		return random_word

func _on_return_pressed() -> void:
	
	get_tree().change_scene_to_file("res://scenes/main_menue.tscn")

func random_number():
	var rand_num = randi_range(1,3)
	if gui.visibility_changed:
		return rand_num
	
func _on_button1_pressed() -> void:
	if random_number() == 1:
		gui.visible = false
	else:
		error.visible = true
		timer.start(1)

func _on_button_2_pressed() -> void:
	if random_number() == 2:
		gui.visible = false
	else:
		error.visible = true
		timer.start(1)
		
func _on_button_3_pressed() -> void:
	if random_number() == 3:
		gui.visible = false
	else:
		error.visible = true
		timer.start(1)
		
func _on_timer_timeout() -> void:
	gui.visible = false

func button_text():
	if gui.visible == true:
		if random_number() == 1:
			button_1.text = str(random_word_start())
		elif random_number() == 2:
			button_2.text = str(random_word_start())
		elif random_number() == 3:
			button_3.text = str(random_word_start())
		print(random_number(), random_word_start())

Hi!

The problem is that you’re calling button_text every frame, in _process:

func _process(delta):
	pause()
	unpause()
	if gui.visible == true:
		button_text()

Try calling it in _ready, like this:

func _ready():
	if gui.visible == true:
		button_text()

and that should be done only once.

Also, this is not related to your question, but calling pause and unpause every frame is also a very weird thing to do, so you may want to change that too.

I’ll try that when I next open the program hopefully that will work and I’ll look into other ways that I can make the pause and unpause work

1 Like