Topic was automatically imported from the old Question2Answer platform.
Asked By
Liliana Betancur
I am developing a video game focused on teaching math and I need to include in the scene a text that contains a question related to the topic and that the correct answer can be selected
Do you mean to have one scene capable of displaying any question?
Magso | 2019-10-17 18:15
yes, and answer options also appear
if you have any suggestions would be helpful too
The scene needs at least a RichTextLabel and a few Button nodes as children. You mentioned “focused on teaching math” so here’s an example.
var numbers : Array = []
var operation : int
var answer : float
var sum : String
func _new_question():
#set the sum length
var numberAmount = randi()%6+3
#get the numbers and operations
for i in numberAmount:
numbers.append(randi()%11+1)
#set the operation
operation = randi()%4+0
#write the first number
answer = numbers[0]
sum += str(numbers[0])
#write the rest of the sum
for num in numbers.size() - 1:
if operation == 0:
answer += numbers[num+1]
sum += "+"
elif operation == 1:
answer -= numbers[num+1]
sum += "-"
elif operation == 2:
answer *= numbers[num+1]
sum += "x"
elif operation == 3:
answer /= numbers[num+1]
sum += "÷"
sum += str(numbers[num+1])
#set the labels text as the sum
RichTextLabel.text = sum + "="
#get the child button nodes and make one the correct answer
var buttons = get_children()
var correctButton = randi()%buttons.size()+0
for ansOpt in buttons.size():
if ansOpt == correctButton:
buttons[ansOpt].text = str(answer)
else:
buttons[ansOpt].text = str(answer+rand_range(-10, 10))
This should give you a sum like 7+3+2= and a button with 12.