You are on page 1of 25

jump to content

MY SUBREDDITS

POPULAR

-ALL

-RANDOM

ASKREDDIT

-PICS

-WORLDNEWS

-NEWS

-FUNNY

-TODAYILEARNED

-VIDEOS

-GAMING

-GIFS

-AWW

-MOVIES

-MILDLYINTERESTING

-IAMA

-SHOWERTHOUGHTS

-TELEVISION

-JOKES

-OLDSCHOOLCOOL

-NOTTHEONION

-LIFEPROTIPS

-SCIENCE

-MUSIC

-EXPLAINLIKEIMFIVE

-PERSONALFINANCE

-TWOXCHROMOSOMES

-SPORTS

-FOOD

-TIFU

-ASKSCIENCE

-SPACE

-GETMOTIVATED

-EARTHPORN
-ART

-PHOTOSHOPBATTLES

-WRITINGPROMPTS

-DOCUMENTARIES

-UPLIFTINGNEWS

-HISTORY

-GADGETS

-DATAISBEAUTIFUL

-FUTUROLOGY

-NOSLEEP

-BOOKS

-CREEPY

-PHILIPPINES

-DIY

-PHILOSOPHY

-LISTENTOTHIS

-INTERNETISBEAUTIFUL

-ANNOUNCEMENTS

-BLOG

MORE

gamemaker
COMMENTS

Want to join? Log in or sign up in seconds.|


English

This post was submitted on 03 Nov 2014


1 point (67% upvoted)
https://redd.it/2l7c

remember mereset password


LOGIN
gamemaker
Subscribe20,942
95
Subreddit Guidelines
Community Spotlight
Monthly Challenge

Community

Resources

Other
GameMaker is software designed to make developing games easy and fun. It features a unique "Drag-
and-Drop" system which allows non-programmers to make simple games. Additionally, experienced
coders can take advantage of its built in scripting language, "GML" to design and create fully-featured,
professional grade games.
subreddit_guidelines()
Content that does not follow the subreddit guidelines is subject to deletion, so please become familiar with
them.
1. Content must be directly related to GameMaker
2. Content must be in English
3. Content must not be obscene, illegal, racist or offensive
4. Content must not use "click-bait" titles, shortened links, or solicitation
5. Content must demonstrate a previous effort and research before posting and must provide
adequate detailed information
6. Show respect to all users of the subreddit and have patience with other users when providing
help
7. Promotional content must contribute to the community
8. Technical support requests are to be directed to YoYo Games Support
9. Content must be appropriately flaired at the time of submission
community()

Conversation
/r/gamemaker sponsors three chat-rooms: IRC, a Discord server, and a Slack team. Join in the
conversation, get help with any issues you might have and connect with your fellow developers!

Scheduled content
Schedule Content Summary

Ask questions, ask for assistance or ask about something else


Monday Quick Questions
entirely.

Game Design &


Wednesday Discuss game design and game development.
Development

Friday Feedback Friday Play games and lend feedback

Saturday Screenshot Saturday Share the latest pictures and videos of your game

Are you in need of motivation? Then take a stab at these


Monthly Monthly Challenge
creative challenges.

Shining a light on high-quality projects and resources created


Bi-weekly Community Spotlight
by our community.
gm(48)
For more than 3 years, the tight-knit community of /r/gamemaker has run the game jam gm(48) for
GameMaker developers of all ages and experience levels. The gm(48) is a casual, fun game jam that
helps you to learn and grow as a developer.
The next gm(48) will take place on July 15, 2017.
created by Clay_Pigeona community for 8 years

message the moderators


MODERATORS
Cajoled
AutoModerator
HappyHonuRohbert
oLabRattoothsoup
hypnozizziz
It just doesn't work, you know?damimp
about moderation team

discussions in r/gamemaker
<>

Tutorial - Day/Night Cycle with Surfaces

This is an archived post. You won't be able to vote or comment.

