@DanielC said:
It does not print a thing, because I get error
Invalid get index 'my_variable' (on base: 'Nil').
This is the way it works:
Looking at the code, there is a number of reasons why it might not be working.
The first and foremost issue being you need to define score as a class variable, like you have defined pogingen. Using the code you posted, this should fix the issue:
extends Node
# Added the line below:
var score = 0
var pogingen
var current_scene = null
func _ready():
var root = get_tree().get_root()
current_scene = root.get_child(root.get_child_count()-1)
func goto_scene(path):
call_deferred("_deferred_goto_scene", path)
func _deferred_goto_scene(path):
current_scene.free()
var s = ResourceLoader.load(path)
current_scene= s.instance()
get_tree().get_root().add_child(current_scene)
get_tree().set_current_scene(current_scene)
func reset():
score = 0
pogingen = 0
print("reset in global")
pass
The next big issue is how you are accessing the variables through the reset function. Because reset is a function, and you are not returning anything in said function (which makes sense given what the function is doing), the code is not working.
You need to access the class variables within global like this: global.score += 1 to add one to score, for example. This works because score is a class variable within the global node, as defined by the above script.
I would highly suggest reading some programming books covering Python programming, as it will help explain why the code wasn't working. GDScript is very similar to Python, so the transition from one to the other should be fairly smooth.
Personally, I learned Python using the Hello World! book (link) when I was young and if I remember, it serves well as a good introduction to the language while making simple games with Python.
Anyway, here is the, hopefully, fixed code:
func _on_Button_pressed():
antwoord_check = get_node("VBoxContainer/VBoxContainer/Invoer").text
gekozen_vraag = dictionary_met_antwoorden[willekeurige_vraag]
if (antwoord_check == gekozen_vraag):
knopje.add_color_override("font_color", Color( 0, 1, 0, 1 ))
knopje.add_color_override("font_color_pressed", Color( 0, 1, 0, 1 ))
knopje.add_color_override("font_color_hover", Color( 0, 1, 0, 1 ))
knopje.set_text("Juiste antwoord")
# To add one to a variable, just use "variable += 1"
# or "variable = variable + 1"
global.score += 1
global.pogingen += 1
juist = true
pauze.start()
elif (antwoord_check != gekozen_vraag):
knopje.add_color_override("font_color", Color(176,0,0,255))
knopje.add_color_override("font_color_pressed", Color(176,0,0,255))
knopje.add_color_override("font_color_hover", Color(176,0,0,255))
knopje.set_text("Fout antwoord")
# Same here
global.pogingen += 1
fout = true
pauze.start()
func _on_Timer_timeout():
if (juist == true):
self.juist_beantwoord()
elif (fout == true):
self.fout_beantwoord()
func juist_beantwoord():
# I am assuming you want to access the variables directly...
var po=global.pogingen
var sc=global.score
# I'm confused at what this is trying to achieve.
# I think you are wanting to reset the score and pogingen so it is ready for the
# next game/level, and based on the score (prior to reset) change the scene.
# Is that correct?
#
# Reset the score and pogingen, since we have a copy in the
# po and sc variables defined above. This will the variables
# defined in global ready for the next game/level.
global.reset()
# Check the various combinations of score and pogingen using sc and po
if (sc == 2 and po == 2):
knopje.set_disabled(true)
get_node("/root/global").goto_scene("res://eindscore.tscn")
if (sc < 2 and po == 2):
knopje.set_disabled(true)
get_node("/root/global").goto_scene("res://helaas.tscn")
if (sc <2 and po < 2):
knopje.set_disabled(true)
get_node("/root/global").goto_scene("res://zilvervloot.tscn")
if (sc >2 and po > 2):
knopje.set_disabled(true)
print(po)
print(sc)
print("te vaak fout")
get_node("/root/global").goto_scene("res://zilvervloot.tscn")
if (sc ==2 and po > 2):
knopje.set_disabled(true)
print(po)
print(sc)
print("te vaak fout 2")
get_node("/root/global").goto_scene("res://zilvervloot.tscn")
if (sc <2 and po > 2):
knopje.set_disabled(true)
print(po)
print(sc)
print("te vaak fout 2")
get_node("/root/global").goto_scene("res://zilvervloot.tscn")
I added some comments explaining what I changed.
Hopefully this helps! :smile: