Need help with SIMPLE entity based item creation

For the past week I have started a gamemode. In the gamemode, I have completed databases, a HUD, and a few other simple things like falldamage and damage effects. This isn’t really a gamemode to be played, this is more of a playground for me to work in so that I can see what I can do and how to do it.

  I am stuck on the inventory/item creation. I know that I need to have a base entity but from there I am stuck. My database is a table which has a table in it named "items" and in that a table for each item. Right now I'm concerned only with being able to spawn items and pick them up, and the item tables have the item model and the item name. That's it. 

  So, this is what I am thinking I need to do. I need to declare the tables for the items in the code, and assign them each an itemID (which is a number). The itemID would be used as a numeric key and that will be what's stored in the player's inventory table, and the value assigned to the key will be the amount of the item that the player possesses. When a player drops an item it will get which number he's trying to drop and try to reference the model info from there to spawn a prop.

  So far I believe that would work, but what about entities? I want to be able to use the items and pick them back up and use them. How do I attach the base entity to the item table? In the item table I could have a use function that would run whenever somebody hit "use" on the entity but IDK how the hell I would make it so that the base entity referenced the item table that it should be referencing. 

  If I have a can of soda and it is a base_item entity, how does the game know that when I press E on the base_item that it's trying to run the SODA's use command and not the use command for, say, a bandage? Since they use the same entity?

  The syntax is clear to me but the method is not. Perhaps I'm over complicating it a bit. I am not sure how to network variables, and I'm sure it would be useful, but when I looked it up I didn't see how it would solve my problem.

                    Thank you for reading my giant block of words and I look forward to some help!

I am currently experimenting with this sort of thing also. How it works for me is; I have a table of items and then a table of their attributes inside of them. One of those is the function that is to be ran when you use the item. The way to do it is to use the ENT:Use()
and when used, get the item table and run the function. Look at this:



local items = {}

items["WaterBottle"] = { 
	name = "Bottle of Water",
	weight = 2,
	capacity = true,
	model = "models/props/cs_office/water_bottle.mdl",
	ent = "item_basic",
	use = true,
	sizex = 4,
	sizey = 2,
	zoom = 10,
	func = function( ply ) ply:SetHealth( ply:Health() + 100 ) end
 }

function getItem( class )

	return items[class]

end


In this case, i could run the function by doing getItem( self.ID ).func(). That is, after i set the entities ID to the appropriate class name.

Alright, thanks for this. However, I don’t understand what self.ID is. Is that how the item_basic knows which table it is supposed to reference?

Soo, when you spawn the entity, what would that look like? I’m just trying to see how one item_basic is assigned an ID or name that differentiates it from the rest of the item_basic’s in the game. Does it happen when you first spawn the item_basic ( like using a net variable to tell it which table it needs to look at to get it’s model info and such )?

And just to verify, would this code:



local example = "WaterBottle"
local name = getItem( example ).name


return “Bottle of Water”? I am used to C++ so I get Lua documentation confused with some things.

Edit:
Oh, so self.ID. I understand it now but how do you set it? That’s all I need to know.

“self” is referring to the entity. So when you do self.ID, it is setting the key ID in the entities table. It doesn’t have to be ID, it could be anything. You could do self.ItemName or something of the sort. Either way you would do something along the lines of self.ItemName = “WaterBottle”.

Alright. Thank you very much for your help! I’m sure I can try this now.

Yep, if you have any other problems you can contact me on steam and i will be happy to help!

Alright, so everything is working very well but I have come across an issue. I made an entity for the gamemode and to narrow things a bit I put everything into the shared.lua file.


ENT.Type = "anim"
ENT.Base = "base_entity"
ENT.PrintName = "item"
ENT.Spawnable = true
ENT.AdminSpawnable = true

--init.lua
if ( SERVER ) then

	function ENT:Initialize()
		local tab = items_GetItem( self.ID )
		self.Entity:SetModel( tab.model )
		
		self.Entity:PhysicsInit( SOLID_VPHYSICS )
		self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
		self.Entity:SetSolid( SOLID_VPHYSICS )
		--self.Entity:SetUseType( SIMPLE_USE )
		
		local PhysObj = self.Entity:GetPhysicsObject()
		if (PhysObj and PhysObj:IsValid()) then
			PhysObj:EnableGravity( true )
			PhysObj:Wake()
		end
	end

end

--cl_init.lua
if ( CLIENT ) then

	function ENT:Think()
	end

	function ENT:Draw()
		self:DrawModel()
	end
end

Now, I can see the shadow of the model and confirm that it is correct but I cannot see the model itself. It’s just invisible. Any ideas? searching this on google was a no go.

[editline]15th March 2014[/editline]

Nevermind. I’ve got it. I separated them into the three normal files and it works fine now.

@Inevitibility, You should use entities/my_entity.lua format, instead of entities/myentity/shared.lua for single file entities.

[editline]15th March 2014[/editline]

Also, ENT.AdminSpawnable = true doesn’t do anything at all since GMod 13 ( Which is out for over a year. )

I had just put that in in case it was messing with the spawning or something. I spawn it using a concommand and not through the menu anyways so both of those are obsolete.