Picking Up and Droping a Single Object

TyGamess

Member
I’m new(ish) to GMS2 but new to the website so I’m sorry if I’m positing this in the wrong area. But I’m having some problems woth “Picking up and Droping a single item” I’m making a game where you can only carry one item at a time. So I made the code and my character will pick one up but can pick up multiple items, and won’t put them down. Yet when I use a while statement, my game crashes. Idk what to do in this situation, I’ve tried 4 different ways and all failed.
 

jo-thijs

Member
I’m new(ish) to GMS2 but new to the website so I’m sorry if I’m positing this in the wrong area. But I’m having some problems woth “Picking up and Droping a single item” I’m making a game where you can only carry one item at a time. So I made the code and my character will pick one up but can pick up multiple items, and won’t put them down. Yet when I use a while statement, my game crashes. Idk what to do in this situation, I’ve tried 4 different ways and all failed.
Hi and welcome to the GMC!

Don't worry, you're posting this question at the right location.

Try the following approach:
Have a variable in the character object that represents which item the player is currently holding and set it to the value noone when the character isn't holding any items.
Then have some code in the character's step or begin step event that teleports the item to the character.

For example, you could create a parent object obj_carriable_item_parent that is the parent of every item object that the character can carry.
Then in the create event of the character object, you initialize the variable carrying_item to noone:
GML:
carrying_item = noone;
In the keyboard pressed event of the character object for the key you use to pick up or drop an item, you update the variable:
GML:
if carrying_item == noone {
  carrying_item = other.id;
} else {
  carrying_item = noone;
}
Finally, in the step event of the character object, you teleport the item to the character:
Code:
if carrying_item != noone {
  carrying_item.x = x;
  carrying_item.y = y;
}
Let us know how that goes and if you have any questions, feel free to ask them.
 

TyGamess

Member
This all makes sence, thank you for the help and understanding, yet how is the player checking to see what item it’s about to pick up? In the code you had me try my character will not pick up the item. My guess is that even tho I pressed my pick up item key, it still domestically know what exactly it’s picking up. So I wanted to ask is there a way like a list or something, for me to classify a Specific group of objects as items? That way I could do
Instance_place(x,y,itemID)? I don’t want to have to make 8 or 10 different insurance_place()’s because it will not be easily expanded upon
 

TyGamess

Member
So it’s not picking up the item, it’s picking up itself, what do I do at this point? The instance for obj_Stick is 100002 but when I pick it up it says I’m carrying 100000 witch is obj_Player.
 

jo-thijs

Member
Hi and welcome to the GMC!

Don't worry, you're posting this question at the right location.

Try the following approach:
Have a variable in the character object that represents which item the player is currently holding and set it to the value noone when the character isn't holding any items.
Then have some code in the character's step or begin step event that teleports the item to the character.

For example, you could create a parent object obj_carriable_item_parent that is the parent of every item object that the character can carry.
Then in the create event of the character object, you initialize the variable carrying_item to noone:
GML:
carrying_item = noone;
In the keyboard pressed event of the character object for the key you use to pick up or drop an item, you update the variable:
GML:
if carrying_item == noone {
  carrying_item = other.id;
} else {
  carrying_item = noone;
}
Finally, in the step event of the character object, you teleport the item to the character:
Code:
if carrying_item != noone {
  carrying_item.x = x;
  carrying_item.y = y;
}
Let us know how that goes and if you have any questions, feel free to ask them.
So it’s not picking up the item, it’s picking up itself, what do I do at this point? The instance for obj_Stick is 100002 but when I pick it up it says I’m carrying 100000 witch is obj_Player.
Oh wow, horrible mistake on my part, I forgot the collision code.
Change the keyboard event in my previous suggestion to:
GML:
if carrying_item == noone {
    with obj_carriable_item_parent {
        if place_meeting(x, y, other) {
            other.carrying_item = id;
            break;
        }
    }
} else {
    carrying_item = noone;
}
 
Top