Containers

Just a pretty general question about in-game containers (think chests, loot, inventory, etc.) I would like to know how you guys make containers so I could build off of an example. I’m not very good with this, so the most I could think of is to do something with tables, and then I get stuck and don’t know where to go. : /

Thank you ahead of time for any help. :3

Not sure if this still is a problem to you considering it was sent nearly a month ago, but if it is, there is a simply way to solve this. You can use “lists” to store every item. For example :

Inventory = {"Sword", "Banana", "Axe"}

and after setting up your list, you can add items to it by doing

table.insert(Inventory, [2], "Pickaxe")

where Inventory is your list, [2] is your position in the table (in this case it would go before banana and after sword, and if you don’t do anything for pos, it defaults to last), and “Pickaxe” is the item you are adding.

if you want to check what is in the 3rd slot you can do

if Inventory[3] == "Axe" then

which means “if the 3rd slot is the axe, then (enter following code on next line).”

And that’s about it!

3 Likes

It may be around a month ago, but sometimes problems remain problems.
Anyway, what I meant was while, yes, I am able to index an item, how am I supposed to have the game recognize what item it is?
I know Minecraft has something like tags, but I have no idea how to do that (probably subtables?)

(also thanks for the help)

I’m a bit confused on what you mean by “recognise.” Because I showed you that you can check if an inventory slot is an axe or a pickaxe, or any other item, so then the game can recognise it is there? I don’t fully understand your question, so please elaborate

1 Like

I meant that the game would be able to know what graphics to put for an item in a certain slot and its name with stats or whatever, like if it’s the same item but with, say, different origins or something.
An example would be if there were two pickaxes but say one has less attack power than that of another. How would that be shown?

In that case, instead of just storing item names, you can store a table of data about the item.

{
    name = "Pickaxe",
    damage = 5,
    speed = 0.7
}

Hmm.
Then how would I put it in a container? Say a player’s inventory?
And if the player makes changes to said item, how would I save that change? Would I put in extra tags by inserting variables into the table?

The same as Alexusha’s example. You have your “Inventory” table, and instead of just inserting the name, you insert a table with whatever data you want.

-- Define some items:
local pickaxe1 = { name = "Pickaxe", damage = 5 }
local pickaxe2 = { name = "Pickaxe", damage = 6 }

-- Initialize the inventory list with these items already in it.
local inventory = { pickaxe1, pickaxe2 }

-- Define another item:
local otherItem = { name = "Sword", damage = 8 }
table.insert(inventory, otherItem) -- Add it to the inventory.

If you want to change the item’s stats then…you change the item’s stats, in its table of data.

pickaxe1.damage = 100

It sounds like you need to learn more about how Lua works and tables in particular. You will be severely limited in what you can do until you learn how to use tables comfortably. Did you read Defold’s lua manual? There are plenty of tutorials about lua tables around the internet, or you can keep asking here. Your life will be way easier if you learn the basics first.

5 Likes

I really need to start doing some reading in general. Just diving into it is a bad habit I’ve had for quite a while. : P (Mainly because when I was doing mods for a flash game, there was no manual or forum or whatever I could go to for a reference, so I had to wing it)

But what you say helps a lot. Thank you~

3 Likes