![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | miss_bisque |
I have a fruit-ninja style game that has a singleton/autoload script called Global.gd
. This script randomly sets my “flying objects” to be used during the game. The target_objects
and distractor_object
are what the code generates, and if I quit and re-open the game, these will be randomly generated each time. I then use these in my main Game.gd
script during the game. (Global.gd script below)
extends Node
var list = range(0,10)
var sample =[]
var distractor_object = []
var target_objects = []
var bowl = preload("res://Sprites/Objects/bowl.png")
var bread = preload("res://Sprites/Objects/bread.png")
var cheese_grater = preload("res://Sprites/Objects/cheese_grater.png")
var clock= preload("res://Sprites/Objects/clock.png")
var cup = preload("res://Sprites/Objects/cup.png")
var pot = preload("res://Sprites/Objects/pot.png")
var straws = preload("res://Sprites/Objects/straws.png")
var tissue_paper = preload("res://Sprites/Objects/tissue_paper.png")
var toilet_paper = preload("res://Sprites/Objects/toilet_paper.png")
var tooth_paste = preload("res://Sprites/Objects/tooth_paste.png")
var tex_ref_array = [bowl, bread, cheese_grater, clock, cup, pot, straws, tissue_paper,
toilet_paper, tooth_paste]
func _ready():
randomize()
for i in range(4):
var x = randi()%list.size()
sample.append(list[x])
list.remove(x)
print("Array is " + str(sample))
target_objects = sample.slice(0,2)
print("Target objects are " + str(target_objects))
distractor_object = sample[3]
print("Distractor object is " + str(distractor_object))
func _reset_vars():
queue_free()
get_tree().reload_current_scene()
My problem is that I have now incorporated levels into my game. Therefore, when a player has reached a certain score, they advance to a more difficult level (the game is the exact same except the objects get smaller and occur with more frequency). So, I created a function in the global script above func _reset_vars():
that I thought would re-run the singleton script. However, it is not working in my game script.
In the game script, I have my process():
function that changes the level whenever a score is reached, and a _reset()
function hat
func _process(delta):
if targetObjectsPoints >= 5:
level += 1 #increase level
_reset() #reset game to rerun Global.gd script
if level >= 7:
end = true
_save()
get_tree().change_scene("res://Scenes/Restart.tscn")
func _reset():
#reset points
targetObjectsPoints = 0
distractorObjectsPoints = 0
missedObjectsPoints = 0
#reload game scene
get_tree().reload_current_scene()
queue_free()
#free Global script
Global._reset_vars()
#switch to scene that notifies player they have advanced to a new level
get_tree().change_scene("res://Scenes/LevelSwitch.tscn")
However, if I do this I now get an error in my Game.gd
script that the target object and distractor objects are nil
meaning they’re no longer defined and the Global
script did not re-run as intended.
Any advice here? Could it be that my Global
script is higher in the scene tree than my Game
script?
Thanks in advance!