1
Solved!Been making a fairly simple quiz game, but have hit a wall. Would love
some help! self.gamemaker
Submitted 2 years ago * by bassman2112
Hey all =)
Sadly I shan't be posting my project file because I'm using JCHTML5, and
don't feel right putting all of his work up for free; but I'll be copying in a lot
of the code I'm using.
So the simple premise: I'm making a game for little kids to help them with
music. They will hear a rhythm being played, and will answer "Yes" or "No"
as to whether it was the one they heard. I plan on doing a more interesting
version of this later where they hear the sound, and then pick one of three
rhythms; but simple for now.
So I have it working with one level - here is the code that makes it work:
First-off, when the room is created, it runs this script (this will later be a
script run by a level selector):

//Initialize variables
global.level_number = 1;
global.correct_answers = 0;
global.question_number = 1;
global.clickable = 1

//Create Questions
global.level1_questions =
ds_list_create();

ds_list_add(global.level1_quest
ions, obj_level1_1);
//And so on
ds_list_add(global.level1_quest
ions, obj_level1_12);

//Randomizes questions
ds_list_shuffle(global.level1_q
uestions);
//Referencing questions for
Level 1, providing answer in
"Yes" or "No" as string
//Answers currently all set to
"Yes" for debugging purposes
global.level_answers =
ds_map_create();

ds_map_add(global.level_answers
, obj_level1_1, "Yes");
//And so on
ds_map_add(global.level_answers
, obj_level1_12, "Yes");

//Drawing on the gamespace


global.current_question =
instance_create (30,30,
ds_list_find_value(global.curre
nt_level_questions, 0));
global.yes_button =
instance_create (173, 323,
obj_btn_yes);
global.no_button =
instance_create (483, 323,
obj_btn_no);

instance_create(0, 0,
obj_current_question);
instance_create(0, 0,
obj_correct_answers);

So the tl;dr of that is I initialize my global variables, create a ds_list that


contains all of the objects with my visual & audio questions, randomize it,
and then associate the correct answers with a ds_map. After that I draw
everything into the room with the "instance create" commands. The last two
lines are what I'm using to draw my text of "Current Question" and "Correct
Answers" for tracking progress.
Cool.
Now to make it interactive I have my "Yes" and "No" buttons. They are both
the same, but with "Yes" and "No" switched up in one of the fourth lines.
Here is the code:
On Left Click:

//Checks if user has clicked


already
if (global.clickable == 1) {

if(global.level_answers[?
global.current_question.object_
index] == "Yes"){

//Disables
clicking/extra input
global.clickable = 0;

//Print that we've made


progress
global.correct_answers
+= 1;
global.question_number
+= 1;

//Play a sound saying


"You're right!" (Not yet
created)

//audio_play_sound(snd_correct)

//Go to alarm
alarm[0] = 30;

}
else{

//Disables
clicking/extra input
global.clickable = 0;

global.question_number
+= 1;

//Play a sound saying


"Try again!" (Not yet created)

//audio_play_sound(snd_incorrec
t)

//Go to alarm
alarm[0] = 30;
}

alarm[0]

//Delete that question from


its position on the list

ds_list_delete(global.current_l
evel_questions, 0);

//Delete the current instance


with
(global.current_question){
instance_destroy()
}

//Spawn the next question


global.current_question =
instance_create (30,30,
ds_list_find_value(global.curre
nt_level_questions, 0));

//Re-enables the cursor,


again allowing input
global.clickable = 1;

This all works wonderfully. It keeps track of score, you can't slam on the
button 100 times and suddenly be at "question 100," et cetera.
The problem I'm having is that I want to have multiple levels; but I can't
figure out a way to properly move between them while keeping my button
code the same. I don't want to have to make an "obj_button_yes_level1"
that spawns when the user picks Level 1, for example. I just want to make
"obj_button_yes" so I'm not eating up valuable resource space.
I've tried this a few ways, neither of which has worked.
Firstly I made something that looked like this:

global.current_question_level =
string("global.level"+string(gl
obal.level_number)
+"_questions")

This one would spawn the initial question, as I wanted; but as soon as I
gave a single answer it would end - saying it had finished all of the
questions. I don't quite get why, tbh.
The second way I tried was this:

//Init
global.array_level_questions[25
] = 0

//Usage
global.current_level_questions
=
global.array_level_questions[gl
obal.level_number]

//Creating the questions for


