+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.

Code:
extends Node2D

func ready():
$Result.hide()
start()
_on
Button_pressed()

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

func onButtonpressed():
var a = 1
var b = 2
_on
Inputtextchanged(a, b)

func onInputtextchanged(newtext, balance):
new
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()
random.randomize()
var number = random.randf
range(1,3)
number = int(number)
if number == 1:
balance = float(balance)
newtext = float(newtext)
balance += newtext
$Input/Input.hide()
var balance1 = str(balance)
$Result.show()
$Result.text = "Yes Sir, you won\nyour new balance is " + balance1
$Result/baseline
thumbupblack18dp.show()
$BalanceLabel.text = balance1
elif number == 2:
balance -= new
text
$Input/Input.hide()
var newtext1 = str(newtext)
$Result.show()
$Result.text = "Sorry Sir you lost " + newtext1 +" coins"
$Result/baseline
thumbdownblack18dp.show()
var balance1 = str(balance)
$BalanceLabel.text = balance1
elif number == 3:
balance -= new
text
$Input/Input.hide()
var newtext1 = str(newtext)
$Result.show()
$Result.text = "Sorry Sir you lost " + newtext1 +" coins"
$Result/baseline
thumbdownblack_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 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");
savegame.storeline(tojson(nodedata))
save_game.close()

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:
    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!

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.