Sunteți pe pagina 1din 19

jump to content

MY SUBREDDITS

POPULAR

-ALL

-RANDOM

ASKREDDIT

-FUNNY

-PICS

-NEWS

-TODAYILEARNED

-AWW

-WORLDNEWS

-GAMING

-GIFS

-VIDEOS

-MOVIES

-SHOWERTHOUGHTS

-MILDLYINTERESTING

-IAMA

-JOKES

-TELEVISION

-OLDSCHOOLCOOL

-TIFU

-LIFEPROTIPS

-PERSONALFINANCE

-SCIENCE

-SPACE

-MUSIC

-EARTHPORN

-PHOTOSHOPBATTLES

-FOOD

-SPORTS

-NOTTHEONION

-TWOXCHROMOSOMES

-EXPLAINLIKEIMFIVE

-FUTUROLOGY
-DATAISBEAUTIFUL

-ASKSCIENCE

-WRITINGPROMPTS

-DOCUMENTARIES

-ART

-UPLIFTINGNEWS

-NOSLEEP

-GETMOTIVATED

-BOOKS

-PHILIPPINES

-HISTORY

-DIY

-LISTENTOTHIS

-GADGETS

-CREEPY

-PHILOSOPHY

-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
Subscribe21,008
194
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

Monday Quick Questions Ask questions, ask for assistance or ask about something
Schedule Content Summary

else 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 commu nity fo r 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
<>

41 16 comments

Fatty Rabbit Hole, a game I made with GameMaker, is now on Steam!

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

S-ar putea să vă placă și