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
[CODE]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[/CODE]
in the init.lua for the scrap
[CODE]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
[/CODE]
[QUOTE=funnygamemake;47498673]snip[/QUOTE]
[code]entity:GetClass() == "scrapentity"[/code]
and then,
[code]entity.Smelting = true[/code]
How would I use the ENT:Touch() function, properly
[QUOTE=funnygamemake;47515105]How would I use the ENT:Touch() function, properly[/QUOTE]
[CODE]
function ENT:Touch( entity )
if entity:GetClass() == "scrapentity" then
entity.Smelting = true
end
end
[/code]
and then I would do something like this right, to remove the scrap entity?
[CODE] function ENT:Touch( entity )
if entity:GetClass() == "scrapentity" then
entity.Smelting = true
entity:Remove()
end
end[/CODE]
[QUOTE=funnygamemake;47523029]and then I would do something like this right, to remove the scrap entity?
[CODE] function ENT:Touch( entity )
if entity:GetClass() == "scrapentity" then
entity.Smelting = true
entity:Remove()
end
end[/CODE][/QUOTE]
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
[CODE]
function ENt:Touch(other)
...
end
-- are equivalent to
function ENT.Touch(self, other)
...
end
[/CODE]
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
[CODE]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
[/CODE]
entite scrap init.lua
[CODE]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
[/CODE]
nut still nothing is working
[QUOTE=funnygamemake;47527530]okay so i have this and nothing seems to be working.
Melter entitie init.lua
[CODE]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
[/CODE]
entite scrap init.lua
[CODE]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
[/CODE]
nut still nothing is working[/QUOTE]
Melter entit[B]y[/B] 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
[CODE]Working? Entity [189][melter] Entity [185][scrap][/CODE]
[QUOTE=funnygamemake;47528072]So that is working, its removeing the entity as it is supose to, but the scrapnumber isnt working
console
[CODE]Working? Entity [189][melter] Entity [185][scrap][/CODE][/QUOTE]
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 [CODE]if self.scrapnumer >= 3[/CODE]... statement work
[QUOTE=funnygamemake;47528094]I see that, but how would i make the [CODE]if self.scrapnumer >= 3[/CODE]... statement work[/QUOTE]
What do you mean?
for this
[CODE]self.scrapnumber = self.scrapnumber + 1
if self.scrapnumber >= 3 then
ents.Create("weapon_crowbar")
self.scrapnumber = self.scrapnumber - 3
end[/CODE]
how would I get it to work, and what would I do with the table stuff
[QUOTE=funnygamemake;47528139]for this
[CODE]self.scrapnumber = self.scrapnumber + 1
if self.scrapnumber >= 3 then
ents.Create("weapon_crowbar")
self.scrapnumber = self.scrapnumber - 3
end[/CODE]
how would I get it to work, and what would I do with the table stuff[/QUOTE]
You would get it to work by [URL="http://wiki.garrysmod.com/page/ents/Create#/Examples"]creating the entity properly[/URL]. 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?.
[QUOTE=YourStalker;47530071](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?.[/QUOTE]
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.
Okay I seemed to get that done, how would I network the [B]scrapnumber[/B] variable that is in the [B]init.lua[/B] script the the [B]clientside[/B] script.
[QUOTE=funnygamemake;47539339]Okay I seemed to get that done, how would I network the [B]scrapnumber[/B] variable that is in the [B]init.lua[/B] script the the [B]clientside[/B] script.[/QUOTE]
For simplicity, you would do self:SetNWInt( "scrapnumber", self.scrapnumber ) and self:GetNWInt( "scrapnumber", 0 )
Could I also use the,
[CODE]util.AddNetworkString("scrapnum")[/CODE]
when the 2 entitys touch it dose,
[CODE]net.Start("scrapnum")[/CODE]
then it sends the variable,
[CODE]net.WriteString(self.scrapnumber)[/CODE]
then use the
[CODE]net.receive[/CODE]
sytax to call it, or dose it work that way?
Is it possible to use the util.AddNetworkstring, cause im trying to wrap my head around networking entitys, and data aswell.
[QUOTE=funnygamemake;47539401]Could I also use the,
[CODE]util.AddNetworkString("scrapnum")[/CODE]
when the 2 entitys touch it dose,
[CODE]net.Start("scrapnum")[/CODE]
then it sends the variable,
[CODE]net.WriteString(self.scrapnumber)[/CODE]
then use the
[CODE]net.receive[/CODE]
sytax to call it, or dose it work that way?
Is it possible to use the util.AddNetworkstring, cause im trying to wrap my head around networking entitys, and data aswell.[/QUOTE]
Well you could, but it's overly complicated for what you want. Why would you want to write a total of 8 lines if you could just write 2? If you insist you using the net library, then use net.WriteInt and net.ReadInt, instead of the string counterpart, seeing as how self.scrapnumber isn't a string and all that.
Okay so I'm kinda understanding this, but I'm having a problem.
The problem that Im having is this
[QUOTE]Attempted to create unknown entity type melter[/QUOTE]
Here is what I wrote so far, and if there's any-other problems, or if you have any tips on what I should do, please tell me.
[B][U]shared.lua[/U][/B]
[CODE]ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Melter"
ENT.Author = "Funnygamemaker"
ENT.Spawnable = false
ENT.AdminSpawnable = false
[/CODE]
[U][B]init.lua[/B][/U]
[CODE]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
ENT.ScrapNumber = 0
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:SetNWInt("ENT.ScrapNumber", ENT.ScrapNumber)
end
function ENT:Touch( entity )
if entity:GetClass() == "scrap" then
print( "Working?", self, entity )
ENT.ScrapNumber = ENT.ScrapNumber + 1
self:SetNWInt("ENT.ScrapNumber", ENT.ScrapNumber)
entity:Remove()
end
end
function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
umsg.Start("smelter", Caller)
umsg.End()
end
end
util.AddNetworkString( "SmelterCbarButton" )
net.Receive( "ScrapCbar", function( length, client ) -- receiving the varrible from client, in client when ever the Dbutton is pushed
local data = net.ReadString()
if data == "ScrapCbar" then
spawncbar(client:GetEyeTraceNoCursor().HitPos+Vector(0,0,30))
NewScrapAmount = self:GetNWInt("ENT.NewScrapNumber", ENT.ScrapNumber)
self:SetNWInt("ENT.ScrapNumber", NewScrapAmount)
end
end
function spawncbar() -- The HL2 crowbar weapon
Cbwep = ents.Create( "weapon_crowbar" )
Cbwep:SetPos(10,10,10)
Cbwep:Spawn()
end
util.AddNetworkString( "NewScrapAmount" )
[/CODE]
[B][U]cl_init.lua[/U][/B]
[CODE]
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self:DrawModel()
local Pos = self:GetPos()
local Ang = self:GetAngles()
owner = (IsValid(owner) and owner:Nick())
local text = Melter
Ang:RotateAroundAxis(Ang:Up(), 90)
CurAmtOfScrap = self:GetNWInt("ENT.ScrapNumber", 0)
end
function ENT:Think()
end
function SmeltingVgui(self)
local ply = LocalPlayer()
local sh = (400)
local sw = (300)
local frame = vgui.Create("DFrame")
frame:SetPos(sh,sw)
frame:SetSize(sh,sw)
frame:SetTitle("Smelter")
frame:SetVisible(true)
frame:SetDraggable(false)
frame:ShowCloseButton(true)
frame:MakePopup()
local CBarButton = vgui.Create("DButton", frame) --CrowBar weapon derma button
CBarButton:SetPos(80,80)
CBarButton:SetText("crowbar")
CBarButton:SetSize(40,40)
CBarButton.DoClick = function()
if CurAmtOfScrap < 3 then
ply:ChatPrint( "You Do Not Have Enough scrap" )
elseif
CurAmtOfScrap >= 3 then
CurAmtOfScrap = CurAmtOfScrap - 3
self.SetNWInt("ENT.NewScrapNumber", CurAmtOfScrap)
net.Start("SmelterCbarButton") -- the spawning variable of the crowbar
net.WriteString("ScrapCbar")
net.SendToServer()
ply:ChatPrint( "Congrats! You've got a crowbar!" )
end
end
local scrapnumber = vgui.Create("DLabel", frame) --Amount of scrap. Dermal Label
scrapnumber:SetPos(40,40)
scrapnumber:SetText(CurAmtOfScrap)
end
usermessage.Hook("smelter", SmeltingVgui)
[/CODE]
1- [I]After going through the github, it appears I was wrong.[/I]
2- In your ENT:Initialize() function, when setting up the NWInt for ScrapNumber, you refer to ENT.ScrapNumber and not self.ScrapNumber (ENT won't exist when it gets called, use self when refering to the entity).
3- owner is undefined in ENT:Initialize()
4- Same problem as 2, but in ENT:Touch()
5- You aren't forward declaring the NetworkString "ScrapCBar"
6- You are missing a ) after calling the net.Receive function in init.lua
7- Within the net.Receive function's second argument, the variable self is undefined
8- In cl_init.lua, you are declaring the global CurAmtOfScrap, when it should be a member of the self table, change the rest of the code to reflect this.
9- The first argument of SmeltingVgui is not self, it is the data received from the usermessage.
I hope this helps. Note: Syntax errors such as number 6 will stop your entity from being defined, fix these before trying number 1. And Zerf, feel free to rate me dumb, but only after you make a helpful post in the thread. Thanks babe.
for number 5, how would I declare the NetworkString "ScrapCbar", when I have it to just write a string "ScrapCbar", and where would I declare the NetworkString. To add the gloable variable "CurAmtOfScrap" to the self table would I just add the "self." to each "CurAmtOfScrap".
[QUOTE=funnygamemake;47559582]for number 5, how would I declare the NetworkString "ScrapCbar", when I have it to just write a string "ScrapCbar", and where would I declare the NetworkString. To add the gloable variable "CurAmtOfScrap" to the self table would I just add the "self." to each "CurAmtOfScrap".[/QUOTE]
Basicly change this
[lua]
util.AddNetworkString( "SmelterCbarButton" )
[/lua]
to this
[lua]
util.AddNetworkString( "SmelterCbar" )
[/lua]
And yes, just add "self." infront.
Also I've noticed your changing the ENT.ScrapNumber NWInt clientside. Don't do that. For one, it won't even send to the server, but secondly, never trust your client. Always assume they are trying to find a flaw in your code. Do all your checks serverside (for example, if you're sending something to the server that requires special conditions, check both clientside and serverside, that way the server only has to check real and hacked requests, instead of every single request that might not be valid.)
So I would have to do the amount of scraps checks for serverside, but then how would I call that function in the button DoClick function,
also all I have to do to declare the "ScrapCbar" is to change the [CODE]util.AddNetworkString( "SmelterCbarButton" )[/CODE] to [CODE]util.AddNetworkString( "SmelterCbar" )[/CODE], But I dont understand since I've got nothing calling or creating "SmelterCbar" string/varible.
and also when I got in game, and I press the "Use" key on the entity, there's more than one derma frame that open, and its just multiple copies of the same derma frame.
[QUOTE=funnygamemake;47559684]So I would have to do the amount of scraps checks for serverside, but then how would I call that function in the button DoClick function,
also all I have to do to declare the "ScrapCbar" is to change the [CODE]util.AddNetworkString( "SmelterCbarButton" )[/CODE] to [CODE]util.AddNetworkString( "SmelterCbar" )[/CODE], But I dont understand since I've got nothing calling or creating "SmelterCbar" string/varible.
and also when I got in game, and I press the "Use" key on the entity, there's more than one derma frame that open, and its just multiple copies of the same derma frame.[/QUOTE]
You would move the substraction of the ScraNumber serverside, and do the checks you were doing clientside serverside.
You are hooking into a net message with that name. You would get an error otherwise (although you are still using the name clientside).
You change the use type of your entity to change how often the Use function gets called. [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetUseType]Entity:SetUseType[/url]
Sorry, you need to Log In to post a reply to this thread.