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.

Code:
extends Node2D

func _ready():
$Result.hide()
start()
_on_Button_pressed()

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 = RandomNumberGenerator.new()
random.randomize()
var number = random.randf_range(1,3)
number = int(number)
if number == 1:
balance = float(balance)
new_text = float(new_text)
balance += new_text
$Input/Input.hide()
var balance1 = str(balance)
$Result.show()
$Result.text = “Yes Sir, you won\nyour new balance is " + balance1
$Result/baseline_thumb_up_black_18dp.show()
$BalanceLabel.text = balance1
elif number == 2:
balance -= new_text
$Input/Input.hide()
var new_text1 = str(new_text)
$Result.show()
$Result.text = “Sorry Sir you lost " + new_text1 +” coins”
$Result/baseline_thumb_down_black_18dp.show()
var balance1 = str(balance)
$BalanceLabel.text = balance1
elif number == 3:
balance -= new_text
$Input/Input.hide()
var new_text1 = str(new_text)
$Result.show()
$Result.text = “Sorry Sir you lost " + new_text1 +” coins"
$Result/baseline_thumb_down_black_18dp.show()
var balance1 = balance
$BalanceLabel.text = balance1
else:
$Result.show()
$Result.text = “Error”

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

func save_game():
var save_game = File.new()
save_game.open(“user://savegame.save”, File.WRITE)
var save_nodes = get_tree().get_nodes_in_group(“Persist”)
for i in save_nodes:
var node_data = i.call(“save”);
save_game.store_line(to_json(node_data))
save_game.close()

func load_game():
var save_game = File.new()
if not save_game.file_exists(“user://savegame.save”):
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()


save_game.open("user://savegame.save", 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()
	get_node(current_line["parent"]).add_child(new_object)
	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":
			continue
		new_object.set(i, current_line[i])
save_game.close()

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