Randomise answers' position in quiz game

So previously I made a quiz game from this topic I asked Link button answers to randomised questions - How do I…? - GDevelop Forum

But now I’d like to step it up a notch. So I have a structure of questions in an array as each question is an element like this

And what I’m trying to do is :

  1. have 4 possible answers and 1 question in each structure of the array, I can put the questions manually in the array so ok

  2. I want the possible answers spawn RANDOMLY, in between the 4 texts that I created in the UI, while the questions associated with the answers always spawns in the TextQuestion fixed position on top.

My code is basically the same of the topic I linked above, but I can’t seem to make it work properly, the questions spawns, but the answers don’t.

Do i use a “Pick a random Text”? And which action do I assign to it? I tried :
Change text to Text1 set to:VariableString(MediumQuestions[Variable(random)][“Answer1”])

I don’t think I can apply this to when I have 4 random spawns, In my easier version I had 2 answers and didn’t change position.

How do I fix this?
Thanks and kind regards.

1 Like

If you look at the “Pair” example when you launch Gdevelop, it has something similar.

If you add placeholder objects and put your question text objects into a group then you can use a for each placeholder and choose a random text from the text group. Then all you need to do is change the text object in some way like its visibility. Then you can filter out the picked objects from the random picker. In this case I used an [inverted] “answers” is visible.

For this, the answer text objects are in a group named “answers”. Hide answers, randomly pick one, move it to placeholder, show and repeat for hidden answers.

Hey thanks for replying,

I checked out the solution and it gave me an insight to progress! There’s a flaw in the example for my case

  1. I don’t need to hide the answers, I just want them to spawn randomly at 4 of the istances of the same text object Spawn1

  2. In this case, I don’t spawn sprites in the position of the object placeholders, I am spawning text, so instead of the sprites I went with default text objects (Spawn1)
    Here’s a skeleton view of what it looks like now (ignore the red squares):

Here’s also the events and conditions that I wrote

Now the one thing I want to fix is that I planned 4 ANSWERS to each question with 1 CORRECT ANSWER and I don’t know how to exactly choose the other placeholders that contain the incorrect answers (“errore”).

How do I change the text of the placeholders for the wrong answers without repeating them like in the first picture I put here?

My questions/answer structure is as the one I put in the original question.

Hi. I used visibility as an example and also to hide the answers until they’re shuffled.

If you use a “for each placeholder object” you can cycle thru the placeholders. In my example I used the condition of visible to pick all hidden text objects. It works like a filter. Without a condition all answers are picked. After you choose a random answer you change something like the visibility so the next time you pick a random object the condition will ignore it. So, it won’t get picked more than once.

My thinking was:
hide the text objects.
Assign the answers to the text objects

For each placeholder object
If answer text object is invisible
------ | move text object to placeholder
------ | show text object

I don’t mean to talkdown or repeat something you already know. I just don’t know what you know.

1 Like

Hey I think I’m understanding what you mean. I tried this solution and I have a few doubts.

  • How should my questions and answers be structured then? I’ve set them up in a structure so that I could maintain them proper and improve/change in the future.
    And the group method you suggested works, but I don’t think it’s the best for this because I’m fairly new and never tried this before, will need more time to think on this. The group method only gathers objects in the scene? I can’t put my scene variables in it(in this case, the questions with the answers).

  • Referring myself to the first point, how would you add the questions with the matching wrong and correct answers?

I’m not sure where you’re getting you’re questions and answers (right and wrong ones)

Hide the answer text objects

IDK if you’re randomizing the questions or going in order. Assign an answer to each answer text object

And as discussed earlier,
Shuffle the position of the text objects and make answer text objects visible

Ex. questionNumber=1
TextAnswer1 = VariableString (questionsArray[Variable(questionNumber)].answer1)
TextAnswer2 = VariableString (questionsArray[Variable(questionNumber)].answer2)

1 Like

I have this structure from the first post with questions containing wrong and right answers.

I am randomising the questions :slight_smile: so that’s why I’m having difficulties, assigning directly answers to text variables is not what I want to do.

So I want the different answers in the structure to be assigned at random instances of the text (in this case Spawn1) and I can do that with the correct answer.

BUT the other answers I can’t, the UI shows the correct answer (“risposta1”) randomly and errore is only one of the wrong answers. They are all part of the Array Quiz, inside the same structure

and the code for the random position is

Does this make sense? How would it be solved?

Thank you

I wish there was a trivia example. I’m having trouble with this. Maybe someone else can explain it better.

1 Like

I’ll look into it thanks anyways :slight_smile: seems to be more in the intermediate/advanced gdevelopment side

1 Like

I would look at putting the answers (1 correct and 3 incorrect) into a temporary array. For each answer textbox, I’d set the text to a random entry from the array, and then remove that entry from the array. Something like :

Note, this isn’t the best way, but it’s the easiest and simplest to understand.

And the reason for the first being action using Random(3) and not Random(4) is because arrays are 0 based, meaning the first entry has an index of 0. For an array with 4 entries, the index numbers are 0, 1, 2 & 3, and so we want a random number between 0 and 3, which Random(3) provides.

1 Like

Thank you! I’m trying it now and I understand it. I now need to combine it with the other functions.

How would I structure the question text then? I would still put it in the structure containing the answers array:

And the code would be identical to what you said, plus the question referred to the randomised answers. I’m sure syntax of the random question is not correct yet, how would it be right?

And this I understand.
What would you do if you want to randomise between questions paired up with their answers in this structure?
I would create a structure for each set of answers(always in array paired up with their respective questions) and copy these events you proposed as sub event when clicking an answer.
So then I would have to randomise also between structures alongside the arrays contained in them?