Legacy GM Need help putting items in inventory, using them on object

A

Ashus

Guest
I'm trying to have multiple doors like in Zelda where they need silver keys but can be unlocked in any order. I'm relativity new to gamemaker but understand the concepts. I just can't figure out how to 1: put object in inventory/stack it 2:remove it and 3: cause the door to unlock. If anybody could help I'd really appreciate it.
 
R

Ryzzax

Guest
Well, I'm not a pro programmer and everything, but I might be able to help you...

I guess one way to "stack" your object would be with a global variable. Example: global.key = 0; Also, if you don't want the player to have too many keys, just do that: if (global.key >= 3) global.key = 3; (3 is just an example, put 5,6,7,etc if you want)

Make it that when you pick up the object, that variable upgrades by 1. Just like that: global.key = global.key + 1; Also, when you unlock a door: global.key = global.key - 1;
So with that, you can stack objects. Of course, you will need to say that;
Code:
if (global.key > 0)
{
   global.key = global.key - 1;
}
You should put that code when the doors get unlocked, destroyed, something like that.

Now, to unlock doors with keys, say that you can only unlock doors when that global.key is greater than 0. You could put that in an key_press event or something,like'U'.
Also, you don't want to open those doors where ever you are in the game, so you will need an extra condition to it. For example, I suggest you to use collisions, like place_meeting or something like that.

Code:
if (global.key > 0)
{
//Unlock door commands, it could simply be to destroy
the doors, and add a condition to it, like when the player is close to the door, or with collision or something.
}
Hope that helps, I'm not pro though, so there are maybe better ways to do it, but since I saw nobody replied to you yet, I said to myself that I could try to help him and maybe give him some ideas how to do it. Also, huh, sorry if I don't give more explanations with collisions, you know, with that extra condition, since I'm not a pro, not a "beginner", but still, I will let that part to someone else more experienced than me to help you on that, I don't really want to give you some false explanations, if you know what I mean...
 
G

Gre_Lor12

Guest
Hello! Welcome to GMK!

Yes Ryzzax has the essence of what you want ;)

When you move over a key you then destroy the key and add one to the global.key:

with (obj_key) {instance_destroy()} //Destroy the key as it has been collected
global.key ++; //Add one to the key amount

Also when you collide with a door (I assume done with the collision event):

with (obj_door) {instance_destroy} //Destroy the door
global.key --; //Take a key away


I obviously do not know your code, but there is the base of it ;)
 
W

Wild_West

Guest
I'm trying to have multiple doors like in Zelda where they need silver keys but can be unlocked in any order. I'm relativity new to gamemaker but understand the concepts. I just can't figure out how to 1: put object in inventory/stack it 2:remove it and 3: cause the door to unlock. If anybody could help I'd really appreciate it.
Globals aren't essentially always the way to go, I mean if your inventory is gonna follow the player everywhere just make it persistent and use a 2D array to hold the info about the items you pick up.
If your game is gonna have save files the globals will have to be reset upon starting a new game so it can be a pain if there's too many, but that's just me.

This is the code I used for my inventory item collecting :

Collision Event with Item Parent

///Look through the items array


//check every Column
for( search = 0; search < 6; search ++; )
{

//If there isn't already an name assigned to column being checked, at row 0(where the name string goes)
if(menu_object.held_items[search,0] == "")
{
menu_object.held_items[search,0] = other.name;
menu_object.held_items[search,1] = other.object_index;
menu_object.held_items[search,2] += 1;

with(other){instance_destroy();}

break;
}
}
Then when you get to a door just use the same loop and check if you have any key items, subtract one from array index being searched, row 2 and set a different variable(probably should be an instance one) for the doors to unlocked.
Unless your doors aren't actually objects then I'm not sure off the top of my head how you'd unlock it but that seems the simplest way.
 
Top