Unique identifier for objects that get destroyed?

I'm currently making a dungeon crawler and working on the inventory system. On my map, I have two trash cans in different locations, but both are technically using the same object. As the character walks through the dungeon, it will create an instance of a trash can object if detected on the map and destroy the instance of that trash can object when walking too far away from it (because it will be out of view).

I have another object that's basically a list/directory of the trash cans and the items within them like so:
Code:
trash_can_1 = ds_list_create();
ds_list_add(trash_can_1, "Item A");

trash_can_2 = ds_list_create();
ds_list_add(trash_can_2, "Item B");
What I'm having an issue with is upon clicking a trash can, I want that specific trash can to only grab the item from the trash_can_1 and if I click a different trash can, that one should only grab the item from trash_can_2. I can't use the instance id with the way we're creating and destroying the trash cans because the id keep changing.

In my trash can object, I have a Left Pressed event that takes the item from the trash can and adds it to the player's inventory. (Obviously, the example below is only grabbing from trash_can_1 at the moment.)
Code:
var item_in_trash_can = ds_list_find_value(trash_can_1, 0); // grabs "Item A"
ds_list_add(player.inventory, item_in_trash_can); // adds "Item A" to player's inventory list
ds_list_clear(trash_can_1); // empties trash can
How do I make it so that each trash can instance grabs a specific item?
 
Last edited:

TheouAegis

Member
Well for starters you are already going about naming your list improperly because your naming convention is clearly compatible with an array.

Code:
trash_count = 2; //or whatever
var i=0;
repeat trash_count
{ trash_can[i++] = ds_list_create(); }
Then all you need to do when you create a trash can is store inside of it what index in that array to use. so trash can one will use index0, trash can two will use index 1 and so forth. Then you would be able to fetch the list ID through that array.
 
Well for starters you are already going about naming your list improperly because your naming convention is clearly compatible with an array.

Code:
trash_count = 2; //or whatever
var i=0;
repeat trash_count
{ trash_can[i++] = ds_list_create(); }
Then all you need to do when you create a trash can is store inside of it what index in that array to use. so trash can one will use index0, trash can two will use index 1 and so forth. Then you would be able to fetch the list ID through that array.
Good suggestion with the loop and will refactor once I get the main problem addressed. But I'm not quite sure how I would specify an index to use upon creation of the trash can object since they're all using the same base object.

The main problem is I have a trash can obj (let's call it obj_trash_can) that gets created and destroyed when the player gets closer or further away from the trash can. This object has a Left Pressed event that is supposed to take the item inside it by somehow looking up in a directory, grabbing the item from the correct list, emptying the list, and adding it to the inventory. I can't think of a way to look up a particular instance of obj_trash_can in the directory.

The screenshot below, I have a map that a player navigates around. If it detects a trash can 3 cells ahead, it will render the trash can. (This map is actually hidden behind the floor and wall sprites, so is not currently visible to the player.) The player is only able to click the trash can when he/she is standing on the cell the trash can is on.

upload_2019-8-8_23-23-53.png

Trash can 3 cells in front of the player (obj_trash_can_row_3):

upload_2019-8-8_23-37-2.png

Trash can 2 cells in front of the player (obj_trash_can_row_2):

upload_2019-8-8_23-37-36.png

(continued...)
 

Attachments

Last edited:
Trash can 1 cells in front of the player (obj_trash_can_row_1):

upload_2019-8-8_23-40-29.png

Trash can on the same cell as the player (obj_trash_can_row_0):

upload_2019-8-8_23-41-48.png

Each one of the trash cans at different distances has its own object with an associated sprite. obj_trash_can_row_0 (trash can on the same cell as the player) is the one with the Left Pressed event that allows a player to pick up the item inside. With each step closer to the trash can, the previous object is destroyed and a new one is created. (According to the map, this trash can is the one in the upper left corner.) I want this trash can object to contain "Item A". I want the trash can in the lower right to contain "Item B". I'm just unsure how to reference a particular instance of an object that is used multiple times to a directory to contain different items. Using the built-in variable id doesn't work because the object keeps being created and destroyed causing the id to increment, so I don't know how I would uniquely identify an instance of an object.
 
When standing on the cell the trash can is on, turning (or walking) will destroy the trash can (along with the entire previous view) and render the new view. So even turning left to face the trash can will destroy the previous one and create a new instance.

upload_2019-8-9_0-21-7.png

Turning left to face the direction the player came from initially.

upload_2019-8-9_0-23-0.png
 

Homunculus

Member
But I'm not quite sure how I would specify an index to use upon creation of the trash can object since they're all using the same base object.
You just need to store the instance on creation in a local variable and assign the index, some thing like this:
Code:
var trash_can = instance_create_layer(...);
trash_can.index = 1; //assign this instance to list 1
I think that there's probably a better solution though. Since you need to know where the trash cans are for creating them, why no store the specific trash can ds_list inside the object you use in your map (the instances you have in the image below)?

 

TheouAegis

Member
Why not just deactivate trash cans instead of destroying them? They're not going to take up that much memory.

Wait, are you saying the little trash cans in the map are created and destroyed repeatedly? Or are you saying the trash cans in the 3D view are created and destroyed? The trash can in the 3D view is irrelevant aside from determining when the player is actually interacting with the trash can. The only trash can that really matters is the one in the map, and that should never be destroyed. Just assign an index to the map trash can. Then all you need ot do is read the map trash can's index when interacting with the 3D trash can, since the player arrow is going to be in the same cell as the trash can at that point. Even if you wanted to have the 3D trash can holding all the data, you'd just have the map trash can's index assigned to the 3D trash can. But again, I'd just have everything handled at the map level, not the 3D level.
 
Wait, are you saying the little trash cans in the map are created and destroyed repeatedly? Or are you saying the trash cans in the 3D view are created and destroyed? The trash can in the 3D view is irrelevant aside from determining when the player is actually interacting with the trash can. The only trash can that really matters is the one in the map, and that should never be destroyed. Just assign an index to the map trash can.
Yup, you make some good points. The trash cans on the map are always there, but the 3D view trash cans are the one that keeps creating and destroying. The 3D trash cans are created with instance_create_depth, but the trash can objects (obj_map_trash_can) on the map are just simply dragged into the room itself. How do I assign different indices though? I'm assuming inside the create event in obj_map_trash_can, we assign the index, but how I'm not sure how to assign different indices for each instance?

Then all you need to do is read the map trash can's index when interacting with the 3D trash can, since the player arrow is going to be in the same cell as the trash can at that point. Even if you wanted to have the 3D trash can holding all the data, you'd just have the map trash can's index assigned to the 3D trash can. But again, I'd just have everything handled at the map level, not the 3D level.
Does that mean that I need to keep track of what the player's current coordinates are and check to see if they are standing on the same cell as a trash can? Sounds like in order to do this, I would need to have an object that keeps track of all the trash cans' coordinates. The only problem I see is that if I was to rearrange the map and move the trash cans around, I'd also have to update all the trash cans' coordinates in the object as well, which can be tedious, but as long as it works, I'm fine with doing that.

With that approach, sounds like in the Left Pressed mouse event, we grab the player's current coordinate, then check the object that stores all the trash can coordinates to see if it happens to be standing on a cell the trash can is on and if the coordinates match up, then grab the item. That's my understanding and thinking about it more, that makes pretty good sense, but feel free to correct me if I'm wrong or if there's a better approach. This way, I don't believe we need to even assign indices.
 
Last edited:

Homunculus

Member
All instances are independent. If you define some variables in the create event (or any other event) each instance has its own independent copy of the variable. This means that creating a ds_list in the create event of obj_map_trash_can results in every instance having its own ds_list (or index, or whatever).

This is really basic stuff, I suggest reading about instance variables and instances vs objects in the manual, you won't get very far without knowing the difference between the two.
 
All instances are independent. If you define some variables in the create event (or any other event) each instance has its own independent copy of the variable. This means that creating a ds_list in the create event of obj_map_trash_can results in every instance having its own ds_list (or index, or whatever).

This is really basic stuff, I suggest reading about instance variables and instances vs objects in the manual, you won't get very far without knowing the difference between the two.
Cool, thanks for the suggestion. I feel like doing something like this in the create event would give all trash cans the same item though, when I want specific ones to have specific items.
Code:
items = ds_list_create();
ds_list_add(items, "Item A");
I'll do some reading on those topics to get a better understanding and will post if I have more questions. I was just learning as I go :D
 

Homunculus

Member
I feel like doing something like this in the create event would give all trash cans the same item though, when I want specific ones to have specific items.
Not at all, running
Code:
items = ds_list_create();
in the create event of obj_map_trash_can will create a new list for every instance and assign its id to the items variable of that specific instance.

Imagine if all instances of the same object followed your logic: all instances would share the same x, y, speed, direction etc... Instance variables have to be independent on a per instance basis, it would make no sense otherwise.
 
Imagine if all instances of the same object followed your logic: all instances would share the same x, y, speed, direction etc... Instance variables have to be independent on a per instance basis, it would make no sense otherwise.
True, very good point. I'll read more into instance variables and see if I can figure out a way to set the items separately. One way that hypothetically should work is in the Left Pressed event for the 3D trash can object, grab the player's x and y, then check the object that stores all the trash can objects' coordinates and grab from the specific list.

I appreciate the input and will see if I can figure this out.
 

FrostyCat

Member
Cool, thanks for the suggestion. I feel like doing something like this in the create event would give all trash cans the same item though, when I want specific ones to have specific items.
Code:
items = ds_list_create();
ds_list_add(items, "Item A");
I'll do some reading on those topics to get a better understanding and will post if I have more questions. I was just learning as I go :D
This is not difficult at all if you know what Instance Creation Code is. I'm surprised you're taught 3D development before basic instance handling.

Create the empty items list in the Create event as usual. Then go into the room editor, double-click on an instance of obj_map_trash_can, then click "Creation Code", then add entries to items there. Repeat for the other instances.
 
This is not difficult at all if you know what Instance Creation Code is. I'm surprised you're taught 3D development before basic instance handling.

Create the empty items list in the Create event as usual. Then go into the room editor, double-click on an instance of obj_map_trash_can, then click "Creation Code", then add entries to items there. Repeat for the other instances.
Ah, that's seems really helpful! I didn't actually know about Instance Creation Code, so will definitely look more into that. As I mentioned before, I'm learning as I go and have already learned a few things with from this post.
 
Top