Level 1
global.array_level_questions[1]
= global.level1_questions;

global.level1_questions =
ds_list_create();
ds_list_add(global.level1_quest
ions, obj_level1_1);

//later I would call


global.current_level_questions
in place of
global.level1_questions

But this just straight-up doesn't work. It makes me think that, perhaps, GML
doesn't use arrays in the way I'm used to with other C-based languages?
Any guidance would be extremely appreciated =)
Sorry for the long post!!!

Edit:
Still broken.
So as talked about in the comments, I added a For loop to initialize the
variables; but that didn't help. I'll copy my starting script, as well as the
error it displays:

//Initialize variables

global.level_number = 1;
global.correct_answers = 0;
global.question_number = 1;
global.clickable = 1;
//Init
for (i = 0; i < 25; i++){

global.array_level_questions[i]
= 0;
}

//Creating the questions for


Level 1
global.level1_questions = 0;
global.array_level_questions[1]
= global.level1_questions;

global.level1_questions =
ds_list_create();
ds_list_add(global.level1_quest
ions, obj_level1_1);
//Et cetera
ds_list_add(global.level1_quest
ions, obj_level1_12);

//Randomizes questions
ds_list_shuffle(global.level1_q
uestions);

//Referencing questions for


Level 1, providing answer in
"Yes" or "No" as string
//Answers currently all set to
"Yes" for debugging purposes
global.level_answers =
ds_map_create();
ds_map_add(global.level_answers
, obj_level1_1, "Yes");
//And so on
ds_map_add(global.level_answers
, obj_level1_12, "Yes");

//Usage
global.current_level_questions
= 0;
global.current_level_questions
=
global.array_level_questions[gl
obal.level_number];

//Drawing on the gamespace


global.current_question =
instance_create (30,30,
ds_list_find_value(global.curre
nt_level_questions, 0));
global.yes_button =
instance_create (173, 323,
obj_btn_yes);
global.no_button =
instance_create (483, 323,
obj_btn_no);

instance_create(0, 0,
obj_current_question);
instance_create(0, 0,
obj_correct_answers);

This is the error I get:

Creating instance for non-


existing object: -4
at
gml_Script_scr_level1_questions
answers (line 58) -
global.current_question =
instance_create (30,30,
ds_list_find_value(global.curre
nt_level_questions, 0));

That line previously worked when I was not using the arrays, so I'm not sure
why it is not anymore D=
Again, any help would be massively appreciated
13 comments
share
report
all 13 comments
sorted by:
best
[]SaladLizard 1 point 2 years ago
Initialising that array like this:
global.array_level_questions[25]=0;
is only going to set the 25th value to 0, so the rest of the values are
undefined. Use a loop to define them all.
permalink
embed
[]bassman2112 S 0 points 2 years ago
[ ]

I assumed it would create and initialize everything up until that point haha -
guess GML doesn't work exactly like other C-based languages.
I'm on my phone, but would you do a for loop? Something like this?
For (i = 0; i > 25: i++) {
global.array_level_questions[i] = 0
}
Otherwise, does it seem like my syntax is reasonable?
Thanks a ton
permalink
embed
parent
[]TheWinslow 2 points 2 years ago
That for loop will never run but that is the basic idea.
permalink
embed
parent
[]bassman2112 S 1 point 2 years ago*
[ ]

I'm curious as to why the for loop wouldn't run =)


Edit: Oh because < rather than >, right?
permalink
embed
parent
[]TheWinslow 1 point 2 years ago
Yup! i > 25 would always return false.
permalink
embed
parent
[]bassman2112 S 1 point 2 years ago
[ ]

Thanks =D
Sorry to bug, but would you be willing to take a look at my edit and see if
you have any advice? =)
permalink
embed
parent
[]TheWinslow 1 point 2 years ago

//Creating the
questions for
Level 1
global.level1_qu
estions = 0;
global.array_lev
el_questions[1]
=
global.level1_qu
estions;

global.level1_qu
estions =
ds_list_create()
;
ds_list_add(glob
al.level1_questi
ons,
obj_level1_1);
//Et cetera
ds_list_add(glob
al.level1_questi
ons,
obj_level1_12);

