Inventory System

I’ve been wanting to create an inventory/item system for some time now. I don’t know where to start and i’m having a little bit of a hard time. I was wondering if anyone had a super simple basic inventory that would show me the basic ‘framework’ of an inventory and item system. I have looked at the code of a few others but i think it would be a lot easier for me to learn if i saw something cleaner and more simplistic instead of the finished products with bunched up code that i have been looking at for the past few days.

It’s as simple as learning how tables in Lua work, which is mentally really easy.

My main problem is the item system. I will work on it tomorrow, maybe i just need to give my brain a break for a little while. Can’t think straight right now.

I wroted something for you

[lua]
hook.Add(“PlayerSpawn”, “InitInventory”, function(ply)

ply.Inventory = {}

end)

local meta = FindMetaTable(“player”)

function meta:giveItem(ent)

table.insert(self.Inventory,ent:GetClass())

end

function meta:dropItem(class,remove)

if(table.HasValue(self.Inventory,class)) then

	if(!remove) then
		local ent = ents.Create(class)
		ent:SetPos(self:GetPos() + self:GetForward() * 16)
		ent:Spawn()
		ent:Activate()
	end
	table.RemoveByValue(self.Inventory,class)
end

end

function meta:removeAll()

table.Empty()

end

function meta:howManyObjects(class)

local iterator = 0

for k,v in pairs(self.Inventory) do
	if(class == v) then
		iterator = iterator +1
	end
end

if(iterator == 0) then
	MsgN("We don't have that item")
elseif(iterator == 1) then
	MsgN("We got ",iterator," items")
else
	MsgN("We got ",iterator,"s items")
end

end

function meta:giveItem(class,quantity,ply)

if(quantity < 1) then return false end

if(self:howManyObjects(class) > quantity) then
	ply:giveItem(class)
	self:removeItem(class,true)
end

end
[/lua]

I don’t know if it’s enough clear, but enjoy it

It depends on how you want your items setup, if items are randomized and have random values then you won’t be relying on an item ID system. By that I mean instead of saving the item table data on the player, you’ll only be saving the item’s ID on the player to then later on access that item’s properties within your database.

If you want a direct example you can look at _Undefined’s Pointshop code.

That’s just horrible, read over your code again and maybe you’ll see that you’re resetting the player’s inventory every time he spawns.

I don’t want to do anything random. ID’s is how i am planning on doing it. Thanks for the help!

Yeah, if you’re death, you lost your items (I don’t know if you would like to use your items when you’re dead)

This is a good start, http://www.garrysmod.org/downloads/?a=view&id=97223

I used this and modified it, works 100%.
I am sure you can use this to serve your purposes

Also these youtube tutorials should help you get an idea of how to do it.
They’re in order by the way.

[quote=“Jeezy, post:10, topic:170087”]

Also these youtube tutorials should help you get an idea of how to do it.
They’re in order by the way.

[/QUOTE]

Those tutorials aren’t that great…

He’s using NWInt which is a lazy and inefficient way to network data that’s supposed to be shared.

Also, you should never rely on the PlayerDisconnected hook like he does in the video, because IIRC it doesn’t get called when a players game crashes.

I know they’re not great, I suppose I should have mentioned that.
I just don’t know of any other inventory tutorials.

Other than this one.

If the OP still needs help and can specify what type of inventory he want’s I’ll write one, but serverside only.

What’s the purpose of your inventory? Carrying props in sandbox mode? An mmo/rpg-like inventory for storing various items and whatnot in your game mode? (like this?) The latter is a much more involved process, like you say, you would need to know how you are planning on scripting/storing the information for said items

I just got back from vacation so sorry for the late response. I was hoping to create something like dayz’s inventory and how it has the grids and the ability to equip items easily through the menu but I realized that would be a little to difficult to make even with the tutorial. That’s why I decided to keep it simple. I don’t want you to go out of your way to write me something, I am in the car on the way home now so when I get something I will repost it in the thread.