Help with random variables

Hello,
This is my first post here and I’m still new to Gdevelop, but I’ve been really stuck on something and I’ve watched a ton of videos and read previous threads and was hoping to get some help on my problem
I’m creating a little trivia game, and I want it so when the mouse is released a question and 4 associated answers appear. But I also want the questions that can a appear each time to be random.

Basically - Release mouse button > Random number is generated > variable assigned to that number fills question and answer text fields associated with that variable number. I’ve been able to get random numbers to generate, but I’m honestly very stuck on this and I have little coding experience.
I was initially going to create a scene for each question but I figured that would be too clunky.

Appreciate any help.

Perhaps store your questions/answers in an array variable, which is a structure of question and answers, like this:

0:
  question: "Why is the sky blue?"
  answer1: "To get to the other side"
  answer2: "Because green is ugly"
  answer3: "Science"
1:
  question: "Why is the ocean blue?"
  answer1: "See previous answer"
  answer2: "Because the sea weed"
  answer3: "Because the sky fell down"
2:
  question: "You get the idea..."

Then you can pick a random number in your mouse event, and then choose which variable to display based on that number.

Hey, appreciate the answer! So I already have questions and answers set in the scene variables, it’s the connecting the random number to the variables which I’m having the most difficulty with.

Can you post a screenshot of your variables?

1 Like


Here you go.

Hmm, I just realised that I don’t think you can do what you want with an array… You want to be able to set the text object to something like VariableString(Variable[Random(2)].Question), but you can only do that with Structures. See Variables [GDevelop wiki] for details.

1 Like

Hi Ruskimedveyt,

the setup and array you have works well! I recommend you only rename your question array to something like “All_Questions”. Right now it is named “Variable” as I see in your screenshot. That can cause some confusion.

To fill out the questions, you need to do the following:
1.) Save the random value in a variable
(e.g. Variable “_Question_Picked” = Random(3)) side note: if you pick Random(3), it will pick a random number between 0, 1, 2 and 3, so you have the 4 options you need. Do not actually write Random(4) which would give you 5 options (because it’d pick from 0,1,2,3,4) :wink:

2.) Rename your question array to “All_Questions”. Then fill out the text objects like this:

E.g. the question:
Modify text to → VariableString(All_Questions[Variable(_Question_Picked)].Question)

E.g. answer 1:
Modify text to → VariableString(All_Questions[Variable(_Question_Picked)].Answer1)

E.g. answer 2:
Modify text to → VariableString(All_Questions[Variable(_Question_Picked)].Answer2)

etc.

1 Like

Hey, thank you so much for the help! I’m learning a lot. I think the only little bit I don’t get is when you say to save the random value in a variable? Do I create a scene variable and set it to =random(3)? Bit confused on that part!


Wait I think I got it! Thank you so much!

Hey,

I’m glad I could help you out!

Yes, that’s it, exactly! You create the scene variable in the scene editor and then you give it a random value as you did!

You can theoretically just set a random value without creating the variable in the scene editor first, because in Javascript when you set a variable value, it automatically declares the variable. But I don’t recommend that, if you’re not very used to Gdevelop. Because the variable name will not be stored in the variables list, and the name will not be suggested when you type it. So it will be easier to make mistakes.

And you can use different names btw.! I use the underscore “_” personally only for me to organize certain variables that I use for a specific thing.
But feel free to use your own names and descriptions!

1 Like