0 votes

My app has some buttons, that are filled on startup and set with some data. Then when the button is pressed a scene is loaded.

extends Button

var quiz = null

func lesson_selected():
    Quizzes.set_current_quiz(quiz)
    get_tree().change_scene("res://Quiz.tscn")

I am still a beginner so this is copy and paste code. But I think there is probably something better I can do here yes? Because I don't know how to pass that "quiz" data object into the scene, I am doing a "setcurrentquiz" call to a global object to hold the data. There must be a way to do something like this right?

extends Button

var quiz = null

func lesson_selected():
    s = get_tree().change_scene("res://Quiz.tscn")
    s.set_quiz(quiz)
Godot version 3.2
in Engine by (21 points)

1 Answer

0 votes

This is possible. Try preloading your quiz scene, then setting the quiz data on the instance.

Something like this:

extends Button

var quiz_scene = preload("res://Quiz.tscn")
var quiz = null

func lesson_selected():
    var s = quiz_scene.instance()
    get_tree().change_scene_to(s)
    s.set_quiz(quiz)
by (272 points)
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.