Code runs immediatly after starting

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Criepstar

I wanted to make my first game, that I made alone a game like roulette so you choose the stake and then click on a play button. And randomly you lose or win.

extends Node2D

func _ready():

func start():
var balance = 50
var balance1 = str(balance)
$BalanceLabel.text = balance1

func _on_Button_pressed():
var a = 1
var b = 2
_on_Input_text_changed(a, b)

func _on_Input_text_changed(new_text, balance):
new_text = float(new_text)
balance = float(balance)
if new_text > balance:
$Result.text = “Sorry Sir, the stake is to high”
elif new_text < balance:
var random =
var number = random.randf_range(1,3)
number = int(number)
if number == 1:
balance = float(balance)
new_text = float(new_text)
balance += new_text
var balance1 = str(balance)
$Result.text = “Yes Sir, you won\nyour new balance is " + balance1
$BalanceLabel.text = balance1
elif number == 2:
balance -= new_text
var new_text1 = str(new_text)
$Result.text = “Sorry Sir you lost " + new_text1 +” coins”
var balance1 = str(balance)
$BalanceLabel.text = balance1
elif number == 3:
balance -= new_text
var new_text1 = str(new_text)
$Result.text = “Sorry Sir you lost " + new_text1 +” coins"
var balance1 = balance
$BalanceLabel.text = balance1
$Result.text = “Error”

I too found a code in the documentry, for saving the game:
extends Control

func save_game():
var save_game =“user://”, File.WRITE)
var save_nodes = get_tree().get_nodes_in_group(“Persist”)
for i in save_nodes:
var node_data =“save”);

func load_game():
var save_game =
if not save_game.file_exists(“user://”):
return # Error! We don’t have a save to load.

var save_nodes = get_tree().get_nodes_in_group("Persist")
for i in save_nodes:
	i.queue_free()"user://", File.READ)
while not save_game.eof_reached():
	var current_line = parse_json(save_game.get_line())
	var new_object = load(current_line["filename"]).instance()
	new_object.position = Vector2(current_line["pos_x"], current_line["pos_y"])
	for i in current_line.keys():
		if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y":
		new_object.set(i, current_line[i])

So if I run the project, it immediatly says if you’ve won or lost and sometimes the ,balannce’’ is around 3.
I really dont know whats false
(If my english is bad, sorry)
Thanks for all your answers!

Well, from what I can see in the _ready() function, we hide the Result node, run the code in the start() function, and run the code in the _on_Button_pressed() function.

Not sure if you want that last one to run right away, though…

System_Error | 2020-04-07 22:03

Thank you for your answer.
I probaly misunderstud the function _ready

Criepstar | 2020-04-07 22:06

:bust_in_silhouette: Reply From: Star Frog

Good morning (00:01)!

The “_on_button_pressed” signal does not have to be called (in the “_ready” function). It should be activated automatically when you have connected the signal.

If you want to do something in a similar direction, I recommend the “_process” function. It is modified and is called every frame. For example, can be used for key queries. The “_ready” function is ‘only’ executed once at the start of the script.

I hope I could help you for further questions, get in touch. Bye for now!

Thanks for the answer

Criepstar | 2020-04-07 22:17