-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question.gd
244 lines (191 loc) · 5.2 KB
/
Question.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
extends Node2D
var global_script = preload("res://Global.gd")
# var GS = get_node("res://Global")
var num = 0
var condition_met = false
var q_data = []
var a_data = []
var Old_data = [
{
"Q": "What is the meaning of life?",
"A": ["Life is good","Help!","There is Hope","What?"],
"CA":1
},
{
"Q": "What time is it?",
"A": ["tiger time","Now?","where?","10:00"],
"CA":1
},
{
"Q": "What language you speak?",
"A": ["French","Russian","Italian","Alien"],
"CA":1
},
{
"Q": "What car do you drive?",
"A": ["Audi","BMW","Toyota","Honda"],
"CA":4
},
{
"Q": "Where do you live?",
"A": ["in space","Bangkok","New York","in a cave"],
"CA":2
}
]
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
func _writejson_old():
var file = File.new()
if file.open("user://saved_game.sav", File.WRITE) != 0:
print("Error opening file")
return
# Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
file.store_line(q_data.to_json())
file.close()
func _writejson():
var jsonStr = JSON.print(q_data)
var file = File.new()
if file.open("user://user_data.json", File.WRITE) == OK:
file.store_string(jsonStr)
file.close()
print("Data exported to JSON successfully.")
else:
print("Failed to open file for writing.")
func loadFromJson():
var file = File.new()
# if file.open("res://user_data.json", File.READ) == OK:
if file.open(GS.quizz_file, File.READ) == OK:
var jsonStr = file.get_as_text()
file.close()
var jsonData = JSON.parse(jsonStr)
q_data = jsonData.result
# q_data = jsonData
if jsonData != null:
# Data has been loaded successfully
processLoadedData(jsonData)
else:
print("Failed to parse JSON data.")
else:
print("Failed to open file for reading.")
for _i in range(q_data.size()):
a_data.append("?")
func processLoadedData(data):
# Process the loaded data here
print("Loaded data:", data)
func _loadquestion(value):
var n = value
var QN = get_node("QuestionNumber")
var ANS = get_node("Answer")
var COR_ANS = get_node("Correct_Answer")
var MaxQString = str(q_data.size())
var QNString = str(n+1)
var PB = get_node("ProgressBar")
PB.min_value = 0
PB.max_value = q_data.size()
PB.step = 1
PB.value = n+1
QN.set("text",QNString + "/" + MaxQString)
var QT = get_node("QuestionText")
QT.set("text","Test Question")
var NQ = q_data[n].Q
QT.set("text",NQ)
var CA = q_data[n].CA
COR_ANS.set("text",CA)
ANS.set("text",a_data[n])
var AT = get_node("ANSGRID/ANS A")
var AQ = q_data[n].A[0]
AT.set("text",AQ)
AT = get_node("ANSGRID/ANS B")
AQ = q_data[n].A[1]
AT.set("text",AQ)
AT = get_node("ANSGRID/ANS C")
AQ = q_data[n].A[2]
AT.set("text",AQ)
AT = get_node("ANSGRID/ANS D")
AQ = q_data[n].A[3]
AT.set("text",AQ)
# pass
# Called when the node enters the scene tree for the first time.
func _ready():
num = 0
loadFromJson()
periodic_check()
_loadquestion(num)
# pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _on_Prev_Q_pressed():
if (num > 0):
num = num - 1
_loadquestion(num)
func _on_Next_Q_pressed():
num = num + 1
if num == q_data.size():
num = num -1
_loadquestion(num)
#pass # Replace with function body.
func _on_Save_pressed():
_writejson()
pass # Replace with function body.
func _on_ANS_A_pressed():
var text_value = "A"
a_data[num] = text_value
var ANS = get_node("Answer")
ANS.set("text",a_data[num])
pass # Replace with function body.
func _on_ANS_B_pressed():
var text_value = "B"
a_data[num] = text_value
var ANS = get_node("Answer")
ANS.set("text",a_data[num])
pass # Replace with function body.
func _on_ANS_C_pressed():
var text_value = "C"
a_data[num] = text_value
var ANS = get_node("Answer")
ANS.set("text",a_data[num])
pass # Replace with function body.
func _on_ANS_D_pressed():
var text_value = "D"
a_data[num] = text_value
var ANS = get_node("Answer")
ANS.set("text",a_data[num])
pass # Replace with function body.
func _on_Submit_pressed():
# check all questions answered
check_answers()
get_tree().change_scene("res://Score.tscn")
# Check Responses to Check
# Update Score
# change to final scene
pass # Replace with function body.
func check_answers():
GS.good_answer = 0
for i in range(q_data.size()):
print (q_data[i].CA, a_data[i])
if (q_data[i].CA == a_data[i]):
GS.good_answer = GS.good_answer + 1
# global_script.total_question = q_data.size()
GS.total_question = q_data.size()
print ("Number of Question: ", GS.total_question)
print ("Good Answer: ", GS.good_answer)
#compare asnwer to value
pass
func periodic_check():
var my_node = get_node("Submit")
while true: # Infinite loop to continuously check the condition
# Check if all questions have been answered.
condition_met = true
for i in range(q_data.size()):
if a_data[i] == "?":
condition_met = false
if condition_met:
# Perform the desired action when the condition is met
print("Condition is met!")
my_node.disabled = false
else:
print("Condition is not met!")
# Add a delay of 2 seconds (adjust this value to your desired time interval)
yield(get_tree().create_timer(2.0), "timeout")