i cant seemed to wrap my head around this??

Okay so im trying to make a quick detection between two entity’s. The two entitys that I have is a melter and scrap. Im wondering how will I be able to detect when ever the scrap touchs the melter entity. Im also wondering for when the scrap touches the melter, I want it to network it to be able to call some functions and stuff.

this is what I got so far, its bad but its all i got.

in the init.lua for melter


function ENT:Initialize()
	self:SetModel("models/props/cs_militia/furnace01.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	phys:Wake()
	self:SetTrigger(true)
	meltingscrap = false


end

function ENT:Touch( entity )
   if entity == scrap then
       //Idont know how to send a string or function or something to the scrap init.lua
       //what I want is for it to be something like this
      meltingscrap = true
end

if scrapnumber >= 3 then
    entity:create(pistol)
end

in the init.lua for the scrap


function ENT:Initialize()
	self:SetModel("models/props_junk/garbage_metalcan002a.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	phys:Wake()
	

end

if meltingscrap == true then
    self:remove()
    scrapnumber = scrapnumber + 1
    meltingscrap == false
end



entity:GetClass() == "scrapentity"

and then,


entity.Smelting = true

How would I use the ENT:Touch() function, properly



function ENT:Touch( entity )
    if entity:GetClass() == "scrapentity" then
        entity.Smelting = true
    end
end


and then I would do something like this right, to remove the scrap entity?


 function ENT:Touch( entity )
    if entity:GetClass() == "scrapentity" then
        entity.Smelting = true
        entity:Remove()
    end
end

Yes, although that will make it hard to call functions if it doesn’t exist.

I presume you would want to change it from
entity.smelting = true
to
self.smelting = true

^what he said.

Keep in mind (this is something I found confusing as a beginning Lua coder)

That functions like



function ENt:Touch(other) 
   ...
end
-- are equivalent to 
function ENT.Touch(self, other)
   ...
end


The : is adding an invisible self argument everywhere you see it used.

I only bring this up since it seems you’ve been avoiding it religiously in your code - perhaps because you don’t know what it is.
Pardon if this isn’t helpful xD.

okay so i have this and nothing seems to be working.

Melter entitie init.lua


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
	self:SetModel("models/props_c17/FurnitureCouch002a.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	phys:Wake()
	
	meltingscrap = false
	scrapnumber = 0
end



	function ENT:Touch( self, entity ) 
   if entity:GetClass() == "scrap" then
       //Idont know how to send a string or function or something to the scrap init.lua
       //what I want is for it to be something like this
	 meltingscrap = true
	 self.Remove()
	end
end



if scrapnumber >= 3 then
    ents.Create("weapon_crowbar")
end

if self.meltingscrap == true then
	self.scrapnumber = self.scrapnumber + 1
    self.meltingscrap = false
end



entite scrap init.lua


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

 meltingscrap = false

function ENT:Initialize()
	self:SetModel("models/props_junk/garbage_metalcan002a.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	phys:Wake()
	

end
 


nut still nothing is working

Melter entity init.lua

[lua]
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)
include(“shared.lua”)

function ENT:Initialize()
self:SetModel(“models/props_c17/FurnitureCouch002a.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
phys:Wake()

self.scrapnumber = 0

end

function ENT:Touch( self, entity )
if entity:GetClass() == “scrap” then
//Idont know how to send a string or function or something to the scrap init.lua
//what I want is for it to be something like this
meltingscrap = true
self.Remove()

	self.scrapnumber = self.scrapnumber + 1
	if self.scrapnumber >= 3 then
		ents.Create("weapon_crowbar")
		self.scrapnumber = self.scrapnumber - 3
	end
end

end
[/lua]

Try this.

still nothing happens to both entities,

[Lua]
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)
include(“shared.lua”)

function ENT:Initialize()
self:SetModel(“models/props_c17/FurnitureCouch002a.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
phys:Wake()

self.scrapnumber = 0

end

function ENT:Touch( entity )
if entity:GetClass() == “scrap” then
print( “Working?”, self, entity )
entity:Remove()

	self.scrapnumber = self.scrapnumber + 1
	if self.scrapnumber >= 3 then
		ents.Create("weapon_crowbar")
		self.scrapnumber = self.scrapnumber - 3
	end
end

end
[/lua]

run this and tell me if it prints to console.

So that is working, its removeing the entity as it is supose to, but the scrapnumber isnt working

console


Working?	Entity [189][melter]	Entity [185][scrap]

Scrapnumber is a variable on the melter now. It makes it easier. You problem was with the self argument you added in. A user explained this previously in the thread:

[lua]
local Table = {}

function Table:DoSomething() PrintTable( self ) end

Table:DoSomething() – This will print itself.
Table.DoSomething(Table) – This is another way of writing the above

function Table:DoSomethingElse(self) PrintTable( self )
Table:DoSomethingElse() – This will error because the function above expects 2 arguments, both with the same name, so the second one takes precedence, but it’s nil because we are only passing one argument (Table)
[/lua]

I see that, but how would i make the


if self.scrapnumer >= 3

… statement work

What do you mean?

for this


self.scrapnumber = self.scrapnumber + 1
		if self.scrapnumber >= 3 then
			ents.Create("weapon_crowbar")
			self.scrapnumber = self.scrapnumber - 3
		end

how would I get it to work, and what would I do with the table stuff

You would get it to work by creating the entity properly. What table stuff?

(Trying to get this cleared up in my head)

  1. So in the code you posted above self is referring to whatever is before the : ?
    So like with ENT:Touch() self would be referring to ENT? Does the same to with stuff like SWEP:OnDrop() where self would be the SWEP?

  2. I understand doing something like self: or ply:Health(120) sets something (like a player’s hp in this example) but when should something like self.whatever or self.weaponscraps be used, are those for tables?.

Yes to #1.

On number 2, you’re treating self as a table, pretty much. They should be used when you aren’t networking data, and it doesn’t need to stay for too long.