LoginLogin

Storage system Help!

Root / Programming Questions / [.]

WuulfCreated:
I'm trying to figure out how to create a chest storage system for what is essentially a 2d Minecraft clone, the problem i am having is figuring out how to create a new array whenever a new chest is placed and then reference back to that specific array when attempting to open that particular chest. I am completely clueless as to how I could accomplish this, could someone please help me figure this out?

The only thing I can think of off the top of my head is to create a two dimensional array with a set amount (meaning that only a limited number of chests can be placed) and have the game assign a value to each chest placed (it should be two dimensional so that the second dimension can be used for items). There's probably a much better way to do this, but since two dimensional arrays can't be expanded, this is all I can think of. Maybe one way to get around this is to use comma-separated values? But you would have to create a parser for that.

I've grown accustomed to using 1 dimensional arrays pretty often in my code, but I have no clue on how arrays with more than one dimension work

Are there any tutorials explaining this?

Try to think of it like a 2D graph. For example: DIM ARR[8,5] DIM initializes an array. In this case, we've initialized a 2D array. There are 9 different x values (0 included). Each x value has 6 corresponding y values (also 0 included). Each of these y values can contain a number. So let's say we set ARR[5,1] to 32. Here's how we can do this: ARR[5,1]=32 That means that we've set the x value 5's corresponding y value 1 to contain the number 32. With a 1D (normal) array, it's only x values, where each x value contains a number. Does this help?

Yes, very much, thanks a ton, I understand it a lot better now, so its somewhat like a 1D array except it's got sub-arrays. This is really helpful, and I'm perfectly fine with the limitation on the amount of chests I was just trying to save myself having to manually write down each and every array, and setup the same system for the inventory. So thanks a lot you've saved me a lot of time.

No problem! Do note that 1D arrays can be expanded with PUSH, and shrunken with POP, so if you're using item IDs you can use comma-separated values or something similar. Most people don't like 2D arrays for this reason. If you want to do this you'll have to make a string array though. So it's just a matter of whether you want to stick with easy to maintain 2D arrays or harder to implement 1D arrays.

A 1-dimensional array would probably be the best course of action. Basically, each time a chest is placed, you'd PUSH that array by whatever the length of the chest is, and to reference that chest's data you'd look at (ChestID*Size+Size-1). The only problem with this is garbage collection, but that can be accomplished by recycling chest IDs.

A 1-dimensional array would probably be the best course of action. Basically, each time a chest is placed, you'd PUSH that array by whatever the length of the chest is, and to reference that chest's data you'd look at (ChestID*Size+Size-1). The only problem with this is garbage collection, but that can be accomplished by recycling chest IDs.
I see what you mean, allocating parts of an array to chest IDs? That's a very efficient way of implementing this. 2D arrays can't be expanded or shrunken (for whatever dumb reason), so a way to get around this by using 1D arrays is definitely preferable, and removes the issue of having a limit on chests.

I appreciate the help, though I may just stick to using a 2D array, I don't think most players are going to craft over 1000 chests so I think I can manage. Again thanks so much for the help, I'll try implementing it soon as I can. Once I've made a prototype, I can post the key, if you want to check it out.

Um I kind of have an idea, dispite being away from the community for a while. What if every time you made a new chest the array size is updated, with the size being a variable with the number of chests × the number of items in the chest, then when theres a tile with a chest, give it 2 value, the sprite index and the chest index, the sprite just to show what block it is, and the chest index to be used with the array. Then when using the chest it loads the chest index into a variable wich is multiplied by the number of items in a chest, then plus one to load from the array, i guess when using the chest load it into a separate array, then when closing, save the contents back into position in the original array. I dont see why that wouldn't work. If changing the size of the array overites the contents of it, load it into a temporary array to store all its contents, update the array size, then load it back in, saving the index for the new chest onto the chest index. Idk how your code is set up but I think im going to do that when I begin my "earth" reboot on smilebasic 4.

You could use a 1d array and treat it as a 2d array by this method:
ITEM=CHEST[Y*CHEST_WIDTH+X]
where CHEST_WIDTH is the number of items slots along a horizontal row. You will need to dimension the chest as
DIM CHEST[CHEST_HEIGHT*CHEST_WIDTH]
1d arrays are a better imo to use than 2d

I appreciate the help, though I may just stick to using a 2D array, I don't think most players are going to craft over 1000 chests so I think I can manage. Again thanks so much for the help, I'll try implementing it soon as I can. Once I've made a prototype, I can post the key, if you want to check it out.
That might take up A LOT of memory (for example, if your chests include 27 slots, like Minecraft, and you occupy space for 1000 chests via a 2D array, that's a lot of memory taken up by that dedicated space). If you're going for a Minecraft-like game, you're going to want as much memory free as possible so no "Out of Memory" errors occur. You should definitely use a 1D array in that case, so the chests only take up memory as they're placed. Chatter and MZ952 showed good alternatives to using 2D arrays.