How to implement the generation of buttons with answers?

I want to make a game where a question appears and two buttons “Yes” and “No”, so that if the player answers correctly, he gets points, and if he makes mistakes, he loses them. Here’s the solution that’s come to my mind so far: Create a fabric object with the question and the previously voiced buttons. That’s the right decision ?

And how to implement random so that the conditional questions change places every time, like the answer buttons?

Hello @Regven!

Here is what I’d do:
Have 3 gui nodes, one text node to hold the question, a button for yes and one for no.
Then I would use a module (Lua modules in Defold) containing a table to hold the questions and the expected answers, eg:

questions.lua

local M = {}

M.questions = {
	{"1 + 3 = 5", false},
	{"10 + 10 = 20", true},
	{"10 / 2 = 5", true},
	{"6 + 4 = 11", false},
	{"2 + 3 = 5", true},
	{"1 * 1 = 2", false},
}

return M

Require the file in your script and assign a local variable to it, eg like so:

local m = require “main.questions”

Then you can access question and solution like so using the number of the question:
local question = m.questions[question_number][1]
local solution = m.questions[question_number][2]

Set the text of the text node to the question.

Next get the answers:
If “yes” is pressed:
answer = true
if “no” is pressed:
answer = false

Compare answer and solution:
if equal, the answer is correct, add one point, if not, the answer is incorrect, deduct a point.

This leaves you with the number of the question.
I assume, you want the question to be both random and not repeating:
Take a look here:
Pick random numbers without repetition - Questions - Defold game engine forum

Check out the Lua manual about math.random and math.randomseed, here eg:
Lua 5.1 Reference Manual
Programming in Lua : 18

7 Likes

Thank you very much for your answer !

1 Like