+1 vote

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 onButtonpressed():
var a = 1
var b = 2
Inputtextchanged(a, b)

func onInputtextchanged(newtext, balance):
text = float(newtext)
balance = float(balance)
if new
text > balance:
$Result.text = "Sorry Sir, the stake is to high"
elif newtext < balance:
var random = RandomNumberGenerator.new()
var number = random.randf
number = int(number)
if number == 1:
balance = float(balance)
newtext = float(newtext)
balance += newtext
var balance1 = str(balance)
$Result.text = "Yes Sir, you won\nyour new balance is " + balance1
$BalanceLabel.text = balance1
elif number == 2:
balance -= new
var newtext1 = str(newtext)
$Result.text = "Sorry Sir you lost " + newtext1 +" coins"
var balance1 = str(balance)
$BalanceLabel.text = balance1
elif number == 3:
balance -= new
var newtext1 = str(newtext)
$Result.text = "Sorry Sir you lost " + newtext1 +" 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 savegame():
var save
game = File.new()
savegame.open("user://savegame.save", File.WRITE)
var save
nodes = gettree().getnodesingroup("Persist")
for i in savenodes:
var node
data = i.call("save");

func loadgame():
var save
game = File.new()
if not savegame.fileexists("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:

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()
    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!

in Engine by (44 points)

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…

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

1 Answer

0 votes
Best answer

Good morning (00:01)!

The "onbutton_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!

by (73 points)
selected by

Thanks for the answer

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.