//Randomizes
questions
ds_list_shuffle(
global.level1_qu
estions);

I'm wondering why you add global.level1_questions to the level questions


array before you initialize the list. As is, the array is initialized with
global.array_level_questions[1] = 0 as global.level1_questions = 0 at that
point. If the rest of the levels' questions are added like this, you would end
up with an array where every value is 0.
permalink
embed
parent
[]bassman2112 S 1 point 2 years ago
[ ]

I typed up a big response as to why I made those changes; but thought


"wait, if it's an execution order thing..." and moved
the global.current_level_questions =
global.array_level_questions[global.level_number]; line
below global.array_level_questions[1] = global.level1_questions; and
it worked. This is weird to me because I thought the former line was
initializing the latter, but I guess I misinterpreted how I was coding it.
It now appears like this, and works

//Init
for (i = 0; i
< 10; i++){

global.array_
level_questio
ns[i] = 0;
}
//Usage
global.curren
t_level_quest
ions = 0;

//-----------
-------------
-------------
-------------
-------------
----------//
//
LEVEL 1
QUESTIONS
//
//-----------
-------------
-------------
-------------
-------------
----------//

//Init
global.level1
_questions =
0;

//Creating
questions for
Level 1

global.level1
_questions =
ds_list_creat
e();
ds_list_add(g
lobal.level1_
questions,
obj_level1_1)
;
//Etc
ds_list_add(g
lobal.level1_
questions,
obj_level1_12
);
//Randomizes
questions
ds_list_shuff
le(global.lev
el1_questions
);
//Inserts
Level 1
questions
into proper
Array spot

global.array_
level_questio
ns[1] =
global.level1
_questions;
global.curren
t_level_quest
ions =
global.array_
level_questio
ns[global.lev
el_number];

permalink
embed
parent
[]mixmax2 1 point 2 years ago
i < 25
permalink
embed
parent
[]bassman2112 S 1 point 2 years ago
[ ]

Er, right, my bad haha


I'll give this a shot tomorrow and edit how it works =)
permalink
embed
parent

[]I find your lack of pointers disturbingAtlaStar 1 point 2 years ago


So the first comment is wrong in the sense that they didn't look at your code
to see that you are initializing the values inside the array using
global.array_level_questions[1] = global.level1_questions;
the issue in the prior to edited second attempt code is that you are
initializing the data inside the array after defining the usage

//Usage
global.current_level_questions
=
global.array_level_questions[gl
obal.level_number]

meaning global.current_level_questions is undefined since


global.array_level_questions[] has every index except 25 undefined before
you assignment occurs...so basically from what I am seeing in that code
block and all the edited code blocks, you are creating the map after
assigning the array indexes to 0, meaning they will never store the maps Id
so when you use instant create and search your maps and lists using the id
stored, it is using the id 0 since you are initializing everything in the
incorrect order, and that is the id of a non-existing data structure...really
hope what I said makes sense since I am in a hurry and can't go into more
details right now
permalink
embed
[]bassman2112 S 1 point 2 years ago
[ ]

Thanks for the reply! In another response I actually fixed it =D I wasn't


aware that setting the array with the [25] didn't set all of those before it,
which was my mistake. I ended up using a for loop to set all of those before
it, and it worked well =)
permalink
embed
parent
[]I find your lack of pointers disturbingAtlaStar 1 point 2 years ago
Yeah I read it afterwards, but the loop setting the array's variables to 0 isn't
what fixed the problem, because the first code you were using was
initializing the array's elements to 0, hence why the second test using a for
loop didn't work...what happened is you were making
global.array_level_questions[x] = global.levelx_questions
which at the time were zero, then afterwards making global.levelx_questions
equal a newly created list. at that point global.array_level_questions[x] = 0
and global.levelx_questions = the new list id. the last code you posted
before the first edit would have worked if you had first made your
levelx_questions equal a new list, then added that value to the array, and
even in C based languages variables themselves don't point to another
variables data, they simply copy it at that instant and do not pay attention
to changes that come later...hence why i said what I did...not just because I
wanted to help you find a quick solution, but also so you would understand
the language as well as other languages better and how assignment happens
top down... simply put if x = 0, y = 1 then you make x = y and y = 2, x will
= 1 and y = 2 unless you do y=2 x=y in which case both will equal 2...basic
programming my friend
permalink
embed
parent

