• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Coding a combination lock

I am trying to create a 6-digit combination lock, where once the player puts in the correct combination, an item appears in another room. I have a Combo_dial_obj with 10 index images (0-9). That functions fine as far as scrolling through the numbers when clicked. However, I am having issues with the puzzle itself. I have Combo_Dial_Control with the following Step Event:

1 global.dial_1 = Combo_dial_obj(inst_A);
2 global.dial_2 = Combo_dial_obj(inst_B);
3 global.dial_3 = Combo_dial_obj(inst_C);
4 global.dial_4 = Combo_dial_obj(inst_D);
5 global.dial_5 = Combo_dial_obj(inst_E);
6 global.dial_6 = Combo_dial_obj(inst_F);
7
8 if
9 global.dial_1.image_index == 8 and
10 global.dial_2.image_index == 1 and
11 global.dial_3.image_index == 2 and
12 global.dial_4.image_index == 7 and
13 global.dial_5.image_index == 9 and
14 global.dial_6.image_index == 3 then
15 {
16 instance_create_layer(5,5,"Instances",Combo_Kill_obj)
17 room_goto(BookshelfRoom)
18 room_instance_add(BookshelfRoom,392,256,blueprint_in_box_obj)
19 }


The game fails to run, noting the following errors:
Compile Objects...
Error : gml_Object_Combo_Dial_Control_Step_0(4) : unknown function or script Combo_dial_obj : 19A54B7A
Error : gml_Object_Combo_Dial_Control_Step_0(5) : unknown function or script Combo_dial_obj
Error : gml_Object_Combo_Dial_Control_Step_0(6) : unknown function or script Combo_dial_obj
Error : gml_Object_Combo_Dial_Control_Step_0(7) : unknown function or script Combo_dial_obj
Error : gml_Object_Combo_Dial_Control_Step_0(8) : unknown function or script Combo_dial_obj
Error : gml_Object_Combo_Dial_Control_Step_0(9) : unknown function or script Combo_dial_obj

I don't know what Instance 19A54B7A is, as nothing in the room has that code. Also, it seems odd that lines 4-9 are mentioned, and not 1-6.
Anyway, I'm not quite sure what I am doing wrong. If someone could give me advice, I'd appreciate it.
Thanks,
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
You are using Combo_dial_obj as if it were a script. Is it? Or is it an object in the room? If it's an object I'm unclear what you are trying to achieve with that code, so could you elaborate?

Also, this function "room_instance_add" is only for adding instances to a room that your are NOT surrently in, and it is a permanent change. You should probably be using instance_create_layer in the actual room itself. ;)
 
You are using Combo_dial_obj as if it were a script. Is it? Or is it an object in the room? If it's an object I'm unclear what you are trying to achieve with that code, so could you elaborate?

Also, this function "room_instance_add" is only for adding instances to a room that your are NOT currently in, and it is a permanent change. You should probably be using instance_create_layer in the actual room itself. ;)
Combo_dial_obj is the actual object. On LMB Release it executes a code to click to the next number. I have 6 of these in the room. To solve the puzzle, I want the first to be at 8, the second to be at 1, and so on until the 6 digit code is correctly entered. I understand that I need as If Then statement that says Instance A needs to be Index 8, Instance B needs to be index 1, etc. I apparently just don't know how to make that happen.

The object I want created once the puzzle is solved is in a different room than the current one. Are you saying that room_instance_add would make that object always in that room, even after the player picks it up (essentially creating an unlimited number of them)?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, so, you need to get the instance IDs of each of the six instances of the object before you can actually change anything based on values they have. Personally, if the instances are being added in the room editor, then I'd assign the global variable in the Room Create Event of each instance, so that the variable gets the correct ID. You can find the room create event by double clicking on the instance to open the room editor instance properties window. So, you'd simply do:

GML:
global.dial1 = id;
for each one in the room editor (changing dial1 for dial2, etc... as you go through the instances). This will ensure that the global variables hold the correct instance ID. If you are creating the dial objects through code, then create funtion returns the ID of the instance, so -again - you can store it in a global variable, eg:

GML:
global.dial1 = instance_create_layer(x, y, "instances", Combo_dial_obj)
Are you saying that room_instance_add would make that object always in that room, even after the player picks it up (essentially creating an unlimited number of them)?
Yes, that is exactly what I'm saying. The function modifies the actual room asset, so after you call it, every time you enter that room the instance will be created, even if you call game_restart() (if you exit the game and start it again then the room will return to normal). You would be best creating a global variable that is true if the instance should be created or false if it shouldn't, or alternatively add a flag value to a save file and then check that when you enter the room (this is possibly better as it means you don't have hundreds of extra global variables for single things in the game).
 
Okay, so, you need to get the instance IDs of each of the six instances of the object before you can actually change anything based on values they have. Personally, if the instances are being added in the room editor, then I'd assign the global variable in the Room Create Event of each instance, so that the variable gets the correct ID. You can find the room create event by double clicking on the instance to open the room editor instance properties window. So, you'd simply do:

GML:
global.dial1 = id;
for each one in the room editor (changing dial1 for dial2, etc... as you go through the instances). This will ensure that the global variables hold the correct instance ID. If you are creating the dial objects through code, then create funtion returns the ID of the instance, so -again - you can store it in a global variable, eg:

GML:
global.dial1 = instance_create_layer(x, y, "instances", Combo_dial_obj)

Yes, that is exactly what I'm saying. The function modifies the actual room asset, so after you call it, every time you enter that room the instance will be created, even if you call game_restart() (if you exit the game and start it again then the room will return to normal). You would be best creating a global variable that is true if the instance should be created or false if it shouldn't, or alternatively add a flag value to a save file and then check that when you enter the room (this is possibly better as it means you don't have hundreds of extra global variables for single things in the game).
Wow. That was incredibly helpful. Thank you. The puzzle is working beautifully now. I'm still looking into flags. If I run into trouble with those, I'll post back, but I think I can probably figure them out. Also, was unaware of Room Creation Codes (Or Instance Creation Codes for that matter). Thanks for pointing them out to me. GMS2 has so much more than GM 8.1 (where I left off years ago).
Thank you very much!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Glad I could help!

I'm still looking into flags.
I would suggest that you use an ini-file to start with... You can have a section and key like section "Flags", and key "LockRoom", then set it to true/false as required. An alternative that isn't as user-friendly (ie: easy to hack) is to use a buffer where each byte would be a true/false flag for the different things in the game that require it. It's slightly more complicated to set up, but gives a more secure save file.
 
Top