• Comparing an entity from a tracer with a specific entity
    6 replies, posted
SOLVED I want to compare the entity which a player is looking at and check whether or not it is a specific entity, not just the name of the entity as there may be more than one, but the specific entity. I tried to use this, but it doesn't do anything. By the way this is inside of an entitie's init.lua [lua] function plantcheck(ply, cmd, args) local trace = ply:GetEyeTrace() if trace.Entity:IsValid() then if trace.Entity == self.Entity then sell(ply) end end end [/lua]
[QUOTE=sintwins;19899460]I want to compare the entity which a player is looking at and check whether or not it is a specific entity, not just the name of the entity as there may be more than one, but the specific entity. I tried to use this, but it doesn't do anything. By the way this is inside of an entitie's init.lua [lua]function plantcheck(ply, cmd, args) local trace = ply:GetEyeTrace() if trace.Entity:IsValid() then if trace.Entity == self.Entity then sell(ply) end end end[/lua] [/QUOTE] The problem appears to be that you've not got self defined anywhere. By the looks of it plantcheck is supposed to be used with a console command, in which case self is probably one of the values in args. As you can't send an entity through a console command directly it must be sent as an integer, the entity's ID, which can be obtained by [b][url=wiki.garrysmod.com/?title=Entity.EntIndex]Entity.EntIndex [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. You can then get the entity reference back from the entity index by either the [b][url=wiki.garrysmod.com/?title=G.Entity]G.Entity [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] function or [b][url=wiki.garrysmod.com/?title=Ents.GetByIndex]Ents.GetByIndex [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. Assuming you've actually sent this index your code would look something like this: [lua]function plantcheck(ply, cmd, args) local ent = ply:GetEyeTrace().Entity if ValidEntity(ent) then if ent == Entity(tonumber(args[1])) then sell(ply) end end end[/lua] I hope you haven't made sell a global function, its name is too common to be safe like that, all it takes is another addon creator to make a global sell function (or even assign sell to a value) and your code will break, or his will. If someone installed both they want them both to work, which wouldn't be possible if you both used the same variable name.
Snip - I'll just send all of the code and see if you can figure it out :) It all works except for if I press the sell button when I use the entity nothing happens. Client [lua] include("shared.lua") notpressedrecently = true function ENT:Initialize() end function ENT:Draw() self.Entity:DrawModel() end function ENT:Think() end function presscheck() notpressedrecently = true end local function plantoptions() if notpressedrecently then notpressedrecently = false timer.Create( "pressed recently", 1, 1, presscheck ) local frame = vgui.Create("DFrame") frame:SetPos(20,20) frame:SetSize(300,100) frame:SetTitle("Drug Plant") frame:SetVisible(true) frame:SetDraggable(false) frame:ShowCloseButton(true) frame:SetMouseInputEnabled(true) frame:MakePopup() local sell = vgui.Create("DButton") sell:SetParent(frame) sell:SetText("Sell") sell:SetPos(10,30) sell:SetSize(50,20) sell.DoClick = function () RunConsoleCommand("plantcheck") end end end usermessage.Hook("openmenuplant", plantoptions) -- lua_openscript_cl darkrp/entities/entities/drugplant/cl_init.lua [/lua] Server [lua] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") colours = {} colours[1] = 255, 0, 0 , 255 colours[2] = 255, 255 , 255, 255 colours[3] = 125, 100, 50, 255 colours[4] = 153, 6, 2, 255 colours[5] = 100, 100, 100, 255 colours[6] = 0, 0, 0, 255 sellprice = {} sellprice[1] = 1000 sellprice[2] = 2500 sellprice[3] = 5000 sellprice[4] = 8500 sellprice[5] = 11000 function ENT:Initialize() self:SetModel("models/props/cs_office/plant01.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end self.damage = 100 self:SetNWInt("level",1) SelfEntityIndexPlant = self:EntIndex() end function ENT:OnTakeDamage(dmg) self.damage = self.damage - dmg:GetDamage() self.Entity:TakePhysicsDamage( dmg ) if self.damage >= 0 then return false else self:Destruct() self:Remove() end end function ENT:Destruct() Notify(self.Entity:GetNWEntity("owning_ent"), 1, 4, "Your Drug plant has died!") end function levelupplantdrug() levelforyeplant = ents.GetByIndex(SelfEntityIndexPlant):GetNWInt("level") + 1 ents.GetByIndex(SelfEntityIndexPlant):SetNWInt("level", level) colourchange() PlantLevelYe = levelforyeplant end function colourchange() levelcheckcolour = self.Entity:GetNWInt("level") self.Entity:SetColor(colours[levelcheckcolour]) end function ENT:Think() return true end function ENT:Use(activator, caller) if activator:IsPlayer() then umsg.Start("openmenuplant", caller) umsg.End() end end function sellplant(ply) amount = sellprice[self.level] ply:ChatPrint("sell function sell amount got") ply:SetNWInt("money", ply:GetNWInt("money") + amount) ply:ChatPrint("sell function money given") self.Entity:Remove() ply:ChatPrint("sell function ent removed") end function plantcheck(ply, cmd, args) local ent = ply:GetEyeTraceNoCursor().Entity if ValidEntity(ent) then if ent:EntIndex() == SelfEntityIndexPlant then amount = sellprice[(tonumber(PlantLevelYe))] ply:SetNWInt("money", ply:GetNWInt("money") + amount) ents.GetByIndex(SelfEntityIndexPlant):Remove() end end end concommand.Add("sell" ,sell) concommand.Add("plantcheck",plantcheck) timer.Create( "level up timer", 180, 5, levelup ) -- lua_openscript darkrp/entities/entities/drugplant/init.lua [/lua]
[QUOTE=sintwins;19915415]Snip - I'll just send all of the code and see if you can figure it out :) It all works except for if I press the sell button when I use the entity nothing happens.[/QUOTE] Ok, using a variable to save what entity you are, not a good idea because that'd mean it'd be only safe to use one because every entity you spawn will change the value of that variable. You'll have to change your code in a couple of ways, but you still have much to learn about indenting for readability and proper use of variables (you've made everything global grr!). When you press use you'll have to tell the client which entity they pressed Use on. So you'll need to do something like this: [lua]function ENT:Use(activator, caller) if activator:IsPlayer() then umsg.Start("openmenuplant", caller) umsg.Entity(self) -- new stuff here umsg.End() end end[/lua] Then clientside in the reciever you need the gui to remember which entity it belongs to. [lua] -- made this take um as a parameter, that's the usermessage you actually sent local function plantoptions(um) local ent = um:ReadEntity() -- read the entity if notpressedrecently then notpressedrecently = false timer.Create( "pressed recently", 1, 1, presscheck ) local frame = vgui.Create("DFrame") frame:SetPos(20,20) frame:SetSize(300,100) frame:SetTitle("Drug Plant") frame:SetVisible(true) frame:SetDraggable(false) frame:ShowCloseButton(true) frame:SetMouseInputEnabled(true) frame:MakePopup() local sell = vgui.Create("DButton") sell:SetParent(frame) sell:SetText("Sell") sell:SetPos(10,30) sell:SetSize(50,20) sell.DoClick = function() -- This time we run the console command with the EntIndex RunConsoleCommand("plantcheck", ent:EntIndex()) end end end[/lua] Now that the clientside stuff knows which entity it belongs to, when you press the button it sends the console command with the entity it belongs to, so it knows if it's allowed to do that. Finally we need to make the concommand bit use the ent index. [lua]function plantcheck(ply, cmd, args) local ent = ply:GetEyeTraceNoCursor().Entity if ValidEntity(ent) then if ent:EntIndex() == args[1] then -- args[1] is the first parameter of the command amount = sellprice[(tonumber(PlantLevelYe))] ply:SetNWInt("money", ply:GetNWInt("money") + amount) -- seriously, what? it's the same entity as ent, we've already proved that! -- ents.GetByIndex(SelfEntityIndexPlant):Remove() ent:Remove() end end end[/lua]
Ah that seems better. Thanks, I'll try it now. I don't get how Entities work though, is the lua for the entity instanced for each entity or run each time one spawns or? SNIP - Nothing happens when you press the sell button :(
[QUOTE=sintwins;19931834]Ah that seems better. Thanks, I'll try it now. I don't get how Entities work though, is the lua for the entity instanced for each entity or run each time one spawns or? SNIP - Nothing happens when you press the sell button :([/QUOTE] Put debug messages in each function to say what should be happening in that function, then you can trace out what should be happening. A good place to put one would be in the sell button's DoClick function. [lua] sell.DoClick = function() -- This time we run the console command with the EntIndex ErrorNoHalt("Sell button clicked, sending console command: plantcheck "..ent:EntIndex().."\n") RunConsoleCommand("plantcheck", ent:EntIndex()) end[/lua] That should tell you when you press the button what it's doing. Putting this debug info in your sell check would be a good idea too. [lua]function plantcheck(ply, cmd, args) local ent = ply:GetEyeTraceNoCursor().Entity if ValidEntity(ent) then if ent:EntIndex() == tonumber(args[1]) then -- args are strings, we want the number it represents amount = sellprice[(tonumber(PlantLevelYe))] ply:SetNWInt("money", ply:GetNWInt("money") + amount) -- seriously, what? it's the same entity as ent, we've already proved that! -- ents.GetByIndex(SelfEntityIndexPlant):Remove() ent:Remove() else -- we want to know if it isn't the same entity ErrorNoHalt(ent.." is not the same entity as "..args[1].."\n") end else ErrorNoHalt("Player Look ent is not valid\n") end end[/lua] Although I suspect the problem might be that we were comparing an ent index number to a string representing an ent index number. The problem may come about because strictly speaking 1 is not equal to "1".
SOLVED Yes, I made it print to console after every step and it seems to validate the entity but fail comparing the entities. I made a few alterations to the script, but it still seems to fail. I got rid of the eye trace as I felt that it wasn't really neccessary and now when you press the button it just sells the entity which was sent when you use the entity. Here is the new code. Server [lua] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") colours = {} colours[1] = 255, 0, 0 , 255 colours[2] = 255, 255 , 255, 255 colours[3] = 125, 100, 50, 255 colours[4] = 153, 6, 2, 255 colours[5] = 100, 100, 100, 255 colours[6] = 0, 0, 0, 255 sellprice = {} sellprice[1] = 1000 sellprice[2] = 2500 sellprice[3] = 5000 sellprice[4] = 8500 sellprice[5] = 11000 function ENT:Initialize() self:SetModel("models/props/cs_office/plant01.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end self.damage = 100 self:SetNWInt("level",1) timer.Create( "level up timer", 180, 5, function() levelforyeplant = self:GetNWInt("level") + 1 self:SetNWInt("level", levelforyeplant) function egg() self:SetColor(colours[levelforyeplant]) end end ) end function ENT:OnTakeDamage(dmg) self.damage = self.damage - dmg:GetDamage() self.Entity:TakePhysicsDamage( dmg ) if self.damage >= 0 then return false else self:Destruct() self:Remove() end end function ENT:Destruct() Notify(self.Entity:GetNWEntity("owning_ent"), 1, 4, "Your Drug plant has died!") end function ENT:Think() return true end function ENT:Use(activator, caller) if activator:IsPlayer() then umsg.Start("openmenuplant", caller) umsg.String(self:EntIndex()) umsg.End() end end function plantcheck(ply, cmd, args) entindex11= tonumber(args[1]) local amount = sellprice[Entity(entindex11):GetNWEntity("level")] Msg("got sell price") ply:SetNWInt("money", ply:GetNWInt("money") + amount) Msg("Given Money") Entity(args[1]):Remove() Msg("Removed Ent") end end concommand.Add("sell" ,sell) concommand.Add("plantcheck",plantcheck) -- lua_openscript darkrp/entities/entities/drugplant/init.lua [/lua] Client [lua] include("shared.lua") notpressedrecently = true function ENT:Initialize() end function ENT:Draw() self.Entity:DrawModel() end function ENT:Think() end function presscheck() notpressedrecently = true end local function plantoptions(um) local ent = um if notpressedrecently then notpressedrecently = false timer.Create( "pressed recently", 1, 1, presscheck ) local frame = vgui.Create("DFrame") frame:SetPos(20,20) frame:SetSize(300,100) frame:SetTitle("Drug Plant") frame:SetVisible(true) frame:SetDraggable(false) frame:ShowCloseButton(true) frame:SetMouseInputEnabled(true) frame:MakePopup() local sell = vgui.Create("DButton") sell:SetParent(frame) sell:SetText("Sell") sell:SetPos(10,30) sell:SetSize(50,20) sell.DoClick = function() RunConsoleCommand("plantcheck", ent) end end end usermessage.Hook("openmenuplant", plantoptions) -- lua_openscript_cl darkrp/entities/entities/drugplant/cl_init.lua [/lua] Snip, I now get this error : priceentities/drugplant/init.lua:72: attempt to perform arithmetic on local 'amount' (a nil value) [editline]01:49PM[/editline] Snip - I did some debugging and the entity is nill, causing amount to also become nil. [editline]02:15PM[/editline] Snip, lol the problem was that I needed to use Self.Entity and not self when getting it's index. :) It works now though so I am happy. Thankyou :D
Sorry, you need to Log In to post a reply to this thread.