Log in or Sign up
Forums>GameMaker>Programming>
1. This site uses cookies. By continuing to use this site, you are agreeing to
our use of cookies. Learn More.

Multiple Choice Question


Discussion in 'Programming' started by pongsifu, Jul 4, 2016.

Tags:
multiple choice

quiz

trivia
1.
pongsifuMember
Joined:
Jul 4, 2016
Posts:
2
It is simple enough to create a single multiple choice question, but the problem I'm having is
figuring out the most efficient way to make a multiple choice question pull from a pool of many
questions and corresponding answers.

So basically I'll have a question box and 4 answer boxes and I'll have a pool of questions and
those questions will each have 4 possible answers attached to them. Any help/suggestions in
the best way to do this would be appreciated as I'm having a hard time thinking of the best
solution for this. The methods I am thinking of I am sure are a lot more work than are actually
required and I know some of you will have a much more elegant solution.
pongsifu, Jul 4, 2016
#1

2.
jo-thijsMember
Joined:
Jun 20, 2016
Posts:
1,824
Hi and welcome to the GMC!

I'd suggest creating a ds_list, adding arrays to it like this:


Code:
mcQuestions = ds_list_create();
var a;
a[0] = "Question 1";
a[1] = "Answer 1 to question 1";
a[2] = "Answer 2 to question 1";
a[3] = "Answer 3 to question 1";
a[4] = "Answer 4 to question 1";
a[5] = 4 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
a[0] = "Question 2";
a[1] = "Answer 1 to question 2";
a[2] = "Answer 2 to question 2";
a[3] = "Answer 3 to question 2";
a[4] = "Answer 4 to question 2";
a[5] = 2 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
...
ds_list_shuffle(mcQuestions);

You can then keep selecting the first array in the list, save it to a temporary variable and read its
question and answers.
Then delete the first array from the list and you won't select the same question again.

Do remember though to delete the ds_list at time.


jo-thijs, Jul 4, 2016
#2

3.
Ctl-FMember
Joined:
Jul 1, 2016
Posts:
59
I'd create a map to represent a question. And a list to hold the maps.
Code:
question = ds_map_create()
question[?"question"] = "what is the answer?"
question[?"ans1"] = "42"
question[?"ans2"] = "yes"
question[?"ans3"] = "idk"
question[?"ans4"] = "beats me"
question[?"correct"] = "ans1"

questions = ds_list_create()
ds_list_add(questions, question)
ds_list_mark_as_map(questions, ds_list_size(questions)-1)

Then when you want to display things and check the correct answer its easily layed out.

But there are many many ways to lay it out.


Ctl-F, Jul 4, 2016
#3

4.
pongsifuMember
Joined:
Jul 4, 2016
Posts:
2
Thank you for the help, this is pretty much exactly what I needed. It has been a long long time
since I have used GM so I'm basically re-learning from scratch (because I forgot everything). I
watched some videos on ds_lists and ds_maps and I understand how to create the list itself and
its purpose, but I'm still not sure how to actually pull from the list.
What I mean is, say I have a room with 4 blank answer boxes and 1 blank question box, I don't
know how to actually get the list to populate the question/corresponding answers into their
boxes. I set the boxes as their own objects but it may be better to simply use coordinates to line
up on top of the boxes. Either way, I'm feeling kind of silly for not being able to figure out
something that should be simple (I chose this as a project because I wanted to try something
easy to help re-learn everything). Previously I had relied on the pre-made "action" things GM is
known for rather than using code and I told myself I'd try to use code more instead...but maybe
that isn't the best idea, as I really am terrible at coding.
pongsifu, Jul 5, 2016
#4

5.
jo-thijsMember
Joined:
Jun 20, 2016
Posts:
1,824
Nah, coding is fun!
There are many applications by now that will convert D&D actions to code,
so if you know how to do something in D&D and want to know how it could look like in code,
you can use one of those tools to learn the syntax.

Examples are:
https://forum.yoyogames.com/index.php?threads/drag-and-drop-to-gml-converter.138/
and:
http://www.softpedia.com/get/System/File-Management/D-and-D-to-GML-Converter.shtml

Anyway, as to get the information from the array to the other objects,
you could have 1 object pick an array from the list and store it in a global variable.
The other objects can then read the information from the global variable.

(You'll also have to change mcQuestions to global.mcQuestions in my previous post


and the code in my previous post would be executed before any of the codes below)

So, you would have a controller object (mayby the box with the question in it)
with this in its create event:
Code:
global.qInfo = ds_list_find_value(global.mcQuestion, 0);
ds_list_delete(global.mcQuestions, 0);

And you could then use the instance creation code (room editor, right click on instance,
"creation code") to assign every button object its status:
Code:
// Button with first answer
myAnswer = global.qInfo[1];
amICorrect = global.qInfo[5] == 1;

You would replace 1 with 2 here for the button with the second answer and so on.
jo-thijs, Jul 5, 2016
#5
Charliebailey03 likes this.

6.
Charliebailey03Member
Joined:
Mar 28, 2017
Posts:
70
can anyone tell me what a map is?
Charliebailey03, May 7, 2017
#6

7.
jo-thijsMember
Joined:
Jun 20, 2016
Posts:
1,824
Charliebailey03 said:
can anyone tell me what a map is?

A map is sometimes also called a dictionary.


In a dictionary, you've got a list of words with their corresponding meaning.
The words are ordered alphabetically, so you can efficiently lookup words and find their
respective meanings.

Maps are datastructures that behave similarly.


They keep track of "keys" (words in the case of a dictionary) and their corresponding values
(meanings in the case of a dictionary).
Maps then allow to find the value corresponding to any key very quickly.

In GameMaker, maps look like lists, but have as big difference that lists only accept numbers as
indices/keys and need a value at every index they have.
Maps however accept strings as indices/keys too.

For example, this doesn't make sense:


Code:
show_message(array["index"]);

but this does:


Code:
show_message(map[?"index"]);

jo-thijs, May 7, 2017


#7
8.
hipo99Member
Joined:
Apr 1, 2017
Posts:
2
jo-thijs said:
Hi and welcome to the GMC!

I'd suggest creating a ds_list, adding arrays to it like this:


Code:
mcQuestions = ds_list_create();
var a;
a[0] = "Question 1";
a[1] = "Answer 1 to question 1";
a[2] = "Answer 2 to question 1";
a[3] = "Answer 3 to question 1";
a[4] = "Answer 4 to question 1";
a[5] = 4 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
a[0] = "Question 2";
a[1] = "Answer 1 to question 2";
a[2] = "Answer 2 to question 2";
a[3] = "Answer 3 to question 2";
a[4] = "Answer 4 to question 2";
a[5] = 2 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
...
ds_list_shuffle(mcQuestions);

You can then keep selecting the first array in the list, save it to a temporary variable and read its question and
answers.
Then delete the first array from the list and you won't select the same question again.

Do remember though to delete the ds_list at time.


Click to expand...

how can i apply it on my project ?? Please, guide me step by step..


hipo99, Aug 6, 2017
#8
(You must log in or sign up to reply here.)

Share This Page


Forums>GameMaker>Programming>
Forums
o Recent Posts
YoYo Games
Helpdesk
Dark Skin (Default)
Help
Home
Top
RSS
Terms and Rules
Privacy Policy
Forum software by XenForo 2010-2017 XenForo Ltd.
mcQuestions = ds_list_create();
var a;
a[0] = "Question 1";
a[1] = "Answer 1 to question 1";
a[2] = "Answer 2 to question 1";
a[3] = "Answer 3 to question 1";
a[4] = "Answer 4 to question 1";
a[5] = 4 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
a[0] = "Question 2";
a[1] = "Answer 1 to question 2";
a[2] = "Answer 2 to question 2";
a[3] = "Answer 3 to question 2";
a[4] = "Answer 4 to question 2";
a[5] = 2 /* correct answer */;
ds_list_add(mcQuestions, a);
a = 0;
...
ds_list_shuffle(mcQuestions);

You might also like